Create GitHub Actions to makes and Commit Changes

In this article, I will show how I step-by-step created a github action that checkout, zipped, and added the zipped file to the repo, each time I commit to main.

When packaging and deploy an application, there are cases that the build process needs to make changes to the branch before, during, or after the build and commit that changes to the repository. A common example is when cutting a release build, a version number can be updated and then checked back into the future branches of the repo.

Step Breakdown

To set up a GitAction that makes changes and push

  1. Identify a repo to add the GitHub action to
  2. Create a Personal Access Token (PAT)
  3. Add the PAT to the repo secrete
  4. Create a GitHub Action as part of that repo
  5. Verify

I am using my repo ray_service to set this git action up.

Generate Personal Access Token

Next I need to generate a PAT. First log on to GitHub account

  1. Go to YourEmoji->Settings ->

The at the left panel, select Developer Settings

Let’s use the fine-grained token to

Then click on the Generate New Token button. You may be asked to re-authenticate

Configure the Find-Grained PAT

Then give a token name, set the token expiration, and select which repository to grant access. I recommend to create one access token per repo for security and limit the impact if the token does get compromised before it expires.

In this example, I name my token ray_service_repo_token , set the expiration day to default 30 days, and slect ray_service repo

Select Permissions (or Scopes)

Next, set up the permissions, if all you will use this token for is to update the repo content (commit, pull, push, etc), then select Content. To see what each permission set provides, you can click on the little information icon.

Click the Generate Token button

Copy the token value to a safe place.

Add PAT to the Repo Secret

Now go to the repo that you would like to add gitAction to. In this example, it’s ray_service, and select repo Settings

Go to the left panel, and find Security section, then select Secrets and variables. Select Actions

Then select Repository secrets, and click the New repository secret button

Give your secret a name (ex MY_TOKEN) Then at the Secret box, paste in the token value from before, then click Add secret

Add a Workflow

To add a workflow, create a yml file that has

name: 
on: 
   <event>
jobs:
   <job>:
       runs-on: <docker-image>
       steps: 
           - name: <step-name>
              run: <cmd>
     

To check out the repo and make changes to it, I need to use the off-the-shelf checkout and action-commit-push actions.

action resourcegithub link
github actionshttps://github.com/actions
devops-infrahttps://github.com/devops-infra/

To add a github action to a repo, add a folder .github/workflow, and in the folder, add a git action file called archive_project.yml

name: Archive Project

on:
  push:
    branches:
      - main

jobs:
  zip-and-archive:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          token: ${{ secrets.MY_TOKEN }}

      - name: Install ZIP
        run: sudo apt-get install zip

      - name: Create Archive Directory
        run: mkdir -p archive

      - name: Zip project
        run: zip -r archive/${{ github.sha }}.zip . -x "*.git*" ".github/*" "archive/*"
      - name: Commit and push changes
        uses: devops-infra/action-commit-push@master
        with:
          github_token: ${{ secrets.MY_TOKEN }}
          commit_message: update archive

To test the git action, just make a change and pushed it to the main branch, and you can see the Action kicked off and then created zipped file in

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply