Skip to content

Commit 7067149

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

File tree

1 file changed

+150
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)