Skip to content

Commit e313fa0

Browse files
authored
ci(swagger-ui-release-docker): add GA for build&push docker image (#10500)
1 parent bf7b19d commit e313fa0

File tree

2 files changed

+190
-114
lines changed

2 files changed

+190
-114
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
name: Build & Push SwaggerUI multi platform Docker image
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
git_ref:
7+
description: Git branch, tag or SHA to checkout.
8+
type: string
9+
required: true
10+
docker_tag:
11+
description: Docker tag associated with the `git_ref`
12+
type: string
13+
required: true
14+
15+
repository_dispatch:
16+
type: [docker_build_push]
17+
18+
env:
19+
REGISTRY_IMAGE: swaggerapi/swagger-ui
20+
21+
jobs:
22+
inputs:
23+
name: Normalize inputs
24+
runs-on: ubuntu-latest
25+
outputs:
26+
git_ref: ${{ steps.workflow_dispatch.outputs.git_ref || steps.repository_dispatch.outputs.git_ref }}
27+
docker_tag: ${{ steps.workflow_dispatch.outputs.docker_tag || steps.repository_dispatch.outputs.docker_tag }}
28+
29+
steps:
30+
- name: Normalize inputs of `workflow_dispatch` event
31+
id: workflow_dispatch
32+
if: ${{ github.event_name == 'workflow_dispatch' }}
33+
run: |
34+
echo "git_ref=${{ inputs.git_ref }}" >> "$GITHUB_OUTPUT"
35+
echo "docker_tag=${{ inputs.docker_tag }}" >> "$GITHUB_OUTPUT"
36+
37+
- name: Normalize inputs of `repository_dispatch` event
38+
id: repository_dispatch
39+
if: ${{ github.event_name == 'repository_dispatch' }}
40+
run: |
41+
echo "git_ref=${{ github.event.client_payload.git_ref }}" >> "$GITHUB_OUTPUT"
42+
echo "docker_tag=${{ github.event.client_payload.docker_tag }}" >> "$GITHUB_OUTPUT"
43+
44+
45+
46+
build:
47+
name: Build & Push SwaggerUI platform specific Docker images
48+
runs-on: ubuntu-latest
49+
strategy:
50+
fail-fast: false
51+
matrix:
52+
platform:
53+
# linux/amd64 is already built by Jenkins
54+
- linux/arm/v6
55+
- linux/arm64
56+
- linux/386
57+
- linux/ppc64le
58+
needs:
59+
- inputs
60+
61+
steps:
62+
- uses: actions/checkout@v4
63+
with:
64+
ref: ${{ needs.inputs.outputs.git_ref }}
65+
66+
- name: Set up QEMU
67+
uses: docker/setup-qemu-action@v3
68+
69+
- name: Set up Docker Buildx
70+
uses: docker/setup-buildx-action@v3
71+
72+
- name: Log in to DockerHub
73+
uses: docker/login-action@v3
74+
with:
75+
username: ${{ secrets.DOCKERHUB_SB_USERNAME }}
76+
password: ${{ secrets.DOCKERHUB_SB_PASSWORD }}
77+
78+
- name: Build and push by digest
79+
id: build
80+
uses: docker/build-push-action@v6
81+
with:
82+
context: .
83+
platforms: ${{ matrix.platform }}
84+
provenance: false
85+
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
86+
87+
- name: Export digest
88+
run: |
89+
mkdir -p /tmp/digests
90+
digest="${{ steps.build.outputs.digest }}"
91+
touch "/tmp/digests/${digest#sha256:}"
92+
93+
- name: Sanitize platform variable
94+
id: sanitize_platform
95+
run: |
96+
SANITIZED_PLATFORM="${{ matrix.platform }}" # Assuming direct usage for simplicity
97+
SANITIZED_PLATFORM="${SANITIZED_PLATFORM//[^a-zA-Z0-9_-]/}" # Remove special chars
98+
echo "SANITIZED_PLATFORM=${SANITIZED_PLATFORM}" # Echo for debug
99+
echo "::set-output name=sanitized_platform::${SANITIZED_PLATFORM}"
100+
101+
- name: Upload digest
102+
uses: actions/upload-artifact@v4
103+
with:
104+
name: digest-${{ steps.sanitize_platform.outputs.sanitized_platform }}
105+
path: /tmp/digests/*
106+
if-no-files-found: error
107+
retention-days: 1
108+
109+
merge:
110+
name: Merge platform specific Docker image into multi platform image
111+
runs-on: ubuntu-latest
112+
needs:
113+
- inputs
114+
- build
115+
116+
steps:
117+
- name: Download digests
118+
uses: actions/download-artifact@v4
119+
with:
120+
pattern: digest-*
121+
path: /tmp/digests
122+
merge-multiple: true
123+
124+
- name: Set up Docker Buildx
125+
uses: docker/setup-buildx-action@v3
126+
127+
- name: Login to Docker Hub
128+
uses: docker/login-action@v3
129+
with:
130+
username: ${{ secrets.DOCKERHUB_SB_USERNAME }}
131+
password: ${{ secrets.DOCKERHUB_SB_PASSWORD }}
132+
133+
- name: Create manifest list and push
134+
working-directory: /tmp/digests
135+
run: |
136+
docker buildx imagetools create -t ${{ env.REGISTRY_IMAGE }}:${{ needs.inputs.outputs.docker_tag }} \
137+
${{ env.REGISTRY_IMAGE }}:${{ needs.inputs.outputs.docker_tag }} \
138+
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
139+
140+
- name: Inspect image
141+
run: |
142+
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ needs.inputs.outputs.docker_tag }}
Lines changed: 48 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,59 @@
1-
# inspired by https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
2-
name: Build & Push SwaggerUI multi platform Docker image
1+
name: Build & Push SwaggerUI Docker image
32

43
on:
5-
workflow_dispatch:
6-
inputs:
7-
git_ref:
8-
description: Git branch, tag or SHA to checkout.
9-
type: string
10-
required: true
11-
docker_tag:
12-
description: Docker tag associated with the `git_ref`
13-
type: string
14-
required: true
15-
16-
repository_dispatch:
17-
type: [docker_build_push]
18-
19-
env:
20-
REGISTRY_IMAGE: swaggerapi/swagger-ui
4+
workflow_run:
5+
workflows: ["Release SwaggerUI"]
6+
types:
7+
- completed
8+
branches: [master]
219

2210
jobs:
23-
inputs:
24-
name: Normalize inputs
11+
12+
build-push:
13+
if: github.event.workflow_run.conclusion == 'success'
14+
name: Build & Push SwaggerUI Docker image
2515
runs-on: ubuntu-latest
26-
outputs:
27-
git_ref: ${{ steps.workflow_dispatch.outputs.git_ref || steps.repository_dispatch.outputs.git_ref }}
28-
docker_tag: ${{ steps.workflow_dispatch.outputs.docker_tag || steps.repository_dispatch.outputs.docker_tag }}
2916

3017
steps:
31-
- name: Normalize inputs of `workflow_dispatch` event
32-
id: workflow_dispatch
33-
if: ${{ github.event_name == 'workflow_dispatch' }}
34-
run: |
35-
echo "git_ref=${{ inputs.git_ref }}" >> "$GITHUB_OUTPUT"
36-
echo "docker_tag=${{ inputs.docker_tag }}" >> "$GITHUB_OUTPUT"
37-
38-
- name: Normalize inputs of `repository_dispatch` event
39-
id: repository_dispatch
40-
if: ${{ github.event_name == 'repository_dispatch' }}
41-
run: |
42-
echo "git_ref=${{ github.event.client_payload.git_ref }}" >> "$GITHUB_OUTPUT"
43-
echo "docker_tag=${{ github.event.client_payload.docker_tag }}" >> "$GITHUB_OUTPUT"
18+
- uses: actions/checkout@v4
4419

20+
- name: Use Node.js 22
21+
uses: actions/setup-node@v4
22+
with:
23+
node-version: 22
24+
cache: npm
25+
cache-dependency-path: package-lock.json
4526

27+
- name: Install dependencies
28+
run: npm ci
4629

47-
build:
48-
name: Build & Push SwaggerUI platform specific Docker images
49-
runs-on: ubuntu-latest
50-
strategy:
51-
fail-fast: false
52-
matrix:
53-
platform:
54-
# linux/amd64 is already built by Jenkins
55-
- linux/arm/v6
56-
- linux/arm64
57-
- linux/386
58-
- linux/ppc64le
59-
needs:
60-
- inputs
30+
- name: Build SwaggerUI
31+
run: npm run build
6132

62-
steps:
63-
- uses: actions/checkout@v4
33+
- name: Determine released version
34+
uses: actions/github-script@v7
6435
with:
65-
ref: ${{ needs.inputs.outputs.git_ref }}
36+
script: |
37+
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
38+
owner: context.repo.owner,
39+
repo: context.repo.repo,
40+
run_id: context.payload.workflow_run.id,
41+
});
42+
const matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
43+
return artifact.name == "released-version"
44+
})[0];
45+
const download = await github.rest.actions.downloadArtifact({
46+
owner: context.repo.owner,
47+
repo: context.repo.repo,
48+
artifact_id: matchArtifact.id,
49+
archive_format: 'zip',
50+
});
51+
const fs = require('fs');
52+
fs.writeFileSync('${{github.workspace}}/released-version.zip', Buffer.from(download.data));
53+
- run: |
54+
unzip released-version.zip
55+
RELEASED_VERSION=$(cat released-version.txt)
56+
echo "RELEASED_VERSION=$RELEASED_VERSION" >> $GITHUB_ENV
6657
6758
- name: Set up QEMU
6859
uses: docker/setup-qemu-action@v3
@@ -76,68 +67,11 @@ jobs:
7667
username: ${{ secrets.DOCKERHUB_SB_USERNAME }}
7768
password: ${{ secrets.DOCKERHUB_SB_PASSWORD }}
7869

79-
- name: Build and push by digest
80-
id: build
70+
- name: Build docker image and push
8171
uses: docker/build-push-action@v6
8272
with:
8373
context: .
84-
platforms: ${{ matrix.platform }}
74+
push: true
75+
platforms: linux/amd64,linux/arm/v6,linux/arm64,linux/386,linux/ppc64le
8576
provenance: false
86-
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
87-
88-
- name: Export digest
89-
run: |
90-
mkdir -p /tmp/digests
91-
digest="${{ steps.build.outputs.digest }}"
92-
touch "/tmp/digests/${digest#sha256:}"
93-
94-
- name: Sanitize platform variable
95-
id: sanitize_platform
96-
run: |
97-
SANITIZED_PLATFORM="${{ matrix.platform }}" # Assuming direct usage for simplicity
98-
SANITIZED_PLATFORM="${SANITIZED_PLATFORM//[^a-zA-Z0-9_-]/}" # Remove special chars
99-
echo "SANITIZED_PLATFORM=${SANITIZED_PLATFORM}" # Echo for debug
100-
echo "::set-output name=sanitized_platform::${SANITIZED_PLATFORM}"
101-
102-
- name: Upload digest
103-
uses: actions/upload-artifact@v4
104-
with:
105-
name: digest-${{ steps.sanitize_platform.outputs.sanitized_platform }}
106-
path: /tmp/digests/*
107-
if-no-files-found: error
108-
retention-days: 1
109-
110-
merge:
111-
name: Merge platform specific Docker image into multi platform image
112-
runs-on: ubuntu-latest
113-
needs:
114-
- inputs
115-
- build
116-
117-
steps:
118-
- name: Download digests
119-
uses: actions/download-artifact@v4
120-
with:
121-
pattern: digest-*
122-
path: /tmp/digests
123-
merge-multiple: true
124-
125-
- name: Set up Docker Buildx
126-
uses: docker/setup-buildx-action@v3
127-
128-
- name: Login to Docker Hub
129-
uses: docker/login-action@v3
130-
with:
131-
username: ${{ secrets.DOCKERHUB_SB_USERNAME }}
132-
password: ${{ secrets.DOCKERHUB_SB_PASSWORD }}
133-
134-
- name: Create manifest list and push
135-
working-directory: /tmp/digests
136-
run: |
137-
docker buildx imagetools create -t ${{ env.REGISTRY_IMAGE }}:${{ needs.inputs.outputs.docker_tag }} \
138-
${{ env.REGISTRY_IMAGE }}:${{ needs.inputs.outputs.docker_tag }} \
139-
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
140-
141-
- name: Inspect image
142-
run: |
143-
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ needs.inputs.outputs.docker_tag }}
77+
tags: swaggerapi/swagger-ui:latest,swaggerapi/swagger-ui:v${{ env.RELEASED_VERSION }}

0 commit comments

Comments
 (0)