Build, Push Docker Image & Update Helm Chart #1
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, Push Docker Image & Update Helm Chart | |
| on: | |
| workflow_dispatch: | |
| jobs: | |
| build-and-push: | |
| name: (CI) Build and Push Docker Images | |
| runs-on: ubuntu-latest | |
| steps: | |
| # Step 1: Install xmllint (part of libxml2-utils) | |
| - name: Install xmllint | |
| run: sudo apt-get update && sudo apt-get install -y libxml2-utils | |
| # Step 2: Checkout the source code | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| # Step 3: Read the version from the pom.xml file | |
| - name: Extract version from pom.xml | |
| id: extract_version | |
| run: | | |
| VERSION=$(xmllint --xpath "//*[local-name()='project']/*[local-name()='version']/text()" pom.xml) | |
| echo "VERSION=$VERSION" | |
| echo "version=$VERSION" >> $GITHUB_ENV | |
| echo "$VERSION" > version.txt | |
| # Step 4: Log in to DockerHub | |
| - name: Log in to DockerHub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| # Step 5: Build and push Docker image for DB_TYPE=postgres | |
| - name: Build and push Docker image (Postgres) | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ secrets.DOCKER_USERNAME }}/prognose-backend:${{ env.version }}-postgres | |
| build-args: | | |
| DB_TYPE=postgres | |
| # Step 6: Build and push Docker image for DB_TYPE=oracle | |
| - name: Build and push Docker image (Oracle) | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ secrets.DOCKER_USERNAME }}/prognose-backend:${{ env.version }}-oracle | |
| build-args: | | |
| DB_TYPE=oracle | |
| - name: Log out of DockerHub | |
| run: docker logout | |
| - name: Create version file | |
| run: echo "${{ env.version }}" > version.txt | |
| - name: Upload version artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: prognose-backend-version | |
| path: version.txt | |
| update-helm-chart: | |
| name: (CD) Update Helm Chart Image Tag | |
| runs-on: ubuntu-latest | |
| needs: build-and-push | |
| steps: | |
| - name: Download version artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: prognose-backend-version | |
| - name: Read version | |
| id: read_version | |
| run: | | |
| VERSION=$(cat version.txt) | |
| echo "version=$VERSION" >> $GITHUB_ENV | |
| - name: Checkout Helm chart repository | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: netgroup-polito/prognose-helm-chart | |
| token: ${{ secrets.CHART_REPO_TOKEN }} | |
| path: helm-chart-repo | |
| - name: Install yq | |
| run: | | |
| sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 | |
| sudo chmod +x /usr/local/bin/yq | |
| - name: Update backend image tag in values.yaml | |
| run: | | |
| cd helm-chart-repo | |
| CURRENT_TAG=$(yq eval ".be.image.tag" values.yaml) | |
| if [[ "$CURRENT_TAG" =~ -([a-zA-Z]+)$ ]]; then | |
| DB_SUFFIX="-${BASH_REMATCH[1]}" | |
| else | |
| DB_SUFFIX="-postgres" | |
| fi | |
| NEW_TAG="${{ env.version }}${DB_SUFFIX}" | |
| yq eval ".be.image.tag = \"${NEW_TAG}\"" -i values.yaml | |
| - name: Commit and push Helm chart changes | |
| run: | | |
| cd helm-chart-repo | |
| if git diff --quiet; then | |
| echo "No changes detected in values.yaml" | |
| exit 0 | |
| fi | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| git add values.yaml | |
| git commit -m "chore: update Backend image tag to ${{ env.version }}" | |
| git push |