Home >Backend Development >Golang >Why can I run Go tests and builds in a CI environment without installing dependencies first?

Why can I run Go tests and builds in a CI environment without installing dependencies first?

WBOY
WBOYforward
2024-02-13 08:42:19657browse

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

The reason why you don’t need to install dependencies first when running Go tests and builds in a CI environment is because the CI (Continuous Integration) tool can automatically download and install the required ones on every build dependencies. The advantage of this is that it reduces the burden on developers and does not require manual maintenance of dependency installation and updates. At the same time, CI tools can also ensure that each build is performed in the same environment, avoiding build failures or inconsistent results due to inconsistent dependency versions. Therefore, when running Go tests and builds in a CI environment, development and testing work can be performed more conveniently and efficiently.

Question content

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 example to run tests on 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 want to know why this workflow works without the install dependency step. The project itself is using external dependencies and I think there should be a step to run go get ./...

If they do not exist, will go install them in the background? Or does the actions/setup-go@v3 action install dependencies?

Workaround

According to the go test documentation (or you can run go help test locally to read its description):

"Go test" recompiles every package and any file whose name matches the file pattern "*_test.go".

It also installs all dependencies; therefore, this happens when the action go test is executed. You may be able to observe it in the logs.

actions/setup-go@v3 Does not depend on the code itself. It just sets up the go environment you requested. In your setup, if you swap setup-go and checkout, it will still work.

The above is the detailed content of Why can I run Go tests and builds in a CI environment without installing dependencies first?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete