简介
本指南介绍如何构建、测试和发布 Go 包。
必须在自托管运行程序上安装所需的软件。 有关自托管运行程序的详细信息,请参阅“托管您自己的运行器”。
先决条件
您应该已经熟悉 YAML 语法及其如何与 GitHub Actions 结合使用。 有关详细信息,请参阅“GitHub Actions 的工作流语法”。
建议你对 Go 语言有基本的了解。 有关详细信息,请参阅 Go 入门。
使用 Go 入门工作流
要快速学会使用,请将入门工作流添加到存储库的 .github/workflows
目录。
GitHub 提供了一个适用于大多数 Go 项目的 Go 入门工作流程。 本指南的后续部分提供了如何自定义入门工作流的示例。
-
在 你的企业 上,导航到存储库的主页。
-
在存储库名称下,单击 “操作”。
-
如果存储库中已有工作流,请单击“新建工作流”。
-
“选择工作流”页面显示了一系列推荐的入门工作流。 搜索“go”。
-
单击“持续集成”**** 以筛选工作流选择。
-
在“Go - by GitHub Actions”工作流上,单击 “配置”****。
如果未找到“Go - by GitHub Actions”入门工作流,请将以下工作流代码复制到存储库的.github/workflows
目录中名为go.yml
的新文件。YAML name: Go on: push: branches: [ "main" ] pull_request: branches: [ "main" ] jobs: build: runs-on: self-hosted steps: - uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v3 with: go-version: '1.20' - name: Build run: go build -v ./... - name: Test run: go test -v ./...
name: Go on: push: branches: [ "main" ] pull_request: branches: [ "main" ] jobs: build: runs-on: self-hosted steps: - uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v3 with: go-version: '1.20' - name: Build run: go build -v ./... - name: Test run: go test -v ./...
-
根据需要编辑工作流。 例如更改 Go 版本。
-
单击“提交更改”。
指定 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.19', '1.20', '1.21.x' ] steps: - uses: actions/checkout@v4 - name: Setup Go ${{ matrix.go-version }} uses: actions/setup-go@v3 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.19', '1.20', '1.21.x' ]
steps:
- uses: actions/checkout@v4
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v3
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 版本
可以将作业配置为使用 Go 的特定版本,例如 1.20.8
。 或者,您也可以使用语义版本语法来获得最新的次要版本。 此示例使用了 Go 1.21 的最新修补版本。
- name: Setup Go 1.21.x uses: actions/setup-go@v3 with: # Semantic version range syntax or exact version of Go go-version: '1.21.x'
- name: Setup Go 1.21.x
uses: actions/setup-go@v3
with:
# Semantic version range syntax or exact version of Go
go-version: '1.21.x'
安装依赖关系
可以使用 go get
安装依赖项:
steps: - uses: actions/checkout@v4 - name: Setup Go uses: actions/setup-go@v3 with: go-version: '1.21.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@v3
with:
go-version: '1.21.x'
- name: Install dependencies
run: |
go get .
go get example.com/octo-examplemodule
go get example.com/octo-examplemodule@v1.3.4
构建和测试代码
您可以使用与本地相同的命令来构建和测试代码。 此示例工作流演示如何在作业中使用 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@v3 with: go-version: '1.21.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@v3
with:
go-version: '1.21.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.19', '1.20', '1.21.x' ] steps: - uses: actions/checkout@v4 - name: Setup Go uses: actions/setup-go@v3 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.19', '1.20', '1.21.x' ]
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v3
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