Danawa Tech Blog

Github Actions에 Self-hosted Runner 등록하기

thumbnail

Github Actions에 Self-hosted Runner 등록하기

소개

Self-hosted Runner는 Github Actions에서 사용자가 직접 지정하는 로컬 컴퓨팅 자원을 사용하여 빌드를 수행하는 기능입니다. 이 기능은 배포 작업이 많아 배포가 지체되거나 서버 비용이 부담되는 경우 유용합니다. MacOS를 기준으로 예를 들어보겠습니다. 쏙 프로젝트에서는 macOS 기반의 Github-Hosted Runner의 비용이 부담되어, iOS 앱을 배포하는 목적으로 맥북을 Self-hosted Runner로 등록하여 사용하였습니다.

설정 방법

Github Actions을 사용하고자 하는 저장소에서 Settings - Actions - Runners로 이동하여 로컬 머신에 해당되는 OS를 선택하면 OS 별로 설정하는 방법이 Download와 Configure 란에 설명되어 있습니다. MacOS를 기준으로 설정 방법을 설명하겠습니다.

  1. Self-hosted Runner로 등록하고자 하는 기기에서 폴더 생성
  2. 가장 최신의 Runner 패키지 다운로드
  3. 다운로드 받은 압축 파일 압축 해제
  4. 저장소 연결: Github가 로컬 머신에 직접 접속하는 것이 아니라 로컬 머신이 Github 저장소로 접속하는 방식이기 때문에, Github 저장소 주소와 액세스 토큰을 설정해야 합니다.

액세스 토큰은 개인 계정에서 Settings - Developer settings - Personal access tokens - Generate new token에서 발급받을 수 있습니다. 적절한 권한(admin:enterprise - manage_runners :enterprise)을 선택하여 토큰을 발급받아야 합니다.

사용 방법

Self-hosted Runner를 활성화시키기 위해서는 해당 로컬 기기의 actions-runner 폴더에서 run.sh 프로그램을 실행해야 합니다. 이제 workflows 디렉토리 안에 actions 사용시 해당 self-hosted runner를 통해 빌드되도록 yaml 파일을 만들어 주면 됩니다. 이 yaml 파일의 내용은 다음과 같습니다.

on:
  push:
    branches:
      - develop
      # if there is a commit message that contains "#all" or "#ios"
      # or a commit message with "[all]" or "[ios]"
      # or any combination of the above, then run the job
      # only use this runner if it's specified as macOS
      if: github.event.head_commit.message =~ /#all|#ios|\[all\]|\[ios\]/ && runner.os == 'macOS'
  pull_request:
    branches:
      - develop
    # if there is a commit message that contains "#all" or "#ios"
    # or a commit message with "[all]" or "[ios]"
    # or any combination of the above, then run the job
    # only use this runner if it's specified as macOS
    if: github.event.pull_request.head.sha == github.event.pull_request.base.sha && github.event.head_commit.message =~ /#all|#ios|\[all\]|\[ios\]/ && runner.os == 'macOS'
jobs:
  build:
    runs-on: [self-hosted, macOS]
    steps:
      - uses: actions/checkout@v2
      - name: Sync Files
        run: rsync -av --delete ./ /Users/runner/work/myproject/myproject/
      - name: Build 
        run: fastlane deploy

yaml 파일은 develop 브랜치에 push나 pull request하는 경우에 해당 macOS로 등록된 self-hosted runner를 사용하도록 설정한 것입니다. 만약에 등록된 self-hosted runner가 많을 경우 특징들을 조합해서 지정할 수 있습니다.

정리

Self-hosted Runner를 사용하면 Github-hosted Runner와 달리 사용 비용이 전혀 없기 때문에 배포작업이 많은 경우 유용한 기능입니다. 배포하는 상황에 따라서 적절히 로컬 머신을 연결하여 배포용 서버로 사용하면 좋을 것 같습니다.

참고 자료

  • https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners
  • https://mildwhale.github.io/2021-04-24-build-machine-with-m1-macmini/
  • https://www.hahwul.com/2022/07/05/macos-github-action-runner/