내일배움캠프 - 도커 실무 강의 3주차 개발일지 - (2)

Dongwoo Kim·2022년 7월 15일
0

스파르타 코딩클럽

내일배움캠프 AI 웹개발자양성과정 2회차

도커 실무 강의 3주차 개발일지 - (2)

1. CI/CD 란?

1) CI - Continuous Integration

: 여러 개발자들이 함께 개발을 하는 과정에서 코드가 잘 작동 하는지를 확인하는 것 - 자동화된 테스트

2) CD - Continuous Deployment(or Delivery)

: 반영된 소스코드가 실제 서비스에도 자동으로 반영이 되게 하는 것 - 자동화된 배포

In software engineering, CI/CD or CICD is the combined practices of continuous integration (CI) and (more often) continuous delivery or (less often) continuous deployment (CD).
... Automated tests verify the software functionality, and automated deployment services deliver them to end users. The aim is to increase early defect discovery, increase productivity, and provide faster release cycles.
- https://en.wikipedia.org/wiki/CI/CD

2. Github Action

: github에서 CI/CD를 할 수 있는 툴

GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.
GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.
- https://github.com/features/actions

1) Github Action - CI

1. `Workflows`: 자동화 하려고 하는 과정들
    - 한개 또는 여러개의 job으로 구성되며, event에 의해서 시작
    - 빌드, 테스트, 릴리즈, 배포 등의 작업
2. `Events` : workflow를 trigger되는 행동들
    - push, pull request, cronjob 등이 있음
3. `Jobs`: 동일한 runner에서 실행하려고하는 여러개의 step의 모임
4. `Steps`: job을 구성하는 한개의 커맨드로 action이거나 shell command로 구성
5. `Actions`: 다른 곳에서 정의된 커맨드의 모음
6. `Runner`: Job이 실행되는 환경
# .github/workflows/ci-pipeline.yaml

name: ci-pipeline
on:
  push:
    branches:
      - main
jobs:
  run-test-code:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: "3.8"
      - run: pip install -r requirements.dev.txt
      - run: pytest

2) Docker image build 자동화

2-1) Docker Access Token

https://hub.docker.com/settings/security

2-2) Github Repository secrets

: github repository - settings - Secrets - Actions 에서 Repository secret 생성

DOCKERHUB_TOKEN : Doccker Access Token
DOCKERHUB_USERNAME: Docker hub username

2-3) yaml 파일 수정

# .github/workflows/ci-pipeline.yaml

name: ci-pipeline
on:
  push:
    branches:
      - main
jobs:
  run-test-code:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: "3.8"
      - run: pip install -r requirements.dev.txt
      - run: pytest
  build-image:
    needs: run-test-code
    runs-on: ubuntu-latest
    steps:
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME }}/docker-memo:latest
      -
        name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}

3) Github Action - CD

3-1) Github Repository secrets

: github repository - settings - Secrets - Actions 에서 Repository secret 생성

PRIVATE_KEY: ssh keypair
HOST: EC2 public IPv4 address

3-2) yaml 파일 수정

# .github/workflows/ci-pipeline.yaml

name: ci-pipeline
on:
  push:
    branches:
      - main
jobs:
  run-test-code:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: "3.8"
      - run: pip install -r requirements.dev.txt
      - run: pytest
  build-image:
    needs: run-test-code
    runs-on: ubuntu-latest
    steps:
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME }}/docker-memo:latest
      -
        name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}
  cd-pipeline:
    needs: build-image
    name: continuos deploy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - name: copy file via ssh password
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ec2-user
          key: ${{ secrets.PRIVATE_KEY }}
          port: 22
          source: "docker-compose.yaml"
          target: "/home/ec2-user/"
      - name: executing remote ssh commands using password
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ec2-user
          key: ${{ secrets.PRIVATE_KEY }}
          port: 22
          script: "docker-compose pull && docker-compose up -d"

4) CI/CD 확인

profile
kimphysicsman

0개의 댓글