Swift 빌드 및 테스트

Swift 빌드 및 테스트

Swift 프로젝트를 자동으로 빌드하고 테스트하는 지속적 통합(CI) 워크플로우를 만드는 방법을 알려드릴게요. 이 가이드를 따라 하면 Swift 패키지를 직접 빌드하고 테스트할 수 있어요.

출처: 문서

본문

Introduction

이 가이드에서는 Swift 패키지를 빌드하고 테스트하는 방법을 보여드릴게요.

GitHub에서 호스팅하는 러너에는 사전 설치된 소프트웨어가 담긴 도구 캐시(tools cache)가 있고, Ubuntu 및 macOS 러너에는 Swift 패키지를 빌드하는 데 필요한 의존성도 포함되어 있답니다. 최신 소프트웨어 전체 목록과 사전 설치된 Swift 및 Xcode 버전을 보려면 GitHub-hosted runners 문서를 확인해 주세요.

Prerequisites

YAML 문법과 GitHub Actions에서 YAML을 사용하는 방법을 이미 알고 계신다면 좋아요. 자세한 내용은 Workflow syntax for GitHub Actions 문서를 참고하세요.

Swift 패키지에 대한 기본적인 이해가 있으면 더 좋아요. Apple 개발자 문서의 Swift Packages 문서를 참고하세요.

Using a Swift workflow template

빠르게 시작하고 싶다면, 저장소의 .github/workflows 디렉터리에 워크플로우 템플릿을 추가해 보세요.

GitHub는 대부분의 Swift 프로젝트에 맞게 동작하는 Swift 워크플로우 템플릿을 제공해요. 이 가이드의 뒷부분에서 이 워크플로우 템플릿을 커스터마이즈하는 방법을 예시와 함께 보여드릴게요.

  1. GitHub에서 저장소의 메인 페이지로 이동하세요.

  2. 저장소 이름 아래에서 Actions을 클릭하세요.

    Screenshot of the tabs for the "github/docs" repository. The "Actions" tab is highlighted with an orange outline.

  3. 저장소에 워크플로우가 이미 있다면 New workflow를 클릭하세요.

  4. "Choose a workflow" 페이지에는 추천 워크플로우 템플릿 목록이 표시돼요. "swift"를 검색해 보세요.

  5. Continuous integration을 클릭해서 워크플로우 목록을 필터링하세요.

  6. "Swift" 워크플로우에서 Configure를 클릭하세요.

  7. 필요에 따라 워크플로우를 수정하세요. 예를 들어 워크플로우가 실행될 브랜치를 변경할 수 있어요.

  8. Commit changes를 클릭하세요.

    그러면 swift.yml 워크플로우 파일이 저장소의 .github/workflows 디렉터리에 추가돼요.

Specifying a Swift version

GitHub에서 호스팅하는 러너에서 특정 사전 설치된 Swift 버전을 사용하려면 swift-actions/setup-swift 액션을 사용하세요. 이 액션은 러너의 도구 캐시에서 특정 Swift 버전을 찾아 필요한 바이너리를 PATH에 추가해요. 이러한 변경 사항은 job의 나머지 기간 동안 유지된답니다. 자세한 내용은 swift-actions/setup-swift 액션 문서를 참고하세요.

자체 호스팅 러너를 사용한다면, 원하는 Swift 버전을 직접 설치해서 PATH에 추가해야 해요.

아래 예시들은 swift-actions/setup-swift 액션을 사용하는 방법을 보여줍니다.

Using multiple Swift versions

job을 매트릭스(matrix)로 구성해서 여러 Swift 버전을 사용할 수 있어요.


# 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: Swift

on: [push]

jobs:
  build:
    name: Swift ${{ matrix.swift }} on ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        swift: ["5.2", "5.3"]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: swift-actions/setup-swift@65540b95f51493d65f5e59e97dcef9629ddf11bf
        with:
          swift-version: ${{ matrix.swift }}
      - uses: actions/checkout@v6
      - name: Build
        run: swift build
      - name: Run tests
        run: swift test

Using a single specific Swift version

job을 구성해서 5.3.3 같은 특정 Swift 버전 하나만 사용하도록 할 수 있어요.

# 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.
steps:
  - uses: swift-actions/setup-swift@65540b95f51493d65f5e59e97dcef9629ddf11bf
    with:
      swift-version: "5.3.3"
  - name: Get swift version
    run: swift --version # Swift 5.3.3

Building and testing your code

Swift로 로컬에서 코드를 빌드하고 테스트할 때 쓰는 명령을 그대로 사용할 수 있어요. 다음 예시는 한 job에서 swift buildswift test를 사용하는 방법을 보여줍니다:

# 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.
steps:
  - uses: actions/checkout@v6
  - uses: swift-actions/setup-swift@65540b95f51493d65f5e59e97dcef9629ddf11bf
    with:
      swift-version: "5.3.3"
  - name: Build
    run: swift build
  - name: Run tests
    run: swift test

더 알아보기 (Learn more)