Want to learn how to build your own GitHub Actions? Click here

GitHub Push Action

26 Mar 2021 in Action Spotlight

Push any committed changes back to your repo automatically using the provided `GITHUB_TOKEN`

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 and repository as inputs.
  • If repository isn’t provided it defaults to GITHUB_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 both INPUT_BRANCH and INPUT_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:

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 code
on: push
jobs:
run:
name: Lint with ESLint
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/[email protected]
- name: Set up Node.js
uses: actions/[email protected]
with:
node-version: 12.x
- name: Install dependencies
run: npm install
- name: Update source code
run: eslint "src/**" --fix
- name: Commit files
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add src
git commit -m "ESLint fixes"
- name: Push changes
uses: ad-m/[email protected]
with:
github_token: $
branch: $

Or build your project and commit back the files:

yaml
name: Automatic Compile
on: push
jobs:
run:
name: Compile
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/[email protected]
- name: Set up Node.js
uses: actions/[email protected]
with:
node-version: 12.x
- name: Install dependencies
run: npm install
- name: Compile
run: npm run build
- name: Commit files
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add lib
git commit -m "Built files"
- name: Push changes
uses: ad-m/[email protected]
with:
github_token: $
branch: $

Useful links