A few times I’ve found the need to setup a GitHub Actions workflow that downloads a file and commits that file back to the repository. I had a need to pull in a JSON file and update it periodically to a GitHub repository. There are other scenarios where this may be needed. This article will show you the steps necessary to setup your own GitHub Actions workflow that does the same.
Table of Contents
Step 1: Setup the initial GitHub Action workflow to run on Ubuntu Linux
To start the GitHub Actions workflow, you need to configure the build: runs-on to use the VM image that your code to download the file will require. In this article we’ll use "ubuntu-latest" and run the workflow on the latest version of Ubuntu Linux.
Start your new GitHub Actions workflow with the following YAML:
name: Download File
on:
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
For this example, let’s give the GitHub Actions workflow the name of .github/workflows/download-file.yml. Also, this example is setting up a GitHub Actions manual trigger using workflow_dispatch. You will want to setup the trigger needed for your specific workflow scenario.
The first step in this GitHub Actions workflow is to checkout the git repository using the actions/checkout@4 action. This is the first action that will be defined in almost every GitHub Actions workflow, since it’s necessary in order to acquire the files in the repo before the build, deployment or other actions can be done using those files.
Step 2: Download the File
The next step in the workflow will be to download the file needed. Since the workflow is already configured to use Ubuntu Linux (via setting runs-on to ubuntu-latest), the easiest way to download a file is to use a bash script that executes either the curl or wget commands to download the file.
Here’s an example script action that uses curl to download a file from the Internet:
- name: Download File
run: |
curl https://build5nines.com/folder/file.json --output ./file.json
working-directory: ${{ github.workspace }}
This “Download File” action run a bash command with the following parameters:
curlarguments: Thecurlcommand is passed the URL to the file to download, along with the--outputargument that is specifying the filename to write the file to../file.jsontells it to output the file to a file namedfile.jsonin the working directory of the action.working-directory: Thescriptactions working directory is set, in this example, to the value of${{ github.workspace }}. This will tell the action to use the location of where the repository files were checked out to as the working directory.
As a result of this configuration of the action, the file specified in the URL will be downloaded to the specified file name located in the root of the working directory; which is also where the git repo files are copied to. To put the downloaded file in a subfolder of the repo folder structure, then change the --output location to specify that subfolder; such as ./folder/file.json. Also, be sure to set the file extension of the downloaded file correctly.
Step 3: Commit File to GitHub Repo
Now that the file has been downloaded and placed in the desired location, then the next step is to commit that file back to the GitHub repository. The github.workspace is the location where the GitHub repo has been checked out, and where the downloaded file has been placed. This sets everything up to be able to use the Git CLI to add and commit the file to the source GitHub repository.
Here’s an example GitHub Action that will run the Git CLI commands to add the downloaded file and commit it to the GitHub repo:
- name: Commit and Push Changes
run: |
git config --global user.name "${{ github.actor }}"
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
git add file.json
git commit -m 'Update downloaded file'
git push
The first thing needed is to setup the Git user name and email, using the git config command. In order to setup the GitHub Actions workflow to set the necessary Git user name and email that is compatible with the GitHub repository, the following is configured:
git config --global user.name: Theuser.nameconfig needs to be set to the GitHub username of the user that setup the GitHub Actions workflow. This can be done by setting theuser.namewith the"${{ github.actor }}"expression. This will get the GitHub username of the user that setup the GitHub Actions workflow (generally your username, as you’re creating the workflow), so they will need permission to make commits to the repo.git config --global user.email: Theuser.emailconfig needs to be set to the email for the GitHub user. There’s no way to automate retrieval of the email address the user uses to login to GitHub. However, GitHub does have a “default” email for every user at the domain of@users.noreply.github.comwhere the username is the address. This can be setup by configuring the GitHub Actions workflow to setup theuser.emailwith the"${{ github.actor}}@users.noreply.github.com"expression.
The Git CLI commands are the normal commands used. As you can see, git add is executed to stage the new file to get it ready for the commit. Then the git commit command is executed to commit the changes “locally” within the workspace, then git push is executed to push the commit up to the source Github repository.
Summary
Automating file downloads and commits using GitHub Actions workflows may not be all that common of a task performed, but I have found a couple times where I’ve needed to do it. By following the steps outlined in this article, you can set up a GitHub Actions workflow to handle this task. Experiment with additional steps and conditions to customize the workflow to your needs.
Original Article Source: GitHub Actions: Download File and Commit to Repository written by Chris Pietschmann (If you're reading this somewhere other than Build5Nines.com, it was republished without permission.)
Microsoft Azure Regions: Interactive Map of Global Datacenters
Create Azure Architecture Diagrams with Microsoft Visio
Byte Conversion Calculator from KB, MB, GB, TB, PB
Retirement of AzureEdge.net DNS: Edg.io Business Closure and What You Need to Know
How to Write AI Prompts That Output Valid CSV Data





