Skip to content

Commit 0227632

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

File tree

1 file changed

+148
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)