Travis CI에서 GitHub Actions로 마이그레이션하기

Travis CI에서 GitHub Actions로 마이그레이션하기

GitHub Actions와 Travis CI는 구성상 몇 가지 유사점을 공유하기 때문에 GitHub Actions로의 마이그레이션이 비교적 간단할 수 있어요. 이 가이드에서는 두 시스템의 차이점과 마이그레이션 시 유의할 점을 알려드릴게요.

출처: 문서

본문

Introduction

Travis CI와 GitHub Actions는 모두 코드를 자동으로 빌드, 테스트, 게시, 릴리스, 배포하는 워크플로우를 만들 수 있게 해 줘요. Travis CI와 GitHub Actions는 워크플로우 구성에서 몇 가지 유사점을 공유해요.

  • 워크플로우 구성 파일은 YAML로 작성되고 저장소에 저장돼요.
  • 워크플로우에는 하나 이상의 job이 포함돼요.
  • job에는 하나 이상의 단계 또는 개별 명령이 포함돼요.
  • 단계나 작업은 재사용되고 커뮤니티와 공유될 수 있어요.

자세한 내용은 Understanding GitHub Actions 문서를 참고하세요.

Key differences

Travis CI에서 마이그레이션할 때 다음 차이점을 고려하세요.

  • Travis CI는 dist 키를 사용해서 xenial, bionic 같은 Linux 배포판을 지정할 수 있게 해 줘요. GitHub Actions에서 워크플로우는 러너에 의해 운영 체제가 결정되는 자체 호스팅 또는 GitHub 호스팅 러너에서 실행돼요.
  • GitHub Actions에서는 워크플로우가 저장소의 .github/workflows 디렉터리에 저장된 별도의 YAML 파일로 정의돼요. Travis CI는 저장소 루트에 있는 하나의 .travis.yml 파일을 사용해요.
  • GitHub Actions에서는 워크플로우가 저장소의 .github/workflows 디렉터리에 저장된 별도의 YAML 파일로 정의돼요. Travis CI는 저장소 루트에 있는 하나의 .travis.yml 파일을 사용해요.

Migrating workflows and jobs

Travis CI와 GitHub Actions 모두 구성 파일에서 jobs 또는 matrix를 유사한 문법으로 구성해요. Travi CI의 매트릭스에서 사용하는 이중 매트릭스를 만들려면 두 매트릭스를 결합해야 해요.

다음은 두 시스템의 문법을 보여주는 예시예요. 아래 예시는 Python 버전에 따라 다른 실행을 보여줍니다. GitHub Actions에서는 매트릭스를 사용해서 Python 버전을 지정할 수 있어요. GitHub Actions 워크플로우에는 기본적으로 저장소 체크아웃이 포함되지 않으므로 actions/checkout 액션을 사용해야 해요.

Travis CI syntax for running jobs on different Python versions

language: python
python:
  - "2.7"
  - "3.7"

script:
  - python -m pytest

GitHub Actions syntax for running jobs on different Python versions

name: Testing

on: push

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['2.7', '3.7']
    steps:
      - uses: actions/checkout@v6
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Run tests
        run: python -m pytest

Migrating language versions

Travis CI에서 언어 버전은 YAML 구성의 최상위 키로 지정돼요. GitHub Actions에서는 setup-* 액션을 사용해서 언어 버전을 지정할 수 있어요. 예를 들어 Python을 사용한다면 actions/setup-python을 사용할 수 있어요. 자세한 내용은 Reusing automations 문서를 참고하세요.

예를 들어, Travis CI에서 Node.js 애플리케이션을 테스트하고 싶다면 다음 구성을 사용할 수 있어요.

Travis CI syntax for testing a Node.js application

language: node_js
node_js:
  - "10"
  - "12"

script:
  - npm ci
  - npm test

GitHub Actions syntax for testing a Node.js application

name: Testing

on: push

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: ['10', '12']
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Node
        uses: actions/setup-node@v7
        with:
          node-version: ${{ matrix.node-version }}

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test

Migrating script and command steps

Travis CI의 script 키는 실행할 명령을 정의해요. GitHub Actions에서는 run 키를 사용해서 동일한 작업을 수행할 수 있어요.

Travis CI 구조는 다음과 같아요.

script:
  - npm ci
  - npm test

GitHub Actions에서 run 키 하나로 여러 명령을 결합할 수 있어요.

- name: Install dependencies
  run: npm ci
- name: Run tests
  run: npm test

Travis CI에서는 before_install, before_script, after_script, after_success 같은 키를 사용해서 스크립트 실행 주위에서 명령을 실행할 수 있어요. GitHub Actions에서는 일반적으로 run 단계 여러 개를 명명된 단계로 사용해서 이 동작을 재현할 수 있어요. 예를 들어 before_script 단계를 만든 다음 테스트 실행 단계를 만들 수 있어요.

Travis CI 구조는 다음과 같아요.

language: node_js
node_js:
  - "10"
  - "12"

before_script:
  - echo "Testing before script"

script:
  - npm test

after_script:
  - echo "Testing after script"

GitHub Actions 워크플로우 파일은 저장소의 .github/workflows 디렉터리에 저장됩니다.

name: Testing

on: push

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: ['10', '12']
    steps:
    - name: Checkout repository
      uses: actions/checkout@v6

    - name: Setup Node
      uses: actions/setup-node@v7
      with:
        node-version: ${{ matrix.node-version }}

    - name: Before script
      run: |
        echo "Testing before script"

    - name: Run tests
      run: npm test

    - name: After script
      run: |
        echo "Testing after script"

Migrating an after_success step

Travis CI에서 배포는 after_success 키를 사용해서 수행될 수 있어요. GitHub Actions에서는 배포에 after_success 같은 키를 사용할 수 없어요. 대신 작업을 분리해서 별도의 job으로 만들어야 해요. 예를 들어 테스트 job이 성공한 후에 실행되는 별도의 배포 job을 만들 수 있어요.

Migrating conditionals

Travis CI는 조건을 사용해서 단계가 실행되는 시기를 제어할 수 있어요. GitHub Actions는 if 키를 사용해서 이 동작을 구현해요.

Migrating install and before_install commands

Travis CI의 install 단계는 의존성을 설치하는 데 사용돼요. GitHub Actions에서는 워크플로우가 명령을 실행하기 전에 리포지토리를 마운트할 필요가 없어서, 의존성 설치에 대한 특별한 단계가 따로 필요하지 않아요. 대신 actions/setup-* 액션과 npm ci 같은 명령을 사용해서 워크플로우에서 의존성을 설치할 수 있어요.

예를 들어 Python 프로젝트가 있다면 pip install을 사용할 거예요. Travis CI 구조는 다음과 같아요.

language: python
python:
  - "3.7"

install:
  - pip install -r requirements.txt

script:
  - pytest

GitHub Actions에서는 run 키를 사용해서 pip install을 수행할 수 있어요. GitHub Actions 워크플로우 파일은 저장소의 .github/workflows 디렉터리에 저장됩니다.

name: Testing

on: push

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.7']
    steps:
    - name: Checkout repository
      uses: actions/checkout@v6

    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}

    - name: Install dependencies
      run: pip install -r requirements.txt

    - name: Run tests
      run: pytest

Migrating cache commands

Travis CI에서 cache 키를 사용해서 프레임워크별 기본 디렉터리의 의존성을 캐시할 수 있어요. GitHub Actions에서는 actions/cache 액션을 사용해서 워크플로우 실행 간 캐시할 경로를 지정할 수 있어요.

Travis CI 구조는 다음과 같아요.

language: node_js
node_js:
  - "10"
  - "12"

cache: npm

script:
  - npm ci
  - npm test

GitHub Actions에서 actions/cache를 사용하면 되고, 캐시 키를 제공해야 해요. GitHub Actions 워크플로우 파일은 저장소의 .github/workflows 디렉터리에 저장됩니다.

name: Testing

on: push

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: ['10', '12']
    steps:
    - name: Setup Node
      uses: actions/setup-node@v7
      with:
        node-version: ${{ matrix.node-version }}

    - name: Install dependencies
      run: npm ci

    - name: Cache dependencies
      uses: actions/cache@v4
      with:
        path: ~/.npm
        key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-node-

    - name: Run tests
      run: npm test

Migrating services containers

Travis CI에서 services 키를 사용해서 애플리케이션에 대한 지원 컨테이너를 지정할 수 있어요. GitHub Actions에서 이 동작은 서비스 컨테이너로 구현돼요.

자세한 내용은 Communicating with Docker service containers 문서를 참고하세요.

Complete Example

다음은 실제 세계의 예시예요. 왼쪽은 thoughtbot/administrator 리포지토리의 실제 Travis CI 구성이고, 오른쪽은 GitHub Actions 워크플로우 등가물이에요.

Complete example for Travis CI

language: ruby
cache: bundler
before_install:
  - gem install bundler
before_script:
  - cp .sample.env .env
script:
  - bundle exec rake
services:
  - postgresql

Complete example for GitHub Actions

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Ruby

on:
  push:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        ruby-version: ['3.0']
    services:
      postgres:
        image: postgres:13
        env:
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - uses: actions/checkout@v6
      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{ matrix.ruby-version }}
          bundler: true
      - name: Install dependencies
        run: bundle install
      - name: Setup environment configuration
        run: cp .sample.env .env
      - name: Run tests
        run: bundle exec rake

더 알아보기 (Learn more)