GitHub Actions: Fetch JSON from API and output to JSON file in repo

Hello!
I’m brand new to GitHub actions. What I’m trying to achieve is an action that automatically fetches a JSON from an API and then outputs the JSON to a file in my repository. This is my current code:

name: availableLanguages

on:
  schedule: 
    - cron: "0 */2 * * *"
  workflow_dispatch:

jobs:
  availableLanguages:
    runs-on: ubuntu-latest
    steps:
    - shell: bash
      env:
        SECRET: ${{ secrets.mySecret }}
      run: |
        curl -X GET https://crowdin.com/api/v2/projects/idWasHere/languages/progress -H "Authorization: Bearer $SECRET" -o availableLanguages.json

It doesn’t throw any errors, but it also doesn’t write any file. What should my code look like?
Thanks!

I assume you want to commit the fetched file to your repository? In that case you’ll first need to retrieve your repository before anything else:

steps:
  - uses: actions/checkout@v2

Then fetch or otherwise update the file.

After that you can create a commit just like you would on the command line, with git add and git commit. Before committing you should set user name and email to something suitable for your automated update.

1 Like

Thanks for your answer :smiley: Is there any way you could show me a code example? GitHub Actions is still very confusing to me.

Edit: I used this code, but it still doesn’t write any file:

name: availableLanguages

on:
  schedule: 
    - cron: "0 */2 * * *" 
  workflow_dispatch:

jobs:
  availableLanguages:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - shell: bash
      env:
        SECRETPAT: ${{ secrets.CROWDINPAT_availableLanguages }}
      run: |
        curl -X GET https://crowdin.com/api/v2/projects/428718/languages/progress -H "Authorization: Bearer $SECRETPAT" -o availableLanguages.json
        git config --global user.name "GitHub Action"
        git config --global user.email "quintenra@gmail.com"
        git add availableLanguages.json
        git commit -m "Language fetching"

What do you mean by “doesn’t write any file”?

I notice that there’s no “git push” in your workflow, if you mean that the commit doesn’t make it to your GitHub repository that’s the reason. If you make commits using Actions you need to push, like from any other repository.

1 Like

Absolutely no idea why I missed that. I thought I had put it in, but apparently not. Thank you so much for your help! It’s all working now. Have an awesome day :smiley:

1 Like