-
Notifications
You must be signed in to change notification settings - Fork 15
159 lines (141 loc) · 5.18 KB
/
build-and-push.yml
File metadata and controls
159 lines (141 loc) · 5.18 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
name: Build and Push Docker Images
on:
push:
# branches:
# - main
tags:
- "v*"
# pull_request:
# branches:
# - main
env:
DOCKERHUB_USERNAME: charmy1220
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
REGISTRY: docker.io
jobs:
build-and-push:
runs-on: ubuntu-latest
timeout-minutes: 60
strategy:
matrix:
service:
- name: backend
dockerfile: ./backend/Dockerfile
context: ./backend
image: lmeterx-be
- name: st_engine
dockerfile: ./st_engine/Dockerfile
context: ./st_engine
image: lmeterx-eng
- name: frontend
dockerfile: ./frontend/Dockerfile
context: ./frontend
image: lmeterx-fe
- name: mysql
dockerfile: ./mysql/Dockerfile
context: .
image: lmeterx-mysql
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ env.DOCKERHUB_USERNAME }}
password: ${{ env.DOCKERHUB_TOKEN }}
- name: Extract tag name
id: extract_tag
run: |
TAG_NAME=${GITHUB_REF#refs/tags/}
echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT
echo "Extracted tag: $TAG_NAME"
- name: Build and push Docker image
uses: docker/build-push-action@v5
timeout-minutes: 30
with:
context: ${{ matrix.service.context }}
file: ${{ matrix.service.dockerfile }}
push: true
tags: |
${{ env.REGISTRY }}/${{ env.DOCKERHUB_USERNAME }}/${{ matrix.service.image }}:${{ steps.extract_tag.outputs.tag_name }}
${{ env.REGISTRY }}/${{ env.DOCKERHUB_USERNAME }}/${{ matrix.service.image }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
provenance: false
sbom: false
create-release:
needs: build-and-push
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')
permissions:
contents: write
discussions: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Extract tag name
id: extract_tag
run: |
TAG_NAME=${GITHUB_REF#refs/tags/}
echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT
echo "Current tag: $TAG_NAME"
- name: Generate changelog
id: changelog
run: |
CURRENT_TAG=${{ steps.extract_tag.outputs.tag_name }}
PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD~1 2>/dev/null || echo "")
echo "## 🚀 Release $CURRENT_TAG" > CHANGELOG.md
echo "" >> CHANGELOG.md
echo "### 📦 Docker Images" >> CHANGELOG.md
echo "- \`${{ env.DOCKERHUB_USERNAME }}/lmeterx-be:$CURRENT_TAG\`" >> CHANGELOG.md
echo "- \`${{ env.DOCKERHUB_USERNAME }}/lmeterx-eng:$CURRENT_TAG\`" >> CHANGELOG.md
echo "- \`${{ env.DOCKERHUB_USERNAME }}/lmeterx-fe:$CURRENT_TAG\`" >> CHANGELOG.md
echo "- \`${{ env.DOCKERHUB_USERNAME }}/lmeterx-mysql:$CURRENT_TAG\`" >> CHANGELOG.md
echo "- \`${{ env.DOCKERHUB_USERNAME }}/lmeterx-be:latest\`" >> CHANGELOG.md
echo "- \`${{ env.DOCKERHUB_USERNAME }}/lmeterx-eng:latest\`" >> CHANGELOG.md
echo "- \`${{ env.DOCKERHUB_USERNAME }}/lmeterx-fe:latest\`" >> CHANGELOG.md
echo "- \`${{ env.DOCKERHUB_USERNAME }}/lmeterx-mysql:latest\`" >> CHANGELOG.md
echo "" >> CHANGELOG.md
if [ -n "$PREVIOUS_TAG" ]; then
echo "### 📝 Changes since $PREVIOUS_TAG" >> CHANGELOG.md
git log --pretty=format:"- %s (%h)" $PREVIOUS_TAG..$CURRENT_TAG >> CHANGELOG.md
else
echo "### 📝 Initial Release" >> CHANGELOG.md
echo "This is the initial release of LMeterX." >> CHANGELOG.md
fi
- name: Create GitHub Release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ steps.extract_tag.outputs.tag_name }}
name: Release ${{ steps.extract_tag.outputs.tag_name }}
body_path: CHANGELOG.md
draft: false
prerelease: false
generate_release_notes: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
notify:
needs: [build-and-push, create-release]
runs-on: ubuntu-latest
if: always()
steps:
- name: Notify build status
run: |
if [ "${{ needs.build-and-push.result }}" == "success" ]; then
echo "✅ Docker images built and pushed successfully!"
echo "✅ Both versioned and latest tags have been updated!"
else
echo "❌ Docker image build failed!"
exit 1
fi
if [ "${{ needs.create-release.result }}" == "success" ] || [ "${{ needs.create-release.result }}" == "skipped" ]; then
echo "✅ Release process completed successfully!"
else
echo "❌ Release process failed!"
fi