为什么可以在 CI 环境中运行 Go 的测试和构建而无需先安装依赖项呢?

huangapple go评论89阅读模式
英文:

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-gocheckout,它仍然可以工作。

英文:

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.

huangapple
  • 本文由 发表于 2023年1月29日 17:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75273993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定