GITHUB ACTION (Quick guide)
Tags: github, git, devops, opensource
Published: 2022-11-11 09:01:25

** 
This article is written as a reminder of myself, if you can put this to good use, you are welcome to comment to add content.

# What is GITHUB ACTION
it is [CICD](https://en.wikipedia.org/wiki/CI/CD) of Github .
like a normal CICD in the world but it can integrate some action on git for example push , release tag , create branch , delete branch etc ...

# How it work ?
If you imagine deploying typically requires writing a script and using a machine to run . yes that it !

> Hint machine for run script it naming runner

Github jest provide share server for run your script (CICD)

> [But you can register runner for run it](https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners)

# The components of GitHub Actions
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6r4flyb3tg66g08isag2.png)
in work flow it have 5 component
- [Workflows](https://docs.github.com/en/actions/using-workflows) : Workflows are defined in the `.github/workflows` directory in a repository
- [Events](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows) : 
An event is a specific activity in a repository that triggers a workflow run
- [Jobs](https://docs.github.com/en/actions/using-jobs) : A job is a set of steps in a workflow that execute on the same runner. 
- [Actions](https://docs.github.com/en/actions/creating-actions) : An action is a custom application for the GitHub Actions platform that performs a complex but frequently repeated task
- [Runner](https://docs.github.com/en/actions/hosting-your-own-runners) : A runner is a server that runs your workflows when they're triggered.

# Example work flow
This workflow use for deploy cloudfront aws . it trigger when published release tag start with `releases-prod/*` 

Why i use when published release tag 
because when deploy project it should mark version of deployment example 
- `releases-prod/v1.0.1` : when deploy production it check if in below work flow
- `releases-staging/v1.0.1` : when deploy staging it check if in below work flow
- `releases-develop/v1.0.1` : when deploy develop it check if in below work flow


# Repo/.github/workflows/main.yml
When you create this file in the repo, it will be triggered by itself when you do something that matches the event you set.

> hint: uses for call SDK on Github action example [gcloud](https://github.com/google-github-actions/setup-gcloud),[aws](https://github.com/aws-actions/configure-aws-credentials) ,[huawei](https://github.com/messense/huaweicloud-github-runner) etc..

```
name: Node.js CICD
on:
  release:
    types: [published]

jobs:
  deploy-prod: # jobs name deploy-prod
    runs-on: ubuntu-latest # script run on ubuntu
    if: startsWith( github.ref, 'refs/tags/releases-prod/')
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_PROD }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_PROD }}
          aws-region: ap-southeast-2

      - name: yarn install
        run: yarn

      - name: test
        run: yarn test

      - name: build production
        run: yarn build:production

      - name: aws sync s3
        run: aws s3 sync ./build s3://my-hello-aws --acl public-read
      
      - name: deploy aws cloudfront
        run: aws cloudfront create-invalidation --distribution-id MY_DISTRIBUTE_ID --paths '/*'
      
      - name: print
        run: echo "deploy PROD"

#------------ deploy staging ------------
  deploy-staging: 
    runs-on: ubuntu-latest
    if: startsWith( github.ref, 'refs/tags/releases-staging/')
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_PROD }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_PROD }}
          aws-region: ap-southeast-2

      - name: yarn install
        run: yarn
      
      - name: build staging
        run: yarn build:staging

      - name: aws sync s3
        run: aws s3 sync ./build s3://my-hello-aws --acl public-read
      
      - name: deploy aws cloudfront
        run: aws cloudfront create-invalidation --distribution-id MY_DISTRIBUTE_ID --paths '/*'
      
      - name: print
        run: echo "deploy STAGING test"

#------------ deploy develop ------------

  deploy-develop: 
    # needs: build-develop
    runs-on: ubuntu-latest
    if: startsWith( github.ref, 'refs/tags/releases-develop/')
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_DEVELOP }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEVELOP }}
          aws-region: ap-southeast-2

      - name: yarn install
        run: yarn
      
      - name: build development
        run: yarn build:development

      - name: aws sync s3
        run: aws s3 sync ./build s3://my-hello-aws --acl public-read
      
      - name: deploy aws cloudfront
        run: aws cloudfront create-invalidation --distribution-id MY_DISTRIBUTE_ID --paths '/*'
      
      - name: print
        run: echo "deploy develop test"
```
# Secret manager
> `${{ secrets.AWS_ACCESS_KEY_ID_PROD }}` this value store in  secret manager Github can config in below image .when you use it in CICD command it will replace by ****** for hidden.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yt0y804e6xnpccx8morg.png)

## Example log
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r51vwc2wufk7s4wq33gv.png)

## Easy roll back 
When you bomb yourself in the new version.
you can click action and rerun action for roll back follow image
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouj49weywhqnz7zvsdfw.png)

click rerun all jobs for rollback to old version
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny1jijasl7fzttbhzpn9.png)

# Try it on your self
- [Structure explain](https://docs.github.com/en/actions/examples/using-concurrency-expressions-and-a-test-matrix)
- [starter](https://docs.github.com/en/actions/using-workflows/creating-starter-workflows-for-your-organization)
- [build test](https://docs.github.com/en/actions/automating-builds-and-tests/about-continuous-integration)

# Reference 
- [Github action official](https://docs.github.com/en/actions/)
- [Github action repo](https://github.com/actions)