GitHub Push Action
Today we’re looking at ad-m/github-push-action, the third and final action related to pushing changes made during a GitHub Actions workflow run back to a repo.
What does it do?
Unlike git-auto-commit-action
and add-and-push
, github-push-action
doesn’t handle committing files for you. It expects your workflow to add and commit any files you need and handles the act of pushing changes to GitHub.
You might be thinking that this seems like an odd action to build; surely it’s as simple as running git push
and it works? Unfortunately not! The repository provided in your workspace doesn’t have permission to push changes back so you need to configure your repo to use the GITHUB_TOKEN
provided in order to have write permission.
How does it work?
This action is an interesting one as it uses both javascript
and bash
components to get the job done. start.js
is the main entry point, which allows the action to run start.sh
on Linux, MacOS and Windows runners.
Let’s take a look at start.js
first as it’s the main entry point:
- The action accepts both
branch
andrepository
as inputs. - If
repository
isn’t provided it defaults toGITHUB_REPOSITORY
in the environment. If it is provided, that will be used as the destination repository, allowing your workflow to push to other repositories. - If
branch
is provided it is used as-is. If it is not provided the script uses the GitHub API to fetch the default branch for the repo and uses that as the input value - Finally, it executes
start.sh
ensuring that bothINPUT_BRANCH
andINPUT_REPOSITORY
have values
What’s interesting to note here is that start.js
does not have any dependencies. It uses the http
module for making HTTP requests and the child_process
module to executestart.sh
. actions/exec
is typically used for this use case, but github-push-action
does not use any dependencies so that there is no build step required for the JS action.
Now, on to start.sh
:
- We see some new inputs that have defaults set in the script
INPUT_FORCE
(defaultfalse
) which will rungit push --force
INPUT_TAGS
(defaultfalse
) which will rungit push --tags
INPUT_DIRECTORY
(default.
, the current directory) which controls which directory the push is run fromREPOSITORY
, which is set toINPUT_REPOSITORY
orGITHUB_REPOSITORY
. This is a duplication of the logic instart.js
- The
GITHUB_TOKEN
secret is required to push to the repo, so we check that it’s been set asINPUT_GITHUB_TOKEN
- If
INPUT_FORCE
is true then add--force
to the push options - If
INPUT_TAGS
is set then add--tags
to the push options - Configure the remote URL using the
user:[email protected]
format - Push the current
HEAD
commit toINPUT_BRANCH
Common use cases
As with the last post, this action is most useful for committing back changed files such as style fixes or build artefacts.
Run eslint
and commit back:
yaml
name: Lint source codeon: pushjobs:run:name: Lint with ESLintruns-on: ubuntu-lateststeps:- name: Checkout repouses: actions/checkout@v2- name: Set up Node.jsuses: actions/setup-node@v1with:node-version: 12.x- name: Install dependenciesrun: npm install- name: Update source coderun: eslint "src/**" --fix- name: Commit filesrun: |git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"git config --local user.name "github-actions[bot]"git add srcgit commit -m "ESLint fixes"- name: Push changesuses: ad-m/github-push-action@masterwith:github_token: $branch: $
Or build your project and commit back the files:
yaml
name: Automatic Compileon: pushjobs:run:name: Compileruns-on: ubuntu-lateststeps:- name: Checkout repouses: actions/checkout@v2- name: Set up Node.jsuses: actions/setup-node@v1with:node-version: 12.x- name: Install dependenciesrun: npm install- name: Compilerun: npm run build- name: Commit filesrun: |git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"git config --local user.name "github-actions[bot]"git add libgit commit -m "Built files"- name: Push changesuses: ad-m/github-push-action@masterwith:github_token: $branch: $