Github Actions를 이용한 CI/CD 구축

chris·2020년 12월 3일
6

Github Actions

목록 보기
1/1
post-thumbnail

환경

Server : AWS EC2 ubuntu 20.04
Language : Go v1.15.5

1. Github Access Token 생성

Github access token은 ghcr(github container registry)로 docker image 배포하고 runner 실행 시 docker login에 사용된다.
Github Access token 생성은 다음 이미지 순서대로 진행하면 된다.



생성된 token은 반드시 안전한 저장공간에 저장을 해야 한다. 페이지를 이탈하는 순간 생성된 token을 다시 확인 할 수 있는 방법이 없다.

2. Repository의 secrets에 등록

Repository의 secrets에 등록을 해야만 해당 프로젝트의 workflow에서 사용할 수 있다.

1-5에서 복사한 token을 Value 항목에 붙여넣고 Add secret 버튼을 클릭하여 등록한다.

3. Workflow 추가

Repository에 push, pull request 등 이벤트가 발생시 실행될 일련의 동작을 정의한다.
여기서는 docker image를 빌드하고 github container registry에 배포하고 최종 서버 배포까지의 동작을 정의하였다.
deploy의 runs-on 항목은 [self-hosted, label-development]로 설정하였는데, 여기서 self-hosted는 필수값이다.
self-hosted로 설정하지 않으면 서버에 등록한 runner가 실행되지 않는다.
label-development는 실행될 runner를 특정 지을 수 있는 구분값이다.

Workflow 파일 내용

name: DEV CI

on:
  push:
    branches: [ develop ]
env:
  DOCKER_IMAGE: ghcr.io/namil-han/ci-cd
  VERSION: ${{ github.sha }}

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Check out source code
        uses: actions/checkout@v2
      - name: Set up docker buildx
        id: buildx
        uses: docker/setup-buildx-action@v1
      - name: Cache docker layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ env.VERSION }}
          restore-keys: |
            ${{ runner.os }}-buildx-
      - name: Login to ghcr
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.CR_PAT }}
      - name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.buildx.outputs.name }}
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ env.DOCKER_IMAGE }}:${{ env.VERSION }}

  deploy:
    needs: build
    name: Deploy
    runs-on: [ self-hosted, label-development ]
    steps:
      - name: Login to ghcr
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.CR_PAT }}
      - name: Docker run
        run: |
          docker ps -q --filter "name=cat" | grep -q . && docker stop cat && docker rm -fv cat
          docker run -d -p 8000:8000 --name cicd --restart always ${{ env.DOCKER_IMAGE }}:${{ env.VERSION }}
          
  cleanup:
    needs: [ build, deploy ]
    name: Clean up docker images
    runs-on: [ self-hosted, development ]
    steps:
      -   name: Clean up docker images
          run:
            docker rmi $(docker images --filter "before=${{ env.DOCKER_IMAGE }}:${{ env.VERSION }}" --filter=reference="${{ env.DOCKER_IMAGE }}:*" -q)

4. Runner 생성 및 등록



6, 7번 항목 모두 선택입력이지만 등록하는 것을 권장한다.
6번의 경우, development, staging, production 등 복수개의 runner을 등록 시 github에서 구분할 수 있는 runner 이름이고
7번은 github actions의 workflow 실행 시 runs-on 항목에 등록할 구분 값으로 사용된다.

5. 끝

위와 같이 설정을 마쳤다면 develop branch로 새로운 commit이 발생하면 자동으로 서버 배포까지 진행된다.
처리결과는 등록한 이메일 또는 slack으로 받을 수 있다.

profile
software engineer

0개의 댓글