Build, Push Docker Image & Update Helm Chart #5
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 Image | |
| runs-on: ubuntu-latest | |
| steps: | |
| # Step 1: Checkout the source code | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| # Step 2: Read the version from the package.json file | |
| - name: Extract version from package.json | |
| id: extract_version | |
| run: | | |
| VERSION=$(jq -r .version package.json) | |
| echo "VERSION=$VERSION" | |
| echo "version=$VERSION" >> $GITHUB_ENV | |
| echo "$VERSION" > version.txt | |
| # Step 3: Log in to DockerHub | |
| - name: Log in to DockerHub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| # Step 4: Build and push Docker image | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ secrets.DOCKER_USERNAME }}/prognose-frontend:${{ env.version }} | |
| # Step 5: Log out of DockerHub | |
| - 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-frontend-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-frontend-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 frontend image tag in values.yaml | |
| run: | | |
| cd helm-chart-repo | |
| # Get current tag value | |
| CURRENT_TAG=$(yq eval ".fe.image.tag" values.yaml) | |
| echo "Current Frontend tag: ${CURRENT_TAG}" | |
| echo "New Frontend tag: ${{ env.version }}" | |
| # Update the tag in values.yaml | |
| yq eval ".fe.image.tag = \"${{ env.version }}\"" -i values.yaml | |
| # Verify the update | |
| NEW_TAG=$(yq eval ".fe.image.tag" values.yaml) | |
| echo "Updated Frontend tag: ${NEW_TAG}" | |
| - name: Commit and push Helm chart changes | |
| run: | | |
| cd helm-chart-repo | |
| # Check if there are changes | |
| if git diff --quiet; then | |
| echo "No changes detected in values.yaml" | |
| exit 0 | |
| fi | |
| # Configure git | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| # Add and commit changes | |
| git add values.yaml | |
| git commit -m "chore: update Frontend image tag to ${{ env.version }}" | |
| # Push changes | |
| git push |