はじめに
このガイドは、Go パッケージのビルド、テスト、公開の方法を紹介します。
GitHub ホスト ランナーには、ツール キャッシュとプレインストールされたソフトウェアがあり、それには Go の依存関係が含まれます。 最新のソフトウェアの完全な一覧と、プレインストールされたバージョンの Go については、「Using GitHub-hosted runners」を参照してください。
前提条件
YAMLの構文と、GitHub ActionsでのYAMLの使われ方に馴染んでいる必要があります。 詳しくは、「GitHub Actions のワークフロー構文」を参照してください。
Go 言語の基本を理解しておくことをおすすめします。 詳しくは、「Go の概要」をご覧ください。
Go スターター ワークフローの使用
GitHub では、ほとんどの Go プロジェクトで使える Go スターター ワークフローが提供されています。 このガイドには、スターター ワークフローのカスタマイズに使用できる例が含まれます。 詳細については、「Go スターター ワークフロー」を参照してください。
すぐに作業を開始するには、リポジトリの .github/workflows
ディレクトリにスターター ワークフローを追加します。
name: Go package on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v4 with: go-version: '1.15' - name: Build run: go build -v ./... - name: Test run: go test -v ./...
name: Go package
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.15'
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
Go バージョンの指定
最も簡単に Go のバージョンを指定する方法は、GitHub によって提供される setup-go
アクションを使用することです。 詳細については、「setup-go
アクション」を参照してください。
GitHub ホストランナーでプレインストールされたバージョンの Go を使うには、setup-go
アクションの go-version
プロパティに関連するバージョンを渡します。 このアクションは、各ランナーのツール キャッシュから特定のバージョンの Go を見つけて、必要なバイナリを PATH
に追加します。 これらの変更は、ジョブの残りの部分で保持されます。
setup-go
アクションは、異なるランナーや異なるバージョンの Go で一貫した動作を保証するのに役立つため、GitHub Actions で Go を使うときに推奨される方法です。 セルフホスト型ランナーを使用している場合は、Go をインストールし、それを PATH
に追加する必要があります。
複数のバージョンの Go の使用
name: Go on: [push] jobs: build: runs-on: ubuntu-latest strategy: matrix: go-version: [ '1.14', '1.15', '1.16.x' ] steps: - uses: actions/checkout@v4 - name: Setup Go ${{ matrix.go-version }} uses: actions/setup-go@v4 with: go-version: ${{ matrix.go-version }} # You can test your matrix by printing the current Go version - name: Display Go version run: go version
name: Go
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.14', '1.15', '1.16.x' ]
steps:
- uses: actions/checkout@v4
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
# You can test your matrix by printing the current Go version
- name: Display Go version
run: go version
特定のバージョンの Go の使用
1.16.2
のような特定のバージョンの Go を使うようにジョブを構成できます。 あるいは、最新のマイナーリリースを取得するためにセマンティックバージョン構文を使うこともできます。 この例では、最新のパッチ リリースである Go 1.16 を使います。
- name: Setup Go 1.16.x uses: actions/setup-go@v4 with: # Semantic version range syntax or exact version of Go go-version: '1.16.x'
- name: Setup Go 1.16.x
uses: actions/setup-go@v4
with:
# Semantic version range syntax or exact version of Go
go-version: '1.16.x'
依存関係のインストール
go get
を使って依存関係をインストールできます。
steps: - uses: actions/checkout@v4 - name: Setup Go uses: actions/setup-go@v4 with: go-version: '1.16.x' - name: Install dependencies run: | go get . go get example.com/octo-examplemodule go get example.com/octo-examplemodule@v1.3.4
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.16.x'
- name: Install dependencies
run: |
go get .
go get example.com/octo-examplemodule
go get example.com/octo-examplemodule@v1.3.4
依存関係のキャッシング
setup-go
アクションを使うと、依存関係をキャッシュおよび復元できます。 既定では、キャッシュは setup-go
アクションを使うとき、有効になっています。
setup-go
アクションによってリポジトリ ルートで依存関係ファイル go.sum
が検索され、依存関係ファイルのハッシュをキャッシュ キーの一部として使います。
依存関係ファイルを複数使っている場合、またはそれらが別のサブディレクトリにある場合、cache-dependency-path
パラメーターを使うことができます。
- name: Setup Go - uses: actions/setup-go@v4 with: go-version: '1.17' cache-dependency-path: subdir/go.sum
- name: Setup Go
- uses: actions/setup-go@v4
with:
go-version: '1.17'
cache-dependency-path: subdir/go.sum
カスタム要件がある場合、またはキャッシュに対してより細かい制御が必要な場合は、cache
アクションを使用できます。 詳しくは、「依存関係をキャッシュしてワークフローのスピードを上げる」を参照してください。
コードのビルドとテスト
ローカルで使うのと同じコマンドを、コードのビルドとテストに使えます。 このワークフローの例では、ジョブで go build
と go test
を使う方法を示します。
name: Go on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup Go uses: actions/setup-go@v4 with: go-version: '1.16.x' - name: Install dependencies run: go get . - name: Build run: go build -v ./... - name: Test with the Go CLI run: go test
name: Go
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.16.x'
- name: Install dependencies
run: go get .
- name: Build
run: go build -v ./...
- name: Test with the Go CLI
run: go test
成果物としてのワークフローのデータのパッケージ化
ワークフローが完了すると、結果の成果物を分析のためにアップロードできます。 たとえば、ログファイル、コアダンプ、テスト結果、スクリーンショットを保存する必要があるかもしれません。 次の例では、upload-artifact
アクションを使ってテスト結果をアップロードする方法を示します。
詳しくは、「ワークフロー データを成果物として保存する」を参照してください。
name: Upload Go test results on: [push] jobs: build: runs-on: ubuntu-latest strategy: matrix: go-version: [ '1.14', '1.15', '1.16.x' ] steps: - uses: actions/checkout@v4 - name: Setup Go uses: actions/setup-go@v4 with: go-version: ${{ matrix.go-version }} - name: Install dependencies run: go get . - name: Test with Go run: go test -json > TestResults-${{ matrix.go-version }}.json - name: Upload Go test results uses: actions/upload-artifact@v3 with: name: Go-results-${{ matrix.go-version }} path: TestResults-${{ matrix.go-version }}.json
name: Upload Go test results
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.14', '1.15', '1.16.x' ]
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Install dependencies
run: go get .
- name: Test with Go
run: go test -json > TestResults-${{ matrix.go-version }}.json
- name: Upload Go test results
uses: actions/upload-artifact@v3
with:
name: Go-results-${{ matrix.go-version }}
path: TestResults-${{ matrix.go-version }}.json