update ci #3
Workflow file for this run
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 | |
| 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 }} | |
| 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 "" >> 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 }} | |
| - name: Update docker-compose.yml | |
| run: | | |
| CURRENT_TAG=${{ steps.extract_tag.outputs.tag_name }} | |
| # 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 "[email protected]" | |
| 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.extract_tag.outputs.tag_name }} 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 |