Github action — CI/CD

Jiyun Park
8 min readMay 31, 2021

Github workflow 와 self-hosted runner 를 활용하여 빌드, 배포, 테스트를 자동화시키자.

🚀 Github Action 으로 CI/CD 구축하기

Workflow

  1. .github/workflows/release-test.yml , release-prod.yml 생 성
  2. workflow 작성

(1) self-hosted 환경 test 빌드-배포

name: release-test
on:
push:
branches:
- master
jobs:
build:
env:
SERVER: 'test'
runs-on: [self-hosted, linux, x64, TAG_NAME]
steps:
- uses: actions/checkout@master
- name: Setup Node.js environment
uses: actions/setup-node@v2
with:
node-version: '14.x'
- name: Install yarn
run: npm install -g yarn
- name: Cache dependencies
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }} // in case of NPM : hashFiles('**/package-lock.json')
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: yarn install
- name: Test build
run: yarn build
- name: Upload output files on S3
run: aws s3 cp ./build s3://github-action-${{ env.SERVER }} --acl public-read --recursive
- name: Invalidate CloudFront cache
run: aws cloudfront create-invalidation --distribution-id ${{ secrets.CDN_DISTRIBUTION_ID_TEST }} --paths "/*"
- name: Get current date
run: echo "::set-output name=date::$(TZ=UTC-9 date +'%Y-%m-%d %H:%M:%S')"

(2) self-hosted 환경 prod 빌드-배포

name: release-production
on:
push:
tags:
- 'release-[0-9]+'
- 'hotfix-[0-9]+(?=-[0-9]$)'
jobs:
build:
env:
SERVER: 'production'
runs-on: [self-hosted, linux, x64, TAG_NAME]
steps:
- uses: actions/checkout@master
- name: Setup Node.js environment
uses: actions/setup-node@v2
with:
node-version: '14.x'
- name: Install yarn
run: npm install -g yarn
- name: Cache dependencies
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: yarn install
- name: Production build
run: yarn build
- name: Upload output files on S3
run: aws s3 cp ./build s3://github-action-${{ env.SERVER }} --acl public-read --recursive
- name: Invalidate CloudFront cache
run: aws cloudfront create-invalidation --distribution-id ${{ secrets.CDN_DISTRIBUTION_ID_PROD }} --paths "/*"
- name: Get current date
run: echo "::set-output name=date::$(TZ=UTC-9 date +'%Y-%m-%d %H:%M:%S')"

3. 테스트

  • git push origin master
  • git push origin release-210515

4. 배포 시나리오

  • master 브랜치에 병합 시 test 환경에 배포
  • master 브랜치에 태그를 추가할 시 production 환경에 배포

github-hosted 로 작성한 경우,

runs-on, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY 변경 및 추가

name: release-test
on:
push:
branches:
- master
jobs:
build:
env:
SERVER: 'test'
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@master
- name: Setup Node.js environment
uses: actions/setup-node@v2
with:
node-version: '14.x'
- name: Install yarn
run: npm install -g yarn
- name: Cache dependencies
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }} // in case of NPM : hashFiles('**/package-lock.json')
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install dependencies
run: yarn install
- name: Test build
run: yarn build
- name: Upload output files on S3
run: aws s3 cp --region ap-northeast-2 ./build s3://github-action-${{ env.SERVER }} --recursive
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY_SECRET }}
- name: Get current date
run: echo "::set-output name=date::$(TZ=UTC-9 date +'%Y-%m-%d %H:%M:%S')"

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response