英文:
Why is it possible to run Go tests and builds inside CI environments without having to install the dependencies first?
问题
我有一个带有 Makefile 的 Go 项目:
test:
@go test -cover ./...
还有一个 mod 文件:
module path/to/repo
go 1.19
require github.com/go-chi/chi/v5 v5.0.8
我创建了一个 Github action 示例,在 Github 的 PR 上运行测试:
name: QA on pull request
on: pull_request
jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Run tests
run: make test
我想知道为什么这个工作流程在没有 install dependencies
步骤的情况下能正常工作。项目本身使用了外部依赖,我认为应该有一个步骤运行 go get ./...
。
如果不存在,Go 会在幕后安装它们吗?还是 actions/setup-go@v3
动作会安装依赖项?
英文:
I have a Go project with a Makefile
test:
@go test -cover ./...
and a mod file
module path/to/repo
go 1.19
require github.com/go-chi/chi/v5 v5.0.8
I created a Github action sample to run tests on a Github PR
name: QA on pull request
on: pull_request
jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Run tests
run: make test
I would like to know why this workflow is working without a install dependencies
step. The project itself is using external dependencies and I think there should be a step that runs go get ./...
Does Go install them under the hood if not present? Or does the action actions/setup-go@v3
install the dependencies?
答案1
得分: 3
根据go test
文档(或者你可以在本地运行go help test
来阅读其描述):
> 'Go test'会重新编译每个包以及与文件模式"*_test.go"匹配的任何文件。
它还会安装所有的依赖项;所以,当执行go test
时会发生这种情况。你可以在日志中观察到这一点。
actions/setup-go@v3
不依赖于代码本身。它只是设置你所要求的go
环境。在你的设置中,如果你交换了setup-go
和checkout
,它仍然可以工作。
英文:
According to go test
docs (or you may run go help test
locally to read its description):
> 'Go test' recompiles each package along with any files with names matching the file pattern "*_test.go".
It also installs all the dependencies; so, it happens when the action does go test
. You can probably observe it in the logs.
actions/setup-go@v3
doesn't depend on the code itself. It just sets up the go
environment you ask for. In your setup, if you swap the setup-go
and checkout
, it still works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论