[Git] GitHub Actions에 대하여
Application을 개발하며 버그가 일어나면 모두에게 그 사실을 알린다거나 문제가 해결되면 프로젝트의 '버그'란에 있는 문제를 삭제한다거나 하는 등 반복되는 작업이 수없이 많다. 개발자가 일일이 이를 확인하여 처리하는 것도 확실한 방법이지만 절대 효율적인 방법이라 할 수는 없다. 이런 반복 작업을 자동으로 대신 해주는 플랫폼이 많이 있는데 대표적으로 Jenkins, circleci 그리고 지금 알아볼 GitHub Actions등이다.
✅ GitHub Action는 CI/CD방법론을 사용하는 플랫폼이다. 이게 무엇이냐? 앱 개발단계에서 자동화를 도입하여 고객에게 수시로 어떤 행동을 수행하는 것이다. CI/CD의 주요 개념인 Continuous integration(지속적인 통합), Continuous Delivery(지속적인 전달)이 많은 뜻을 담고 있다. 완료된 작업은 조금씩 자주 통합해야 하고 통합된 프로그램은 피드백으로 사전 검열을 받는다. 이때 continuous delivery는 관리자의 승인을 받아 배포하는 방식이고, continuous deployment는 관리자의 승인 없이도 배포 가능한 방식이다.
자동화하고자 하는 작업은 *.yml에 작성한다. yml(YAML)은 사람이 쉽게 읽고 쓸 수 있도록 만들어진 데이터 직렬화 양식이다.
📚 Workflows
이벤트 발생시 자동 실행되는 프로시져를 말한다. 위 그림에서 보듯! Workflow 1개 이상의 Job들로 구성되어 있다.
📚 Event
Wokrflow가 발생하도록 유발시키는! 누군가 커밋을 한다거나, 이슈가 생기거나, pull-request를 한다거나 하는 등의 특정 행위다.
📚 Jobs
동일한 runner에서 실행되는 스텝들의 모임이다. 기본적으로 여러 workflow에 여러 job이 있다면 이들은 병렬적으로 동작한다. 순차적으로 실행하고자 한다면 needs 명령어를 사용한다. 같은 workflow 안에 있는 job끼리 데이터를 공유할 경우 GitHub artifacts로 저장할 수 있다.
# homework.txt라는 파일을 artifact에 업로드하고 다운로드하는 job의 예시다.
name: Share data between jobs
on: [push]
jobs:
job_1:
name: Add 3 and 7
runs-on: ubuntu-latest
steps:
- shell: bash
run: |
expr 3 + 7 > math-homework.txt
- name: Upload math result for job 1
uses: actions/upload-artifact@v3
with:
name: homework
path: math-homework.txt
job_2:
name: Multiply by 9
needs: job_1 # job_1이 실행된 후에만 job_2가 실행되도록
runs-on: windows-latest
steps:
- name: Download math result for job 1
uses: actions/download-artifact@v3
with:
name: homework
- shell: bash
run: |
value=`cat math-homework.txt`
expr $value \* 9 > math-homework.txt
- name: Upload math result for job 2
uses: actions/upload-artifact@v3
with:
name: homework
path: math-homework.txt
job_3:
name: Display results
needs: job_2 # job_2가 실행된 후에만 job_3가 실행되도록
runs-on: macOS-latest
steps:
- name: Download math result for job 2
uses: actions/download-artifact@v3
with:
name: homework
- name: Print the final result
shell: bash
run: |
value=`cat math-homework.txt`
echo The result is $value
📚 Steps
job 안에서 명령을 실행하는 개인적인 단위. step은 action(uses 명령어) 또는 shell command(run 명령어)다. job 안의 각기 step은 같은 runner에서 실행되므로 actions는 서로 데이터를 공유할 수 있다.
📚 Actions
Workflow의 가장 작은 구성 요소. 필요한 actions을 사용자가 수정, 생성하거나 GitHub community에서 골라올 수 있다. workflow에서 action을 사용하려면 step을 반드시 포함해야한다. 코드 재사용이 가능한 단위다.
📚 Runner
사용가능한 job을 듣고 한 번에 하나의 job을 수행한뒤 과정, 로그, 결과 등을 GitHub에 보고하는 역할이다. GitHub Actions runner application이 설치된 서버다.
Runner에는 두 가지 종류가 있다. GitHub가 호스팅하는 GitHub-hosted runner와 사용자가 직접 자신의 머신을 사용할 수 있는 Self-hosted runner다.
✅ GitHub-hosted runner
Ubuntu Linux, Microsoft Windows, macOS를 기반으로 새로운 가상 환경에서 workflow의 각 작업이 실행된다.
✅ Self-hosted runner
다른 운영 체제가 필요하거나 특정 하드웨어 구성이 필요한 경우 직접 runner를 만들 수 있다. 사용자가 많은 제어권을 갖게 된다. 물리적, 가상, 컨테이너, 사내 또는 클라우드에서 제공한다.
# 다음은 .yml 파일의 작성 예시다
name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: [push]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v4
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."
push 이벤트가 발생하면 shell command를 수행하라는 yml 파일이다.
✅ Matrix Strategy
각 job을 다른 환경에서 수행하도록 하는 전략이다. 각 행과 열에 다른 조건을 주고 모든 조합에 따라 여러 버전의 job이 수행되도록 자동화할 수 있다.
jobs:
example_matrix:
strategy:
matrix:
os: [ubuntu-22.04, ubuntu-20.04]
version: [10, 12, 14]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.version }}
ubuntu22.04 | ubuntu-20.04 | |
version 10 | ||
version 12 | ||
version 14 |
위 yml 파일을 수행하면 표와 같은 상황에서 각 조합에 맞는 모든 job을 수행하게 된다. workflow는 6번의 job을 수행한다.
✅ 조건에 맞는 step 또는 job만 수행하도록 할 수도 있다.
on:
push:
branches:
- main
main branches의 push일 경우에만 step/job을 실행하도록 하는 명령이다.
name: check-main-branch
on:
push:
workflow_dispatch:
jobs:
check-main-branch:
if: github.ref_name == 'main'
runs-on: ubuntu-latest
steps:
- run: echo "This is the main branch."
push한 branch가 main인 경우에만 step/job을 실행하도록 하는 명령이다.