Skip to content

Github actions 기초

Seungjae Lee edited this page Nov 16, 2022 · 4 revisions

Introduction

Github actions는 push, issue 등록, PR merge 등 특정 이벤트가 발생했을 때 runner를 이용해 규정한 동작을 해주는 기능이다.

Getting Started

Github repository를 하나 만들어보자.

Actions 탭에 들어간 뒤 configure 버튼을 클릭해보자.

repo/.github/workflow/01-main.yml

# action의 이름
name: Hello World!

# 어떤 event가 발생할 때 동작할 것인가
on: [push]

jobs:
  build:
    # runner
    runs-on: ubuntu-latest

    steps:
    - name: Run pwd
      run: pwd
    - name: Run ls -al
      run: ls -al

Code 탭으로 돌어가보면 .github/workflow/01-main.yml 이 생성되었음을 확인할 수 있다.

이제 repository를 로컬로 clone 하자.

Github checkout

.github/workflow/github-checkout.yml을 생성하자.

02-github-checkout.yml

# action의 이름
name: Github Checkout

# 어떤 event가 발생할 때 동작할 것인가
on: [push]

jobs:
  build:
    # runner
    runs-on: ubuntu-latest

    steps:
    # uses: 다른 사람이 만든 action을 사용한다.
    # actions/checkout@v3: 현재 repository를 git clone하고 checkout 해준다.
    # https://github.com/actions/checkout 또는 Marketplace
    - uses: actions/checkout@v3	
    - name: Run pwd
      run: pwd
    - name: Run ls -al
      run: ls -al

이제 Actions 탭에서 Github Checkout 액션의 빌드 내용을 확인해보자.

image

우리의 github repository가 잘 clone, checkout 되었음을 확인할 수 있다.

Context

03-context.yml

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

name: Context

# Controls when the workflow will run
on: [push]

# 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
      - name: "context"
        env:
          COMMIT_ID: ${{ github.sha }}
        run: echo "Commit ID = $COMMIT_ID"

image

context는 ${{ <context> }} 문법으로 사용할 수 있다.

github는 github, env, job, steps, runner, secrets 등 다양한 context를 제공한다.

예를 들어, github.sha는 workflow를 trigger한 commit의 SHA를 의미한다.

context를 사용하면 runner에게 다양한 정보를 전달할 수 있다.

secrets

Settings > Secrets > Actions 에 들어가서 New repository secret 버튼을 눌러보자.

PASSWORD라는 이름의 secret을 추가한 뒤 새로운 workflow를 생성해보자.

04-secrets.yml

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

name: Secrets

# Controls when the workflow will run
on: [push]

# 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
      - name: Print Password
        env:
          PASSWORD: ${{ secrets.PASSWORD }}
        run: echo "secrets.PASSWORD = $PASSWORD"

image

내가 입력한 비밀번호는 가려진 채로 표시된다.

Bash Shell Commands 적용하기

05-run-sh.yml

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

name: Run sh file

# Controls when the workflow will run
on:
  push:
    branches: [main]

# 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:
      - uses: actions/checkout@v3

      - name: "쉘 스크립트 실행"
        run: sh ./shell-commands/05-run-sh.sh

shell-commands/05-run-sh.sh

echo 현재 경로
pwd
echo 파일 목록
ls -al
echo 자바가 설치되어 있나요?
java -version
echo Git이 설치되어 있나요?
git --version
echo Build tool이 설치되어 있나요?
mvn --version
gradle --version
ant -version

image

의외로(?) Java 관련 툴들은 이미 설치가 되어 있다.

Multi-step Github Actions

step을 여러 개로 나눠두면 코드를 볼 때 뿐만 아니라 빌드 결과를 볼 때도 가독성이 더 좋다.

Multi-job Github Actions

07-multi-job

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

name: Run sh file

# Controls when the workflow will run
on:
	push:
		branches: [main]

# 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-on-windows:
    # The type of runner that the job will run on
    runs-on: windows-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - uses: actions/checkout@v3

      - name: "windows에서 쉘 스크립트 실행"
        run: sh ./shell-commands/07-multi-job-windows.sh

	build-on-ubuntu:
    # 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:
      - uses: actions/checkout@v3

      - name: "ubuntu에서 쉘 스크립트 실행"
        run: sh ./shell-commands/07-multi-job-ubuntu.sh

여러 runner에게 서로 다른 작업을 맡길 수도 있다.

중요한 점은 각각의 job이 서로 다른 runner에 의해 병렬적으로 처리된다는 것이다.

배포 서버를 runner로 사용하기

  1. 레포지토리 > Settings > Actions > Runners > Create self-hosted runner

  2. 배포 서버의 architecture 명시

    image

  3. 배포 서버에서 runner application 설치

    image

Artifacts

artifact 라는 형태로 데이터를 Github에 upload, download 하면서 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
    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
    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

needs 커맨드로 job이 순차적으로 이루어지도록 설정한 점을 주목하자.

image

References

[1] 생활코딩 Github actions 강의

[2] Master Github actions Tutorial

[3] 공식 문서 - adding-self-hosted runners

[4] 공식 문서 - sharing data between job

[5] 공식 문서 - storing workflow data as artifacts

📕 메인

👨🏻‍💻 팀 규칙

🛠 프로젝트 명세

👨‍🏫 멘토님 미팅

📝 회의록

1주차 회의록
2주차 회의록
3주차 회의록
4주차 회의록
5주차 회의록
6주차 회의록

📅 스프린트 계획

🔙 회고록

피어세션

2주차 피어세션
3주차 피어세션
4주차 피어세션
5주차 피어세션

💻 기술적 경험

Clone this wiki locally