generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
238 lines (210 loc) · 9.29 KB
/
sherlock-build.yaml
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
name: Bump, Tag, and Publish
# The purpose of the workflow is to:
# 1. Bump the version number and tag the release if not a PR
# 2. Build docker image and publish to GAR
#
# When run on merge to main, it tags and bumps the patch version by default. You can
# bump other parts of the version by putting #major, #minor, or #patch in your commit
# message.
#
# When run on a PR, it simulates bumping the tag and appends a hash to the pushed image.
#
# The workflow relies on github secrets:
# - BROADBOT_TOKEN - the broadbot token, so we can avoid two reviewer rule on GHA operations
on:
push:
branches:
- main
paths:
- ".github/workflows/sherlock-build.yaml"
- "sherlock/**"
- "!sherlock/docs/**"
- "go-shared/**"
pull_request:
branches:
- main
paths:
- ".github/workflows/sherlock-build.yaml"
- "sherlock/**"
- "!sherlock/docs/**"
- "go-shared/**"
workflow_dispatch:
env:
# Google project where artifacts are uploaded.
GOOGLE_PROJECT: dsp-artifact-registry
# Name of the app-specific Docker repository configured in GOOGLE_PROJECT.
REPOSITORY_NAME: ${{ github.event.repository.name }}
# Name of the image we'll be uploading into the Docker repository.
# This is often equal to the GitHub repository name, but it might also be the
# name of the Helm Chart if that's different.
IMAGE_NAME: ${{ github.event.repository.name }}
# This is the region-specific top-level Google-managed domain where our
# GOOGLE_PROJECT/REPOSITORY_NAME can be found.
GOOGLE_DOCKER_REPOSITORY: us-central1-docker.pkg.dev
# App-specific variables like versions of build tools:
GO_SWAGGER_VERSION: v0.29.0
concurrency:
# Don't run this workflow concurrently on the same branch
group: ${{ github.workflow }}-${{ github.ref }}
# For PRs, don't wait for completion of existing runs, cancel them instead
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
tag-build-publish:
runs-on: ubuntu-latest
permissions:
# We'll use Broadbot to push changed tag
contents: "read"
# Use OIDC for workload identity
id-token: "write"
# Make comments
pull-requests: "write"
steps:
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ (github.actor != 'dependabot[bot]' && secrets.BROADBOT_TOKEN) || secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Set up Git
if: ${{ github.actor != 'dependabot[bot]' }}
run: |
git config --global user.name 'broadbot'
git config --global user.email '[email protected]'
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: sherlock/go.mod
- name: Set up Node/NPM
if: ${{ github.event_name != 'pull_request' }}
uses: actions/setup-node@v3
with:
node-version: 18
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Set up Swag
run: go install github.com/swaggo/swag/cmd/swag@latest
# We use DRY_RUN so we don't push to the repo prematurely -- we may
# have code-gen changes, or we might not end up pushing at all.
- name: Generate Tag
uses: databiosphere/github-actions/actions/[email protected]
id: tag
env:
DEFAULT_BUMP: patch
RELEASE_BRANCHES: ${{ github.event.repository.default_branch }}
WITH_V: true
GITHUB_TOKEN: ${{ (github.actor != 'dependabot[bot]' && secrets.BROADBOT_TOKEN) || secrets.GITHUB_TOKEN }}
DRY_RUN: true
- name: Generate Swagger source
run: make generate-swagger
- name: Generate Go client library
if: ${{ github.event_name != 'pull_request' }}
run: |
docker run --rm -e GOPATH=/go \
-v $(go env GOPATH):/go \
-v "${PWD}:/local" \
-w "/local/sherlock-go-client" \
quay.io/goswagger/swagger:${GO_SWAGGER_VERSION} \
generate client -f /local/sherlock/docs/swagger.json -A sherlock --default-scheme=https -m client/models -c client
- name: Tidy Go client library dependencies
if: ${{ github.event_name != 'pull_request' }}
working-directory: sherlock-go-client
run: go mod tidy
- name: Generate Typescript client library
if: ${{ github.event_name != 'pull_request' }}
run: |
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
-i /local/sherlock/docs/swagger.json \
-g typescript-fetch \
-o /local/sherlock-typescript-client \
--git-user-id broadinstitute \
--git-repo-id sherlock \
--additional-properties=disallowAdditionalPropertiesIfNotPresent=false \
--additional-properties=supportsES6=true \
--additional-properties=npmName=@sherlock-js-client/sherlock \
--additional-properties=npmVersion=${{ steps.tag.outputs.new_tag }}
- name: Build Typescript client
if: ${{ github.event_name != 'pull_request' }}
working-directory: sherlock-typescript-client
run: |
npm install --save-dev
npm run build
- name: Assemble Docker tags
uses: docker/metadata-action@v5
id: meta
with:
# server image for backwards compatibility with old build behavior
images: |
${{ env.GOOGLE_DOCKER_REPOSITORY }}/${{ env.GOOGLE_PROJECT }}/${{ env.REPOSITORY_NAME }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=raw,value=${{ steps.tag.outputs.new_tag }}
type=semver,pattern=v{{major}},value=${{ steps.tag.outputs.new_tag }},enable={{is_default_branch}}
type=semver,pattern=v{{major}}.{{minor}},value=${{ steps.tag.outputs.new_tag }},enable={{is_default_branch}}
- name: Build image
uses: docker/build-push-action@v5
with:
context: .
file: sherlock/Dockerfile
push: false
load: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: BUILD_VERSION=${{ steps.tag.outputs.new_tag }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Run Trivy vulnerability scanner
uses: broadinstitute/dsp-appsec-trivy-action@v1
with:
image: ${{ env.GOOGLE_DOCKER_REPOSITORY }}/${{ env.GOOGLE_PROJECT }}/${{ env.REPOSITORY_NAME }}/${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.new_tag }}
- name: Auth to GCP
if: ${{ github.actor != 'dependabot[bot]' }}
uses: google-github-actions/auth@v1
with:
workload_identity_provider: 'projects/1038484894585/locations/global/workloadIdentityPools/github-wi-pool/providers/github-wi-provider'
service_account: 'dsp-artifact-registry-push@dsp-artifact-registry.iam.gserviceaccount.com'
- name: Set up Cloud SDK
if: ${{ github.actor != 'dependabot[bot]' }}
uses: google-github-actions/setup-gcloud@v1
- name: Commit changes
if: ${{ github.event_name != 'pull_request' }}
run: |
git add .
git commit --allow-empty --message "[sherlock-build] generated from ${{ github.sha }}"
- name: Add tag
if: ${{ github.event_name != 'pull_request' }}
# Go subdirectories need to have tags prefixed with the directory name, otherwise go mod won't be
# able to see them.
# In other words, if you do `go get github.com/broadinstitute/sherlock/[email protected]`,
# go mod is actually going to look for a tag of sherlock-go-client/v1.2.3.
# https://github.com/golang/go/issues/31045
run: |
git tag "${{ steps.tag.outputs.new_tag }}"
git tag "sherlock/${{ steps.tag.outputs.new_tag }}"
git tag "go-shared/${{ steps.tag.outputs.new_tag }}"
git tag "sherlock-go-client/${{ steps.tag.outputs.new_tag }}"
- name: Push to main
if: ${{ github.event_name != 'pull_request' }}
run: |
git push --atomic --tags origin main
- name: Publish typescript client
if: ${{ github.event_name != 'pull_request' }}
working-directory: sherlock-typescript-client
run: |
npx google-artifactregistry-auth --yes
npm publish
- name: Explicitly auth Docker for Artifact Registry
if: ${{ github.actor != 'dependabot[bot]' }}
run: gcloud auth configure-docker ${{ env.GOOGLE_DOCKER_REPOSITORY }} --quiet
- name: Push image
if: ${{ github.actor != 'dependabot[bot]' }}
run: |
docker push --all-tags ${{ env.GOOGLE_DOCKER_REPOSITORY }}/${{ env.GOOGLE_PROJECT }}/${{ env.REPOSITORY_NAME }}/${{ env.IMAGE_NAME }}
- name: Comment published image
if: ${{ github.event_name == 'pull_request' && github.actor != 'dependabot[bot]' }}
uses: marocchino/sticky-pull-request-comment@v2
with:
header: image
message: |
Published image from ${{ github.event.pull_request.head.sha }} (merge ${{ github.sha }}):
```
${{ env.GOOGLE_DOCKER_REPOSITORY }}/${{ env.GOOGLE_PROJECT }}/${{ env.REPOSITORY_NAME }}/${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.new_tag }}
```