본문 바로가기
Programming

[ CI/CD ] Github Action | yml 파일 뜯어보기

by 코뮤(commu) 2021. 12. 3.
728x90
반응형

해당 포스팅은 아래 링크를 보고 제 식으로 한번 더 정리한 것입니다.

 

https://zzsza.github.io/development/2020/06/06/github-action/

 

Github Action 사용법 정리

Github Action 사용법 및 cron 사용 방법에 대해 정리한 글입니다 Github Action으로 YES24 IT 신간을 파이썬으로 크롤링 후 Issue에 업로드하는 예제가 있습니다 Github Action with Python Github action with cron, Github a

zzsza.github.io

 

 


 

 

최근 CI/CD에 눈떴다.

사실 나는 개인적으로 하는 작은 프로젝트를 많이 해서 그런지, 테스트와 배포에 대한 자각이 크게 없었던 것 같다.

 

 

CI/CD

 

CI : 테스트, 빌드 자동

CD : 배포 자동

 

 

CI 와 CD 를 각각의 다른 도구에서도 할 수 있겠지만,

이번 포스팅에서는 둘 다 가능한 Github Action을 다뤄보겠다.

 

 

 

 

깃헙 actions 탭에 들어가면 저렇게 set up a workflow yourself 가 있다.

클릭해보자.

 

 

 

그럼 이렇게 main.yml 파일을 커밋할 수 있게 이미 만들어진 틀이 생긴다.

 

 

 

이제 이 yml 각각을 하나하나 뜯어보자.

 

 

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo Hello, world!

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.

 

 

먼저 가장 윗 단의 name 은 해당 워크플로우의 이름을 지정한다.

 

on 은 Event 에 대해 작성한다.

어떤 이벤트가 발생했을 때 workflow가 trigger 되는가? 에 대한 질문을 정의한다고 보면 되겠다.

사실 on 이라고 표현되어 있어 꽤 직관적이다.

 

on 안에는 push, pull_request, schedule, release 등이 들어갈 수 있다.

이러한 이벤트들을 더 알고 싶다면 아래 링크를 참고하면 되겠다.

 

 

https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows

 

Events that trigger workflows - GitHub Docs

Configuring workflow events You can configure workflows to run for one or more events using the on workflow syntax. For more information, see "Workflow syntax for GitHub Actions." Example: Using a single event on: push Example: Using a list of events on: [

docs.github.com

 

배열로 작성하는 것도 가능하다.

 

on: push

on: [pull_request, issues]

 

주의해야할 것은, yml 의 문법 상 콜론뒤에는 무조건 공백이 하나 들어가야한다.

 

다음은 workflow_dispatch 이다.

GitHub 외부에서 발생하는 활동에 대한 워크플로우를 트리거하려는 경우

repository_dispatch라는 웹훅 이벤트를 트리거할 수 있다는데 솔직히 안써보니까 와닿질 않아 잘 모르겠다.

자세한 내용은 아래 링크에서 확인할 수 있다.

 

https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-event

 

 

다음은 jobs 다.

 

결국 이벤트 발생시 해야하는 일을 정의하는 파트이다.

여러개의 job 이 존재한다면, Default 로 병렬실행한다.

 

현재 코드는 build job 을 생성하고, 그 job 안에 step 이 존재한다.

runs-on은 어떤 OS 에서 실행될 것인지에 대한 것이고,

steps 의 uses는 어떤 액션을 사용할지 지정한다.

액션을 사용자가 지정할 수도 있지만, 이미 만들어져있는 액션도 존재한다.

uses 에는 이미 만들어져있는 액션을 사용할 때 쓰면 된다.

 

 

 

파이썬을 가지고 크롤링한 정보를 주기적으로 github issue 에 update 하는 것은

다음 포스팅에서 다루도록 하겠다.

 

 

728x90
반응형