GitHub Actions, Self-hosted runner 구성 가이드

이 포스트는 GitHub의 리포지토리와 연동해 CI/CD 파이프라인을 구성할 수 있는 GitHub Actions 의 Self-hosted runner 구성 방법을 설명합니다. VM에 Self-hosted runner를 구성하여, GitHub Actions에서 workflow를 실행 시 해당 VM을 통해 작업이 수행되도록 할 수 있습니다.

목차

1. GitHub Actions란?
 
1-1. Self-hosted runner 란?
2. GitHub Actions Self-hosted runner 구성
 2-1. 테스트 workflow
3. 결론

1. GitHub Actions 란?

GitHub Actions는 빌드, 테스트 및 배포 파이프라인을 자동화할 수 있는 CI/CD(Continuous Integration and Continuous Delivery/Deployment) 플랫폼입니다. GitHub Actions는 리포지토리의 파일 변경에 따라 workflow를 트리거하고, workflow에 여러 작업을 정의하여 실행하는 방식으로 작동합니다.

1-1. Self-hosted runner 란?

GitHub Actions Runner는 GitHub Actions의 workflow 작업을 실행하는 애플리케이션입니다.
Self-hosted runner는 GitHub에서 제공하는 GitHub-hosted runners와 다르게 하드웨어, OS, 소프트웨어에 더 많은 선택권을 제공합니다. 기존에 이미 사용하고 있는 서버/클라우드 자원을 활용하거나, 내부 네트워크 리소스에 접근하는 등 사용자 환경에 맞춰 구성할 수 있는 것이 장점입니다.
Self-hosted runners는 물리, 가상, 컨테이너, 온프레미스, 클라우드 환경에 모두 구성될 수 있습니다.

Self-hosted runner 구성에는 다음 OS가 지원됩니다.

Linux
  • Red Hat Enterprise Linux 8 이상 버전
  • CentOS 8 이상 버전
  • Oracle Linux 8 이상 버전
  • Fedora 29 이상 버전
  • Debian 10 이상 버전
  • Ubuntu 20.04 이상 버전
  • Linux Mint 20 이상 버전
  • openSUSE 15.2 이상 버전
  • SUSE Enterprise Linux (SLES) 15 SP2 이상 버전
Windows
  • Windows 10 64-bit
  • Windows 11 64-bit
  • Windows Server 2016 64-bit
  • Windows Server 2019 64-bit
  • Windows Server 2022 64-bit
macOS
  • macOS 11.0 (Big Sur) 이상 버전

2. GitHub Actions Self-hosted runner 구성

GitHub의 GitHub Actions를 사용할 리포지토리의 설정 메뉴로 이동합니다.

GitHub Actions Settings

좌측의 Actions 메뉴 하위의 Runners 메뉴를 클릭하여, New self-hosted runner 버튼을 통해 GitHub Actions Self-hosted runner 추가를 위한 절차를 확인할 수 있습니다.

GitHub Actions adding Self-hosted runner

OS별 Self-hosted runner 구성 가이드를 확인할 수 있습니다.
이 포스트에서는 Ubuntu 22.04 VM에 구성하기 위해 Linux 가이드로 진행했습니다. 모든 과정은 root 사용자가 아닌 사용자로 진행합니다.

GitHub Actions adding Self-hosted runner on Linux

1. GitHub Actions Self-hosted runner를 구성할 디렉터리를 생성합니다.

$ mkdir actions-runner && cd actions-runner

2. 구성 파일을 다운로드합니다. 최신 버전은 포스트의 명령어와 버전이 다를 수 있습니다.

$ curl -o actions-runner-linux-x64-2.320.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.320.0/actions-runner-linux-x64-2.320.0.tar.gz

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  208M  100  208M    0     0  7164k      0  0:00:29  0:00:29 --:--:-- 8876k

(선택) 해시 검증을 통해 파일의 무결성을 확인합니다

$ echo "93ac1b7ce743ee85b5d386f5c1787385ef07b3d7c728ff66ce0d3813d5f46900  actions-runner-linux-x64-2.320.0.tar.gz" | shasum -a 256 -c

actions-runner-linux-x64-2.320.0.tar.gz: OK

3. 다운받은 파일의 압축을 해제합니다.

$ tar xzf ./actions-runner-linux-x64-2.320.0.tar.gz

$ ls
actions-runner-linux-x64-2.320.0.tar.gz  bin  config.sh  env.sh  externals  run-helper.cmd.template  run-helper.sh.template  run.sh  safe_sleep.sh

4. 스크립트를 통해 runner를 설정합니다. GitHub 리포지토리 url과 토큰을 통해 설정됩니다.

$ ./config.sh --url https://github.com/0jsong/Git-NGINX --token *********
--------------------------------------------------------------------------------
|        ____ _ _   _   _       _          _        _   _                      |
|       / ___(_) |_| | | |_   _| |__      / \   ___| |_(_) ___  _ __  ___      |
|      | |  _| | __| |_| | | | | '_ \    / _ \ / __| __| |/ _ \| '_ \/ __|     |
|      | |_| | | |_|  _  | |_| | |_) |  / ___ \ (__| |_| | (_) | | | \__ \     |
|       \____|_|\__|_| |_|\__,_|_.__/  /_/   \_\___|\__|_|\___/|_| |_|___/     |
|                                                                              |
|                       Self-hosted runner registration                        |
|                                                                              |
--------------------------------------------------------------------------------

# Authentication


√ Connected to GitHub

# Runner Registration

# runner 그룹 설정
Enter the name of the runner group to add this runner to: [press Enter for Default] 

# runner 이름 설정 - host 이름값을 기본으로 사용
Enter the name of runner: [press Enter for runner] nginx

# 레이블 설정 - workflow 실행 시 레이블로 runner 설정
This runner will have the following labels: 'self-hosted', 'Linux', 'X64' 
Enter any additional labels (ex. label-1,label-2): [press Enter to skip] nginx

√ Runner successfully added
√ Runner connection is good

# Runner settings

# 작업 디렉터리 설정
Enter name of work folder: [press Enter for _work] 

√ Settings Saved.

위 과정까지 진행 시, Runners에 Self-hosted runner가 지정한 이름으로 추가된 것을 확인할 수 있습니다.

GitHub Actions Self-hosted runner

run 스크립트를 사용하여 runner를 실행합니다.

$ ./run.sh

√ Connected to GitHub

Current runner version: '2.320.0'
2024-11-14 05:12:27Z: Listening for Jobs
GitHub Actions adding Self-hosted runner ready

Runner의 상태가 대기 상태인 Idle로 변경됩니다.

2-1. 테스트 workflow

구성한 Self-hosted runner를 통한 workflow 실행을 확인하기 위해 테스트를 진행합니다.

1. Actions 탭으로 이동하여 SImple workflow를 선택합니다.

2. 테스트 yml 파일을 확인합니다.
구성한 Self-hosted runner를 통해 workflow를 실행하기 위해, runs-on 항목에 runner의 태그를 지정합니다.

# workflow 이름

name: CI

# workflow가 실행될 조건 설정
on:
  # main 브랜치에 push/pull 발생 시 workflow 실행
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

  # Actions 탭에서 workflow를 수동으로 실행할 수 있도록 설정
  workflow_dispatch:

# workflow는 순차적/병렬로 실행할 수 있는 하나 이상의 job으로 구성됨
jobs:
  # 이 workflow는 "build" 이름의 job 하나로 구성됨
  build:
    # job이 실행될 runner 설정. runner의 태그를 통해 지정 가능
    runs-on: self-hosted

    # Steps는 job의 일부로 실행될 일련의 작업을 나타냄
    steps:
      # workflow를 트리거한 리포지토리를 체크아웃
      - uses: actions/checkout@v4

      # 한 줄 명령어 실행
      - name: Run a one-line script
        run: echo Hello, world!

      # | 사용으로 여러 줄의 명령어 실행 
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.

3. Commit changes 버튼을 통해 workflow 파일을 저장하고 실행합니다.

commit workflow for GitHub Actions self-hosted runner

리포지토리의 지정된 디렉터리에 파일이 생성된 것을 확인할 수 있습니다.

4. runner VM의 터미널에서 실행을 확인합니다.

√ Connected to GitHub

Current runner version: '2.320.0'
2024-11-18 02:38:15Z: Listening for Jobs
2024-11-18 02:39:17Z: Running job: build
2024-11-18 02:39:33Z: Job build completed with result: Succeeded

위와 같은 출력을 확인할 수 있습니다.

GitHub의 Actions 탭에서 workflow의 완료 및 worflow의 job 로그를 확인할 수 있습니다.

3. 결론

이번 포스트에서는 GitHub의 리포지토리와 연동해 CI/CD 파이프라인을 구성하기 위한 준비로 workflow를 실행할 GitHub Actions의 Self-hosted runner를 구성하는 방법에 대해 알아봤습니다.

GitHub에서 제공하는 runner 구성 가이드를 통해 간단하게 Self-hosted runner의 구성을 확인하고, 테스트 workflow를 실행하여 구성한 runner를 통해서 실행되는 것을 확인했습니다.

다음 포스트에서는 GitHub Actions를 활용하여 NGINX의 설정 파일을 GitHub 리포지토리를 통해 관리하는 방법에 대해 알아보겠습니다.

GitHub Actions로 NGINX Plus의 버전 관리 및 배포를 체험해 보고 싶으시다면 NGINX STORE를 통해 문의해 무료로 NGINX Plus trial을 체험해 보세요.

NGINX STORE를 통한 솔루션 도입 및 기술지원 무료 상담 신청

* indicates required