forked from temporalio/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
243 lines (216 loc) · 8.7 KB
/
Copy pathbuild-and-publish.yml
File metadata and controls
243 lines (216 loc) · 8.7 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
name: Build and Publish (Reusable)
on:
workflow_call:
inputs:
publish:
description: "Whether to publish the release and Docker image"
required: true
type: boolean
version:
description: "Version tag for the release (required if publish is true)"
required: false
type: string
registry:
description: "Container registry (docker.io, ghcr.io, etc.)"
required: false
type: string
default: ""
registry_namespace:
description: "Registry namespace/organization"
required: false
type: string
default: ""
image_name:
description: "Image name"
required: false
type: string
default: "temporal"
secrets:
DOCKER_USERNAME:
required: false
DOCKER_PASSWORD:
required: false
jobs:
build:
name: Build and Publish
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: "go.mod"
check-latest: true
cache: true
- name: Get build date
id: date
run: echo "date=$(date '+%F-%T')" >> $GITHUB_OUTPUT
- name: Get build unix timestamp
id: timestamp
run: echo "timestamp=$(date '+%s')" >> $GITHUB_OUTPUT
- name: Get git branch
id: branch
run: echo "branch=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_OUTPUT
- name: Get build platform
id: platform
run: echo "platform=$(go version | cut -d ' ' -f 4)" >> $GITHUB_OUTPUT
- name: Get Go version
id: go
run: echo "go=$(go version | cut -d ' ' -f 3)" >> $GITHUB_OUTPUT
- name: Check if release is latest
if: inputs.publish
id: check_latest_release
uses: actions/github-script@v7
with:
script: |
const releaseTag = '${{ inputs.version }}';
const { data: release } = await github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: releaseTag
});
const isLatest = !release.prerelease && !release.draft;
core.setOutput('is_latest', isLatest);
console.log(`Release: ${release.tag_name}`);
console.log(`Prerelease: ${release.prerelease}, Draft: ${release.draft}`);
console.log(`Should tag as latest: ${isLatest}`);
- name: Run GoReleaser (release)
if: inputs.publish
uses: goreleaser/goreleaser-action@v6.4.0
with:
version: v2.12.7
args: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BUILD_DATE: ${{ steps.date.outputs.date }}
BUILD_TS_UNIX: ${{ steps.timestamp.outputs.timestamp }}
GIT_BRANCH: ${{ steps.branch.outputs.branch }}
BUILD_PLATFORM: ${{ steps.platform.outputs.platform }}
GO_VERSION: ${{ steps.go.outputs.go }}
- name: Run GoReleaser (snapshot)
if: ${{ !inputs.publish }}
uses: goreleaser/goreleaser-action@v6.4.0
with:
version: v2.12.7
args: release --snapshot --clean
env:
BUILD_DATE: ${{ steps.date.outputs.date }}
BUILD_TS_UNIX: ${{ steps.timestamp.outputs.timestamp }}
GIT_BRANCH: ${{ steps.branch.outputs.branch }}
BUILD_PLATFORM: ${{ steps.platform.outputs.platform }}
GO_VERSION: ${{ steps.go.outputs.go }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Get build metadata
id: meta
env:
INPUT_VERSION: ${{ inputs.version }}
INPUT_PUBLISH: ${{ inputs.publish }}
INPUT_REGISTRY: ${{ inputs.registry }}
INPUT_REGISTRY_NAMESPACE: ${{ inputs.registry_namespace }}
INPUT_IMAGE_NAME: ${{ inputs.image_name }}
REPO_OWNER: ${{ github.repository_owner }}
uses: actions/github-script@v7
with:
script: |
const inputVersion = process.env.INPUT_VERSION;
const inputPublish = process.env.INPUT_PUBLISH;
const inputRegistry = process.env.INPUT_REGISTRY;
const inputRegistryNamespace = process.env.INPUT_REGISTRY_NAMESPACE;
const inputImageName = process.env.INPUT_IMAGE_NAME;
const repoOwner = process.env.REPO_OWNER;
// Get git information
const { execSync } = require('child_process');
const cliSha = execSync('git rev-parse HEAD', { encoding: 'utf8' }).trim();
const imageShaTag = execSync('git rev-parse --short HEAD', { encoding: 'utf8' }).trim();
const imageBranchTag = execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf8' }).trim();
core.setOutput('cli_sha', cliSha);
core.setOutput('image_sha_tag', imageShaTag);
core.setOutput('image_branch_tag', imageBranchTag);
// Determine version
let version;
if (inputPublish === 'true') {
// Get version from input, strip 'v' prefix
version = inputVersion.startsWith('v') ? inputVersion.slice(1) : inputVersion;
} else {
version = 'snapshot';
}
core.setOutput('version', version);
// Determine registry (with auto-detection for temporalio vs forks)
let registry = inputRegistry;
if (!registry) {
if (repoOwner === 'temporalio') {
registry = 'docker.io';
} else {
registry = 'ghcr.io';
}
}
// Determine registry type for authentication
let registryType;
if (registry === 'ghcr.io') {
registryType = 'ghcr';
} else if (registry === 'docker.io') {
registryType = 'dockerhub';
} else {
registryType = 'other';
}
core.setOutput('registry_type', registryType);
// Set namespace (defaults to repository owner)
const namespace = inputRegistryNamespace || repoOwner;
core.setOutput('image_namespace', namespace);
// Set image name (defaults to 'temporal')
const imageName = inputImageName || 'temporal';
core.setOutput('image_name', imageName);
// For Docker Hub, use empty string as registry (special case)
const imageRepo = registry === 'docker.io' ? '' : registry;
core.setOutput('image_repo', imageRepo);
console.log(`Registry: ${registry}, Type: ${registryType}, Namespace: ${namespace}, Image: ${imageName}`);
- name: Log in to GitHub Container Registry
if: inputs.publish && steps.meta.outputs.registry_type == 'ghcr'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to Docker Hub
if: inputs.publish && steps.meta.outputs.registry_type == 'dockerhub'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
if: inputs.publish
run: |
docker buildx bake \
--file docker-bake.hcl \
--push \
cli
env:
CLI_SHA: ${{ steps.meta.outputs.cli_sha }}
IMAGE_SHA_TAG: ${{ steps.meta.outputs.image_sha_tag }}
IMAGE_BRANCH_TAG: ${{ steps.meta.outputs.image_branch_tag }}
VERSION: ${{ steps.meta.outputs.version }}
TAG_LATEST: ${{ steps.check_latest_release.outputs.is_latest == 'true' }}
IMAGE_REPO: ${{ steps.meta.outputs.image_repo }}
IMAGE_NAMESPACE: ${{ steps.meta.outputs.image_namespace }}
IMAGE_NAME: ${{ steps.meta.outputs.image_name }}
GITHUB_REPOSITORY: ${{ github.repository }}
- name: Build Docker image
if: ${{ !inputs.publish }}
run: |
docker buildx bake \
--file docker-bake.hcl \
cli
env:
CLI_SHA: ${{ steps.meta.outputs.cli_sha }}
IMAGE_SHA_TAG: ${{ steps.meta.outputs.image_sha_tag }}
IMAGE_BRANCH_TAG: ${{ steps.meta.outputs.image_branch_tag }}
VERSION: ${{ steps.meta.outputs.version }}
TAG_LATEST: false
IMAGE_REPO: ${{ steps.meta.outputs.image_repo }}
IMAGE_NAMESPACE: ${{ steps.meta.outputs.image_namespace }}
IMAGE_NAME: ${{ steps.meta.outputs.image_name }}
GITHUB_REPOSITORY: ${{ github.repository }}