DEV Community

Cover image for GITHUB ACTION (Quick guide)
kanthakran
kanthakran

Posted on

GITHUB ACTION (Quick guide)

**
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 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

The components of GitHub Actions

Image description
in work flow it have 5 component

  • Workflows : Workflows are defined in the .github/workflows directory in a repository
  • Events : An event is a specific activity in a repository that triggers a workflow run
  • Jobs : A job is a set of steps in a workflow that execute on the same runner.
  • Actions : An action is a custom application for the GitHub Actions platform that performs a complex but frequently repeated task
  • Runner : 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,aws ,huawei 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"
Enter fullscreen mode Exit fullscreen mode

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

Example log

Image description

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

click rerun all jobs for rollback to old version
Image description

Try it on your self

Reference

Top comments (0)