-
Notifications
You must be signed in to change notification settings - Fork 0
192 lines (168 loc) · 6.58 KB
/
Copy pathdocker-build-beta.yml
File metadata and controls
192 lines (168 loc) · 6.58 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
name: Build and Push Beta Docker Image
on:
push:
branches:
- 'sandbox-*'
- 'beta-*'
- 'feature-*'
- 'dev-*'
- 'fix/*'
# Allow manual triggering with branch input
workflow_dispatch:
inputs:
branch:
description: 'Branch name to build from'
required: true
default: 'sandbox-testv0.1'
beta_version:
description: 'Beta version tag (e.g., beta.1)'
required: true
default: 'beta.1'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
GOTOOLCHAIN: auto
jobs:
build-and-push-beta:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
actions: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.branch || github.ref }}
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23'
check-latest: true
# Add Go module caching
- name: Go Module Cache
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Prepare go.mod for CI
run: |
# Remove toolchain directive for CI compatibility
sed -i '/^toolchain/d' go.mod
# Run tests in parallel
- name: Run unit tests
run: go test -v -parallel 4 ./pkg/... -coverprofile=coverage.txt -covermode=atomic
- name: Display test coverage
run: go tool cover -func=coverage.txt
- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage.txt
retention-days: 7
- name: Extract branch name
shell: bash
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
# For manual workflow, use the provided branch name
BRANCH_NAME="${{ github.event.inputs.branch }}"
else
# For push events, extract from GITHUB_REF
BRANCH_NAME=${GITHUB_REF#refs/heads/}
fi
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
# Also set as output for the step
echo "branch_name=${BRANCH_NAME}" >> $GITHUB_OUTPUT
id: extract_branch
- name: Generate beta version
id: beta_version
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
# Use the manually provided beta version
BETA_VERSION="${{ github.event.inputs.beta_version }}"
else
# Generate a beta version based on branch name and short SHA
SHORT_SHA=$(git rev-parse --short HEAD)
BRANCH_SUFFIX=$(echo "${{ steps.extract_branch.outputs.branch_name }}" | sed 's/[^a-zA-Z0-9]/-/g')
BETA_VERSION="beta.${BRANCH_SUFFIX}.${SHORT_SHA}"
fi
echo "BETA_VERSION=${BETA_VERSION}" >> $GITHUB_ENV
# Also set as output for the step
echo "beta_version=${BETA_VERSION}" >> $GITHUB_OUTPUT
echo "Generated beta version: ${BETA_VERSION}"
- name: Check Go version in Dockerfile and go.mod
run: |
echo "Checking Go version in Dockerfile..."
GO_VERSION_DOCKERFILE=$(grep "FROM golang:" Dockerfile | head -1 | cut -d':' -f2 | cut -d'-' -f1)
echo "Go version in Dockerfile: $GO_VERSION_DOCKERFILE"
echo "Checking Go version in go.mod..."
GO_VERSION_GOMOD=$(grep "^go " go.mod | awk '{print $2}')
echo "Go version in go.mod: $GO_VERSION_GOMOD"
# Check for toolchain directive
if grep -q "^toolchain " go.mod; then
TOOLCHAIN_DIRECTIVE=$(grep "^toolchain " go.mod | awk '{print $2}')
echo "Toolchain directive found in go.mod: $TOOLCHAIN_DIRECTIVE"
else
echo "No toolchain directive found in go.mod"
fi
if [[ "$GO_VERSION_DOCKERFILE" != "$GO_VERSION_GOMOD" ]]; then
echo "Warning: Go version mismatch between Dockerfile ($GO_VERSION_DOCKERFILE) and go.mod ($GO_VERSION_GOMOD)"
echo "This is expected and will be handled by the GOTOOLCHAIN=auto setting"
fi
echo "GOTOOLCHAIN environment variable is set to: $GOTOOLCHAIN"
env:
GOTOOLCHAIN: auto
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ steps.beta_version.outputs.beta_version }}
type=raw,value=beta-${{ steps.extract_branch.outputs.branch_name }}
type=raw,value=beta-latest
type=sha,prefix=beta-
- name: Build and push Docker image
uses: docker/build-push-action@v5
env:
DOCKER_BUILDKIT: 1
GOTOOLCHAIN: auto
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
GOTOOLCHAIN=auto
SKIP_TESTS=true
SKIP_UPX=false
cache-from: type=gha,scope=${{ github.workflow }}
cache-to: type=gha,mode=max,scope=${{ github.workflow }}
platforms: linux/amd64
provenance: false
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=false
build-contexts: |
golang:1.23-alpine=docker-image://golang:1.23-alpine
- name: Display image information
run: |
echo "::notice::Beta image built and pushed successfully!"
echo "::notice::Image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.beta_version.outputs.beta_version }}"
echo "::notice::Branch: ${{ steps.extract_branch.outputs.branch_name }}"
echo "::notice::Commit: ${{ github.sha }}"
echo "::notice::You can pull this image with: docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.beta_version.outputs.beta_version }}"
echo "::notice::All tags pushed:"
echo "${{ steps.meta.outputs.tags }}" | tr '\n' ' '