Skip to content

Commit 30d0637

Browse files
committed
ci: implement action for building Docker image
1 parent 77449a3 commit 30d0637

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
name: Docker
2+
3+
on:
4+
push:
5+
branches: [ "main", "ci/implement-docker-action" ]
6+
tags: [ 'v*.*.*' ]
7+
pull_request:
8+
branches: [ "main" ]
9+
10+
env:
11+
REGISTRY: ghcr.io
12+
IMAGE_NAME_PREFIX: ${{ github.repository }}
13+
14+
jobs:
15+
detect-affected:
16+
runs-on: ubuntu-latest
17+
outputs:
18+
matrix: ${{ steps.set-matrix.outputs.matrix }}
19+
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
with:
24+
ref: ${{ github.event.pull_request.head.sha }}
25+
# ref: ${{ github.head_ref }}
26+
27+
- name: Setup pnpm
28+
uses: pnpm/action-setup@v4
29+
with:
30+
version: 10.7
31+
32+
- name: Setup Node.js
33+
uses: actions/setup-node@v4
34+
with:
35+
node-version: 22
36+
cache: "pnpm"
37+
38+
- name: Setup GoLang
39+
uses: actions/setup-go@v5
40+
with:
41+
go-version-file: "go.work"
42+
43+
- name: Install dependencies
44+
run: pnpm install --frozen-lockfile
45+
46+
- name: Get affected projects
47+
id: get-affected
48+
run: |
49+
set -euo pipefail
50+
51+
AFFECTED_PROJECTS=$(npx nx show projects --affected --projects "apps/*" --json)
52+
53+
echo "affected_projects=$AFFECTED_PROJECTS" >> $GITHUB_OUTPUT
54+
55+
- name: Set matrix
56+
id: set-matrix
57+
run: |
58+
set -euo pipefail
59+
60+
cat > docker-config.json << EOL
61+
{
62+
"ui": {
63+
"dockerfile": "apps/ui/Dockerfile",
64+
"context": "."
65+
},
66+
"api": {
67+
"dockerfile": "apps/api/build/package/Dockerfile",
68+
"context": "."
69+
}
70+
}
71+
EOL
72+
73+
AFFECTED_PROJECTS='${{ steps.get-affected.outputs.affected_projects }}'
74+
MATRIX=$(echo $AFFECTED_PROJECTS | jq -c --slurpfile config docker-config.json '
75+
. as $projects |
76+
$config[0] as $dockerConfigs |
77+
{
78+
project: $projects | map(select(. as $p | $dockerConfigs[$p] != null)) | map({
79+
name: .,
80+
dockerfile: $dockerConfigs[.].dockerfile,
81+
context: $dockerConfigs[.].context
82+
})
83+
}
84+
')
85+
86+
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT
87+
88+
build-docker:
89+
needs: detect-affected
90+
if: ${{ needs.detect-affected.outputs.matrix != '{"project":[]}' }}
91+
runs-on: ubuntu-latest
92+
strategy:
93+
matrix: ${{ fromJson(needs.detect-affected.outputs.matrix) }}
94+
fail-fast: false
95+
permissions:
96+
contents: read
97+
packages: write
98+
id-token: write
99+
100+
steps:
101+
- name: Checkout repository
102+
uses: actions/checkout@v4
103+
104+
- name: Install cosign
105+
if: github.event_name != 'pull_request'
106+
uses: sigstore/cosign-installer@v3
107+
with:
108+
cosign-release: 'v2.2.4'
109+
110+
- name: Set up Docker Buildx
111+
uses: docker/setup-buildx-action@v3
112+
113+
- name: Log into registry ${{ env.REGISTRY }}
114+
if: github.event_name != 'pull_request'
115+
uses: docker/login-action@v3
116+
with:
117+
registry: ${{ env.REGISTRY }}
118+
username: ${{ github.actor }}
119+
password: ${{ secrets.GITHUB_TOKEN }}
120+
121+
- name: Extract Docker metadata
122+
id: meta
123+
uses: docker/metadata-action@v5
124+
with:
125+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_PREFIX }}-${{ matrix.project.name }}
126+
tags: |
127+
type=ref,event=branch
128+
type=ref,event=pr
129+
type=sha
130+
type=semver,pattern={{version}}
131+
type=semver,pattern={{major}}.{{minor}}
132+
type=semver,pattern={{major}}
133+
134+
- name: Build and push Docker image
135+
id: build-and-push
136+
uses: docker/build-push-action@v6
137+
with:
138+
context: ${{ matrix.project.context }}
139+
file: ${{ matrix.project.dockerfile }}
140+
push: ${{ github.event_name != 'pull_request' }}
141+
tags: ${{ steps.meta.outputs.tags }}
142+
labels: ${{ steps.meta.outputs.labels }}
143+
cache-from: type=gha
144+
cache-to: type=gha,mode=max
145+
146+
- name: Sign the published Docker image
147+
if: ${{ github.event_name != 'pull_request' }}
148+
env:
149+
TAGS: ${{ steps.meta.outputs.tags }}
150+
DIGEST: ${{ steps.build-and-push.outputs.digest }}
151+
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}

0 commit comments

Comments
 (0)