Skip to content

Commit 925fb1e

Browse files
authored
Consolidate Go installation step (microsoft#2244)
Create composite action to call `actions/setup-go` with common values and logic across different jobs and workflows to reduce duplication and make sure workflows all use the same Go version. Specifically, the action defaults to `oldstable` for the Go version, uses both `go.sum` and `test/go.sum` for the cache dependency, and allows pre-filling the Go module cache after installing Go. It exposes the same outputs as `actions/setup-go` as well. Signed-off-by: Hamza El-Saawy <[email protected]>
1 parent e7a1be7 commit 925fb1e

File tree

4 files changed

+83
-96
lines changed

4 files changed

+83
-96
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Setup Go
2+
description: Wraps actions/setup-go with default values.
3+
4+
branding:
5+
icon: package
6+
color: gray-dark
7+
8+
inputs:
9+
go-version:
10+
description: The Go version to download.
11+
default: oldstable
12+
cache:
13+
description: Enable caching Go modules and build outputs,
14+
default: "true"
15+
repo-path:
16+
description: Location this repo is checked out to.
17+
default: ${{ github.workspace }}
18+
fill-module-cache:
19+
description: Pre-fill the Go module cache.
20+
default: "false"
21+
22+
outputs:
23+
go-version:
24+
description: The installed Go version.
25+
value: ${{ steps.setup-go.outputs.go-version }}
26+
cache-hit:
27+
description: A boolean value to indicate if there was a cache hit.
28+
value: ${{ steps.setup-go.outputs.cache-hit }}
29+
30+
runs:
31+
using: composite
32+
steps:
33+
- name: Install Go
34+
id: setup-go
35+
uses: actions/setup-go@v5
36+
with:
37+
go-version: ${{ inputs.go-version }}
38+
check-latest: true
39+
cache: ${{ inputs.cache }}
40+
cache-dependency-path: |
41+
${{ inputs.repo-path }}/go.sum
42+
${{ inputs.repo-path }}/test/go.sum
43+
44+
- name: Pre-Fill Module Cache
45+
if: ${{ fromJSON(inputs.fill-module-cache) }}
46+
shell: pwsh # a shell value is required; pwsh is available on all GH runners
47+
run: |
48+
go mod download
49+
cd test
50+
go mod download
51+
working-directory: ${{ inputs.repo-path }}

.github/workflows/ci.yml

Lines changed: 24 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ on:
44
- pull_request
55

66
env:
7-
GO_VERSION: "oldstable"
8-
97
GO_BUILD_CMD: 'go build "-ldflags=-s -w" -trimpath'
108
GO_BUILD_TEST_CMD: "go test -mod=mod -gcflags=all=-d=checkptr -c -tags functional"
119

@@ -29,10 +27,9 @@ jobs:
2927
with:
3028
show-progress: false
3129

32-
- name: Install go
33-
uses: actions/setup-go@v5
30+
- name: Install Go
31+
uses: ./.github/actions/setup-go
3432
with:
35-
go-version: ${{ env.GO_VERSION }}
3633
# sometimes go cache causes issues when linting
3734
cache: false
3835

@@ -67,21 +64,11 @@ jobs:
6764
path: "${{ github.workspace }}/go/src/github.com/Microsoft/hcsshim"
6865
show-progress: false
6966

70-
- name: Install go
71-
uses: actions/setup-go@v5
67+
- name: Install Go
68+
uses: ./go/src/github.com/Microsoft/hcsshim/.github/actions/setup-go
7269
with:
73-
go-version: ${{ env.GO_VERSION }}
74-
cache-dependency-path: |
75-
${{ github.workspace }}/go/src/github.com/Microsoft/hcsshim/go.sum
76-
${{ github.workspace }}/go/src/github.com/Microsoft/hcsshim/test/go.sum
77-
78-
- name: Pre-fill Module Cache
79-
shell: powershell
80-
run: |
81-
go mod download
82-
cd test
83-
go mod download
84-
working-directory: "${{ github.workspace }}/go/src/github.com/Microsoft/hcsshim"
70+
repo-path: ${{ github.workspace }}/go/src/github.com/Microsoft/hcsshim
71+
fill-module-cache: true
8572

8673
- name: Install protoc
8774
shell: powershell
@@ -134,20 +121,10 @@ jobs:
134121
with:
135122
show-progress: false
136123

137-
- name: Install go
138-
uses: actions/setup-go@v5
124+
- name: Install Go
125+
uses: ./.github/actions/setup-go
139126
with:
140-
go-version: ${{ env.GO_VERSION }}
141-
cache-dependency-path: |
142-
go.sum
143-
test/go.sum
144-
145-
- name: Pre-fill Module Cache
146-
shell: powershell
147-
run: |
148-
go mod download
149-
cd test
150-
go mod download
127+
fill-module-cache: true
151128

152129
- name: Validate go.mod and vendoring
153130
shell: powershell
@@ -209,20 +186,10 @@ jobs:
209186
with:
210187
show-progress: false
211188

212-
- name: Install go
213-
uses: actions/setup-go@v5
189+
- name: Install Go
190+
uses: ./.github/actions/setup-go
214191
with:
215-
go-version: ${{ env.GO_VERSION }}
216-
cache-dependency-path: |
217-
go.sum
218-
test/go.sum
219-
220-
- name: Pre-fill Module Cache
221-
shell: powershell
222-
run: |
223-
go mod download
224-
cd test
225-
go mod download
192+
fill-module-cache: true
226193

227194
- name: Validate go generate
228195
shell: powershell
@@ -274,13 +241,10 @@ jobs:
274241
with:
275242
show-progress: false
276243

277-
- name: Install go
278-
uses: actions/setup-go@v5
244+
- name: Install Go
245+
uses: ./.github/actions/setup-go
279246
with:
280-
go-version: ${{ env.GO_VERSION }}
281-
cache-dependency-path: |
282-
go.sum
283-
test/go.sum
247+
fill-module-cache: true
284248

285249
- name: Install gotestsum
286250
run: go install gotest.tools/gotestsum@${{ env.GOTESTSUM_VERSION }}
@@ -314,13 +278,8 @@ jobs:
314278
with:
315279
show-progress: false
316280

317-
- name: Install go
318-
uses: actions/setup-go@v5
319-
with:
320-
go-version: ${{ env.GO_VERSION }}
321-
cache-dependency-path: |
322-
go.sum
323-
test/go.sum
281+
- name: Install Go
282+
uses: ./.github/actions/setup-go
324283

325284
- name: Install gotestsum
326285
run: go install gotest.tools/gotestsum@${{ env.GOTESTSUM_VERSION }}
@@ -447,14 +406,10 @@ jobs:
447406
path: src/github.com/Microsoft/hcsshim
448407
show-progress: false
449408

450-
- name: Install go
451-
uses: actions/setup-go@v5
409+
- name: Install Go
410+
uses: ./src/github.com/Microsoft/hcsshim/.github/actions/setup-go
452411
with:
453-
go-version: ${{ env.GO_VERSION }}
454-
check-latest: true
455-
cache-dependency-path: |
456-
src/github.com/Microsoft/hcsshim/go.sum
457-
src/github.com/Microsoft/hcsshim/test/go.sum
412+
repo-path: src/github.com/Microsoft/hcsshim
458413

459414
- name: Set env
460415
shell: bash
@@ -653,13 +608,8 @@ jobs:
653608
with:
654609
show-progress: false
655610

656-
- name: Install go
657-
uses: actions/setup-go@v5
658-
with:
659-
go-version: ${{ env.GO_VERSION }}
660-
cache-dependency-path: |
661-
go.sum
662-
test/go.sum
611+
- name: Install Go
612+
uses: ./.github/actions/setup-go
663613

664614
- name: Set version info
665615
shell: pwsh
@@ -737,13 +687,8 @@ jobs:
737687
with:
738688
show-progress: false
739689

740-
- name: Install go
741-
uses: actions/setup-go@v5
742-
with:
743-
go-version: ${{ env.GO_VERSION }}
744-
cache-dependency-path: |
745-
go.sum
746-
test/go.sum
690+
- name: Install Go
691+
uses: ./.github/actions/setup-go
747692

748693
- name: Set version info
749694
shell: pwsh

.github/workflows/codeql.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ on:
2727
# minute, hour, day of month, month, day of week
2828
- cron: "0 0 * * 0"
2929

30-
# TODO: consolidate this with ci.yml so they both use the same Go version
31-
env:
32-
GO_VERSION: "1.21.x"
33-
3430
jobs:
3531
analyze:
3632
name: Analyze (${{ matrix.language }} - ${{ matrix.goos }})
@@ -92,10 +88,10 @@ jobs:
9288
with:
9389
show-progress: false
9490

95-
- name: Install go
96-
uses: actions/setup-go@v5
91+
- name: Install Go
92+
uses: ./.github/actions/setup-go
9793
with:
98-
go-version: ${{ env.GO_VERSION }}
94+
fill-module-cache: true
9995

10096
- name: CodeQL Initialize
10197
uses: github/codeql-action/init@v3

.github/workflows/release.yml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@ name: Build & Release
22
on:
33
push:
44
tags:
5-
- 'v*'
6-
7-
env:
8-
GO_VERSION: "1.21.x"
5+
- "v*"
96

107
jobs:
118
build:
129
runs-on: "windows-2019"
1310
steps:
1411
- uses: actions/checkout@v4
15-
- uses: actions/setup-go@v5
16-
with:
17-
go-version: ${{ env.GO_VERSION }}
12+
- name: Install Go
13+
uses: ./.github/actions/setup-go
1814

1915
- run: go build ./cmd/dmverity-vhd
2016
- run: go build ./cmd/dmverity-vhd
21-
env:
17+
env:
2218
GOOS: linux
2319
GOARCH: amd64
2420

@@ -39,7 +35,7 @@ jobs:
3935
uses: actions/download-artifact@v4
4036
with:
4137
name: binaries
42-
38+
4339
- name: Publish draft release
4440
uses: softprops/[email protected]
4541
with:
@@ -49,4 +45,3 @@ jobs:
4945
files: |
5046
dmverity-vhd.exe
5147
dmverity-vhd
52-

0 commit comments

Comments
 (0)