-
-
Notifications
You must be signed in to change notification settings - Fork 21
204 lines (172 loc) · 7.15 KB
/
build-docker-image.yml
File metadata and controls
204 lines (172 loc) · 7.15 KB
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
name: Build and Push Docker Images
on:
push:
branches:
- main
paths:
- 'docker-bake.hcl'
workflow_dispatch:
jobs:
detect-changes:
runs-on: ubuntu-latest
outputs:
release: ${{ steps.extract.outputs.release }}
app: ${{ steps.extract.outputs.app }}
registry_user: ${{ steps.extract.outputs.registry_user }}
should_build: ${{ steps.check.outputs.should_build }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Extract variables from docker-bake.hcl
id: extract
run: |
RELEASE=$(grep 'variable "RELEASE"' -A 2 docker-bake.hcl | grep 'default' | sed 's/.*"\(.*\)"/\1/')
APP=$(grep 'variable "APP"' -A 2 docker-bake.hcl | grep 'default' | sed 's/.*"\(.*\)"/\1/')
REGISTRY_USER=$(grep 'variable "REGISTRY_USER"' -A 2 docker-bake.hcl | grep 'default' | sed 's/.*"\(.*\)"/\1/')
echo "release=${RELEASE}" >> $GITHUB_OUTPUT
echo "app=${APP}" >> $GITHUB_OUTPUT
echo "registry_user=${REGISTRY_USER}" >> $GITHUB_OUTPUT
echo "Current RELEASE: ${RELEASE}"
echo "Current APP: ${APP}"
echo "Current REGISTRY_USER: ${REGISTRY_USER}"
- name: Check if RELEASE changed
id: check
run: |
# For manual triggers, always build
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "should_build=true" >> $GITHUB_OUTPUT
echo "Manual trigger - will build"
exit 0
fi
# Check if docker-bake.hcl was modified
git diff HEAD^ HEAD docker-bake.hcl > /tmp/diff.txt
if grep -E '^\+.*variable "RELEASE"' /tmp/diff.txt || \
grep -E '^\+.*default = ' /tmp/diff.txt | grep -E 'RELEASE' -B 2; then
echo "should_build=true" >> $GITHUB_OUTPUT
echo "RELEASE changed - will build"
else
echo "should_build=false" >> $GITHUB_OUTPUT
echo "No RELEASE changes detected - skipping build"
fi
build-and-push:
needs: detect-changes
if: needs.detect-changes.outputs.should_build == 'true'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
images: ${{ steps.list-images.outputs.images }}
images_json: ${{ steps.list-images.outputs.images_json }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Free up disk space
run: |
sudo swapoff -a
sudo rm -rf /swapfile /usr/share/dotnet /usr/local/lib/android /opt/ghc
sudo apt clean
df -h
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: |
image=moby/buildkit:latest
network=host
buildkitd-flags: --debug
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create temporary docker-bake override file
run: |
# Create an override file that forces ghcr.io
cat > docker-bake.override.hcl << 'EOF'
target "default" {
inherits = ["default"]
tags = ["ghcr.io/${{ github.repository_owner }}/${{ needs.detect-changes.outputs.app }}:${{ needs.detect-changes.outputs.release }}"]
}
EOF
echo "Override file created:"
cat docker-bake.override.hcl
- name: Build and push Docker image
run: |
echo "Building and pushing Docker image to ghcr.io..."
# Show what will be built
echo "Configuration:"
docker buildx bake -f docker-bake.hcl -f docker-bake.override.hcl --print default
echo ""
echo "Starting build..."
# Build with both files - override will take precedence for tags
docker buildx bake -f docker-bake.hcl -f docker-bake.override.hcl --push default
echo "Build completed!"
- name: Verify image was pushed
run: |
IMAGE="ghcr.io/${{ github.repository_owner }}/${{ needs.detect-changes.outputs.app }}:${{ needs.detect-changes.outputs.release }}"
echo "Verifying image: $IMAGE"
# Wait for registry to update
sleep 10
# Try to pull the image
docker pull $IMAGE && echo "✅ SUCCESS: Image verified: $IMAGE" || echo "❌ FAILED: Could not pull image: $IMAGE"
- name: List built image
id: list-images
run: |
RELEASE="${{ needs.detect-changes.outputs.release }}"
APP="${{ needs.detect-changes.outputs.app }}"
REGISTRY="ghcr.io"
REGISTRY_USER="${{ github.repository_owner }}"
# Create the image name
IMAGE="${REGISTRY}/${REGISTRY_USER}/${APP}:${RELEASE}"
# Create JSON for easier parsing by third-party tools
IMAGES_JSON=$(echo "[\"${IMAGE}\"]" | jq -c '.')
# Output for GitHub Actions
echo "images<<EOF" >> $GITHUB_OUTPUT
echo "$IMAGE" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "images_json=${IMAGES_JSON}" >> $GITHUB_OUTPUT
# Also write to a file that can be downloaded
echo "$IMAGE" > built-images.txt
echo "$IMAGES_JSON" > built-images.json
- name: Upload image list artifacts
uses: actions/upload-artifact@v4
with:
name: docker-images-list
path: |
built-images.txt
built-images.json
retention-days: 90
- name: Display built image
run: |
echo "Successfully built and pushed the following image:"
cat built-images.txt
echo ""
echo "JSON format:"
cat built-images.json
create-summary:
needs: [detect-changes, build-and-push]
if: needs.detect-changes.outputs.should_build == 'true'
runs-on: ubuntu-latest
steps:
- name: Create job summary
run: |
echo "## Docker Image Built" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Application:** ${{ needs.detect-changes.outputs.app }}" >> $GITHUB_STEP_SUMMARY
echo "**Release:** ${{ needs.detect-changes.outputs.release }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Image Pushed to GitHub Container Registry:" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "${{ needs.build-and-push.outputs.images }}" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### To pull this image:" >> $GITHUB_STEP_SUMMARY
echo '```bash' >> $GITHUB_STEP_SUMMARY
echo "docker pull ${{ needs.build-and-push.outputs.images }}" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Note:** If the image is private, make it public at:" >> $GITHUB_STEP_SUMMARY
echo "https://github.com/${{ github.repository_owner }}?tab=packages" >> $GITHUB_STEP_SUMMARY