This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| 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 | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ env.DOCKERHUB_USERNAME }} | |
| password: ${{ env.DOCKERHUB_TOKEN }} | |
| - name: Extract metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.DOCKERHUB_USERNAME }}/${{ matrix.service.image }} | |
| tags: | | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| type=semver,pattern={{major}} | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| - 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: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| 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') | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Generate changelog | |
| id: changelog | |
| run: | | |
| # get current tag and previous tag | |
| CURRENT_TAG=${GITHUB_REF#refs/tags/} | |
| PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD~1 2>/dev/null || echo "") | |
| echo "current_tag=$CURRENT_TAG" >> $GITHUB_OUTPUT | |
| # generate changelog | |
| if [ -n "$PREVIOUS_TAG" ]; then | |
| echo "## 🚀 Changes in $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 "" >> CHANGELOG.md | |
| echo "### 📝 Commits" >> CHANGELOG.md | |
| git log --pretty=format:"- %s (%h)" $PREVIOUS_TAG..$CURRENT_TAG >> CHANGELOG.md | |
| else | |
| 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 "" >> CHANGELOG.md | |
| echo "Initial release" >> CHANGELOG.md | |
| fi | |
| - name: Create GitHub Release | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| tag_name: ${{ steps.changelog.outputs.current_tag }} | |
| name: Release ${{ steps.changelog.outputs.current_tag }} | |
| body_path: CHANGELOG.md | |
| draft: false | |
| prerelease: false | |
| generate_release_notes: true | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Update docker-compose.yml | |
| run: | | |
| CURRENT_TAG=${GITHUB_REF#refs/tags/} | |
| # update docker-compose.yml with new image tags | |
| sed -i "s|${{ env.DOCKERHUB_USERNAME }}/lmeterx-be:.*|${{ env.DOCKERHUB_USERNAME }}/lmeterx-be:$CURRENT_TAG|g" docker-compose.yml | |
| sed -i "s|${{ env.DOCKERHUB_USERNAME }}/lmeterx-eng:.*|${{ env.DOCKERHUB_USERNAME }}/lmeterx-eng:$CURRENT_TAG|g" docker-compose.yml | |
| sed -i "s|${{ env.DOCKERHUB_USERNAME }}/lmeterx-fe:.*|${{ env.DOCKERHUB_USERNAME }}/lmeterx-fe:$CURRENT_TAG|g" docker-compose.yml | |
| - name: Commit updated docker-compose.yml | |
| run: | | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| git add docker-compose.yml | |
| git diff --staged --quiet || git commit -m "Update docker-compose.yml to use ${{ steps.changelog.outputs.current_tag }} images" | |
| git push origin HEAD:main | |
| 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!" | |
| 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 |