-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.go
116 lines (101 loc) · 3.38 KB
/
go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package internal
import (
"fmt"
"github.com/CircleCI-Public/circleci-config/config"
"github.com/CircleCI-Public/circleci-config/labeling/labels"
)
const privateModInstructions = `echo "go mod download will fail if you have private repositories
One way to fix this for private go modules that are hosted in github:
1. Add a GITHUB_TOKEN and GITHUB_USER to an org context. Please refer to https://circleci.com/docs/contexts/ for more informaiton on how to use contexts.
2. Add a .circleci/config.yml to your repository or use this config.yml as a starting template
3. Configure the jobs to use the newly created context which includes GITHUB_TOKEN and GITHUB_USER
4. Before downloading the modules you will need to add a step to execute \"go env -w GOPRIVATE=github.com/<OrgNameHere>\".
This allows go mod to install private repos under OrgNameHere.
5. You will also need to run \"git config --global url.\"https://$GITHUB_USER:[email protected]/<OrgNameHere>/\".insteadOf \"https://github.com/<OrgNameHere>/\"\"
6. Finally include the \"go mod download\" it should be able to fetch your private libraries now.
For gitlab private go modules, follow the same instructions as above but include your GITLAB_TOKEN and GITLAB_USER.
Then use gitlab.com instead of github.com in steps 4 and 5.
See https://go.dev/ref/mod#private-modules for more details."`
func goInitialSteps(ls labels.LabelSet) []config.Step {
depsLabel := ls[labels.DepsGo]
steps := []config.Step{
checkoutStep(depsLabel),
}
if !depsLabel.HasLockFile {
return steps
}
const goCacheKey = `go-mod-{{ checksum "go.sum" }}`
return append(steps,
config.Step{
Type: config.RestoreCache,
CacheKey: goCacheKey,
},
config.Step{
Type: config.Run,
Name: "Download Go modules",
Command: "go mod download",
},
config.Step{
Type: config.Run,
Name: "Print go mod help instructions",
Command: privateModInstructions,
When: config.WhenTypeOnFail,
},
config.Step{
Type: config.SaveCache,
CacheKey: goCacheKey,
Path: "/home/circleci/go/pkg/mod",
},
)
}
func goTestJob(ls labels.LabelSet) *Job {
steps := goInitialSteps(ls)
steps = append(steps, []config.Step{{
Type: config.Run,
Name: "Run tests",
Command: "gotestsum --junitfile junit.xml",
}, {
Type: config.StoreTestResults,
Path: "junit.xml",
}}...)
return &Job{
Job: config.Job{
Name: "test-go",
Comment: "Install go modules and run tests",
DockerImages: []string{"cimg/go:1.20"},
WorkingDirectory: workingDirectory(ls[labels.DepsGo]),
Steps: steps,
},
Type: TestJob,
}
}
func goBuildJob(ls labels.LabelSet) *Job {
steps := goInitialSteps(ls)
steps = append(steps,
createArtifactsDirStep,
config.Step{
Type: config.Run,
Name: "Build executables",
Command: fmt.Sprintf("go build -o %s ./...", artifactsPath),
},
storeArtifactsStep("executables"))
return &Job{
Job: config.Job{
Name: "build-go-executables",
Comment: "Build go executables and store them as artifacts",
DockerImages: []string{"cimg/go:1.20"},
Steps: steps,
},
Type: ArtifactJob,
}
}
func GenerateGoJobs(ls labels.LabelSet) (jobs []*Job) {
if !ls[labels.DepsGo].Valid {
return nil
}
jobs = append(jobs, goTestJob(ls))
if ls[labels.ArtifactGoExecutable].Valid {
jobs = append(jobs, goBuildJob(ls))
}
return jobs
}