Skip to content

Commit

Permalink
Consolidate Go installation step
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
helsaawy committed Aug 23, 2024
1 parent e7a1be7 commit d7eb955
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 93 deletions.
49 changes: 49 additions & 0 deletions .github/actions/setup-go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Setup Go
description: Wraps actions/setup-go with default values.

branding:
icon: package
color: grey-dark

inputs:
go-version:
description: The Go version to download.
default: oldstable
cache:
description: Enable caching Go modules and build outputs,
default: true
repo-path:
description: Location this repo is checked out to.
default: ${{ github.workspace }}
fill-module-cache:
description: Pre-fill the Go module cache.
default: false

outputs:
go-version:
description: The installed Go version.
value: ${{ steps.setup-go.outputs.go-version }}
cache-hit:
description: A boolean value to indicate if there was a cache hit.
value: ${{ steps.setup-go.outputs.cache-hit }}

runs:
using: composite
steps:
- name: Install Go
id: setup-go
uses: actions/setup-go@v5
with:
go-version: ${{ inputs.go-version }}
cache: ${{ inputs.cache }}
cache-dependency-path: |
${{ inputs.repo-path }}/go.sum
${{ inputs.repo-path }}/test/go.sum
- name: Fill Module Cache
if: ${{ fromJSON(inputs.fill-module-cache) }}
run: |
go mod download
cd test
go mod download
working-directory: ${{ inputs.repo-path }}
108 changes: 32 additions & 76 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
- pull_request

env:
GO_VERSION: "oldstable"

GO_BUILD_CMD: 'go build "-ldflags=-s -w" -trimpath'
GO_BUILD_TEST_CMD: "go test -mod=mod -gcflags=all=-d=checkptr -c -tags functional"

Expand All @@ -29,10 +27,9 @@ jobs:
with:
show-progress: false

- name: Install go
uses: actions/setup-go@v5
- name: Install Go
uses: ./.github/actions/setup-go
with:
go-version: ${{ env.GO_VERSION }}
# sometimes go cache causes issues when linting
cache: false

Expand Down Expand Up @@ -67,21 +64,21 @@ jobs:
path: "${{ github.workspace }}/go/src/github.com/Microsoft/hcsshim"
show-progress: false

- name: Install go
uses: actions/setup-go@v5
- name: Install Go
# todo ----------------------------------------------------------------------------------------
id: go-install
uses: ./.github/actions/setup-go
with:
go-version: ${{ env.GO_VERSION }}
cache-dependency-path: |
${{ github.workspace }}/go/src/github.com/Microsoft/hcsshim/go.sum
${{ github.workspace }}/go/src/github.com/Microsoft/hcsshim/test/go.sum
repo-path: ${{ github.workspace }}/go/src/github.com/Microsoft/hcsshim
fill-module-cache: true

- name: Pre-fill Module Cache
# todo ----------------------------------------------------------------------------------------
- name: test output
shell: powershell
run: |
go mod download
cd test
go mod download
working-directory: "${{ github.workspace }}/go/src/github.com/Microsoft/hcsshim"
echo "> ${{ steps.go-install.outputs }}"
echo "> ${{ steps.go-install.outputs.go-version }}"
echo "> ${{ steps.go-install.outputs.cache-hit }}"
- name: Install protoc
shell: powershell
Expand Down Expand Up @@ -134,20 +131,10 @@ jobs:
with:
show-progress: false

- name: Install go
uses: actions/setup-go@v5
- name: Install Go
uses: ./.github/actions/setup-go
with:
go-version: ${{ env.GO_VERSION }}
cache-dependency-path: |
go.sum
test/go.sum
- name: Pre-fill Module Cache
shell: powershell
run: |
go mod download
cd test
go mod download
fill-module-cache: true

- name: Validate go.mod and vendoring
shell: powershell
Expand Down Expand Up @@ -209,20 +196,10 @@ jobs:
with:
show-progress: false

- name: Install go
uses: actions/setup-go@v5
- name: Install Go
uses: ./.github/actions/setup-go
with:
go-version: ${{ env.GO_VERSION }}
cache-dependency-path: |
go.sum
test/go.sum
- name: Pre-fill Module Cache
shell: powershell
run: |
go mod download
cd test
go mod download
fill-module-cache: true

- name: Validate go generate
shell: powershell
Expand Down Expand Up @@ -274,13 +251,10 @@ jobs:
with:
show-progress: false

- name: Install go
uses: actions/setup-go@v5
- name: Install Go
uses: ./.github/actions/setup-go
with:
go-version: ${{ env.GO_VERSION }}
cache-dependency-path: |
go.sum
test/go.sum
fill-module-cache: true

- name: Install gotestsum
run: go install gotest.tools/gotestsum@${{ env.GOTESTSUM_VERSION }}
Expand Down Expand Up @@ -314,13 +288,9 @@ jobs:
with:
show-progress: false

- name: Install go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
cache-dependency-path: |
go.sum
test/go.sum
# todo ----------------------------------------------------------------------------------------
# - name: Install Go
- uses: ./.github/actions/setup-go

- name: Install gotestsum
run: go install gotest.tools/gotestsum@${{ env.GOTESTSUM_VERSION }}
Expand Down Expand Up @@ -447,14 +417,10 @@ jobs:
path: src/github.com/Microsoft/hcsshim
show-progress: false

- name: Install go
uses: actions/setup-go@v5
- name: Install Go
uses: ./.github/actions/setup-go
with:
go-version: ${{ env.GO_VERSION }}
check-latest: true
cache-dependency-path: |
src/github.com/Microsoft/hcsshim/go.sum
src/github.com/Microsoft/hcsshim/test/go.sum
repo-path: src/github.com/Microsoft/hcsshim

- name: Set env
shell: bash
Expand Down Expand Up @@ -653,13 +619,8 @@ jobs:
with:
show-progress: false

- name: Install go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
cache-dependency-path: |
go.sum
test/go.sum
- name: Install Go
uses: ./.github/actions/setup-go

- name: Set version info
shell: pwsh
Expand Down Expand Up @@ -737,13 +698,8 @@ jobs:
with:
show-progress: false

- name: Install go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
cache-dependency-path: |
go.sum
test/go.sum
- name: Install Go
uses: ./.github/actions/setup-go

- name: Set version info
shell: pwsh
Expand Down
10 changes: 3 additions & 7 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ on:
# minute, hour, day of month, month, day of week
- cron: "0 0 * * 0"

# TODO: consolidate this with ci.yml so they both use the same Go version
env:
GO_VERSION: "1.21.x"

jobs:
analyze:
name: Analyze (${{ matrix.language }} - ${{ matrix.goos }})
Expand Down Expand Up @@ -92,10 +88,10 @@ jobs:
with:
show-progress: false

- name: Install go
uses: actions/setup-go@v5
- name: Install Go
uses: ./.github/actions/setup-go
with:
go-version: ${{ env.GO_VERSION }}
fill-module-cache: true

- name: CodeQL Initialize
uses: github/codeql-action/init@v3
Expand Down
15 changes: 5 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@ name: Build & Release
on:
push:
tags:
- 'v*'

env:
GO_VERSION: "1.21.x"
- "v*"

jobs:
build:
runs-on: "windows-2019"
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Install Go
uses: ./.github/actions/setup-go

- run: go build ./cmd/dmverity-vhd
- run: go build ./cmd/dmverity-vhd
env:
env:
GOOS: linux
GOARCH: amd64

Expand All @@ -39,7 +35,7 @@ jobs:
uses: actions/download-artifact@v4
with:
name: binaries

- name: Publish draft release
uses: softprops/[email protected]
with:
Expand All @@ -49,4 +45,3 @@ jobs:
files: |
dmverity-vhd.exe
dmverity-vhd

0 comments on commit d7eb955

Please sign in to comment.