CI/CD Workflow #559
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: CI/CD Workflow | |
| on: | |
| schedule: | |
| - cron: '0 6 * * *' | |
| push: | |
| branches: | |
| - main | |
| jobs: | |
| versions: | |
| name: Check Versions | |
| runs-on: ubuntu-latest | |
| outputs: | |
| new-tag: ${{ steps.variables.outputs.new-tag }} | |
| tf-version: ${{ steps.variables.outputs.tf-version }} | |
| tg-version: ${{ steps.variables.outputs.tg-version }} | |
| update-type: ${{ steps.variables.outputs.update-type }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 1 | |
| - name: Check for Terraform updates | |
| id: tf-version | |
| run: | | |
| CURRENT_VERSION=$(grep -oP '(?<=TERRAFORM_VERSION=).*' Dockerfile) | |
| LATEST_VERSION=$(curl --silent "https://checkpoint-api.hashicorp.com/v1/check/terraform?current_version=0.0.0" | grep -Po '(?<=current_version":")[^"]*') | |
| if [[ $LATEST_VERSION == v* ]]; then | |
| # Remove the v from the version number | |
| LATEST_VERSION=${LATEST_VERSION:1} | |
| fi | |
| if [ "$CURRENT_VERSION" != "$LATEST_VERSION" ]; then | |
| echo "NEW_TF_VERSION=$LATEST_VERSION" >> $GITHUB_ENV | |
| echo >&2 "New Terraform version found: $LATEST_VERSION" | |
| fi | |
| echo "TF_VERSION=$CURRENT_VERSION" >> $GITHUB_ENV | |
| - name: Check for Terragrunt updates | |
| id: tg-version | |
| run: | | |
| CURRENT_VERSION=$(grep -oP '(?<=TERRAGRUNT_VERSION=).*' Dockerfile) | |
| LATEST_VERSION=$(curl --silent "https://api.github.com/repos/gruntwork-io/terragrunt/releases/latest" | jq -r ".tag_name" | cut -c 2-) | |
| if [ "$CURRENT_VERSION" != "$LATEST_VERSION" ]; then | |
| echo "NEW_TG_VERSION=$LATEST_VERSION" >> $GITHUB_ENV | |
| echo >&2 "New Terragrunt version found: $LATEST_VERSION" | |
| fi | |
| echo "TG_VERSION=$CURRENT_VERSION" >> $GITHUB_ENV | |
| - name: Compare version and extract update type | |
| id: compare-version | |
| run: | | |
| compare_versions() { | |
| IFS='.' read -r old_major old_minor old_patch <<< "$1" | |
| IFS='.' read -r new_major new_minor new_patch <<< "$2" | |
| if (( new_major > old_major )); then echo "major" | |
| elif (( new_minor > old_minor )); then echo "minor" | |
| elif (( new_patch > old_patch )); then echo "patch" | |
| else echo "" | |
| fi | |
| } | |
| TF_TYPE=$(compare_versions "${TF_VERSION}" "${NEW_TF_VERSION:-$TF_VERSION}") | |
| TG_TYPE=$(compare_versions "${TG_VERSION}" "${NEW_TG_VERSION:-$TG_VERSION}") | |
| if [[ "$TF_TYPE" == "major" || "$TG_TYPE" == "major" ]]; then UPDATE_TYPE="major" | |
| elif [[ "$TF_TYPE" == "minor" || "$TG_TYPE" == "minor" ]]; then UPDATE_TYPE="minor" | |
| elif [[ "$TF_TYPE" == "patch" || "$TG_TYPE" == "patch" ]]; then UPDATE_TYPE="patch" | |
| else UPDATE_TYPE="none" | |
| fi | |
| echo "TF_UPDATE_TYPE=$TF_TYPE" >> $GITHUB_ENV | |
| echo "TG_UPDATE_TYPE=$TG_TYPE" >> $GITHUB_ENV | |
| echo "UPDATE_TYPE=$UPDATE_TYPE" >> $GITHUB_ENV | |
| env: | |
| TF_VERSION: ${{ env.TF_VERSION }} | |
| NEW_TF_VERSION: ${{ env.NEW_TF_VERSION }} | |
| TG_VERSION: ${{ env.TG_VERSION }} | |
| NEW_TG_VERSION: ${{ env.NEW_TG_VERSION }} | |
| - name: Setting the NEW_TAG variable | |
| id: new-tag | |
| run: | | |
| echo "NEW_TAG=tg-${{ env.NEW_TG_VERSION || env.TG_VERSION }}-tf-${{ env.NEW_TF_VERSION || env.TF_VERSION }}" >> $GITHUB_ENV | |
| - name: Export outputs | |
| id: variables | |
| run: | | |
| echo "tg-version=${{ env.NEW_TG_VERSION || env.TG_VERSION }}" >> $GITHUB_OUTPUT | |
| echo "tf-version=${{ env.NEW_TF_VERSION || env.TF_VERSION }}" >> $GITHUB_OUTPUT | |
| echo "new-tag=${{ env.NEW_TAG }}" >> $GITHUB_OUTPUT | |
| echo "update-type=${{ env.UPDATE_TYPE }}" >> $GITHUB_OUTPUT | |
| - name: Publish Summary | |
| run: | | |
| echo "## Version summary" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| if [[ "${{ env.UPDATE_TYPE }}" != "none" ]]; then | |
| echo "A new **${{ env.UPDATE_TYPE }}** version has been found!" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| fi | |
| echo "| | Old | New | Type |" >> $GITHUB_STEP_SUMMARY | |
| echo "| --- | :-: | :-: | :-: |" >> $GITHUB_STEP_SUMMARY | |
| echo "| Terraform | ${{ env.TF_VERSION }} | ${{ env.NEW_TF_VERSION }} | ${{ env.TF_UPDATE_TYPE }} |" >> $GITHUB_STEP_SUMMARY | |
| echo "| Terragrunt | ${{ env.TG_VERSION }} | ${{ env.NEW_TG_VERSION }} | ${{ env.TG_UPDATE_TYPE }} |" >> $GITHUB_STEP_SUMMARY | |
| echo "| Docker Tag | tg-${{ env.TG_VERSION }}-tf-${{ env.TF_VERSION }} | ${{ env.NEW_TAG }} | |" >> $GITHUB_STEP_SUMMARY | |
| ci: | |
| name: Build Docker Image for CI | |
| needs: versions | |
| if: needs.versions.outputs.new-tag && needs.versions.outputs.tf-version && needs.versions.outputs.tg-version | |
| uses: ./.github/workflows/docker-build.yaml | |
| with: | |
| push_image: false | |
| tf_version: ${{ needs.versions.outputs.tf-version }} | |
| tg_version: ${{ needs.versions.outputs.tg-version }} | |
| tags: ${{ vars.DOCKER_REPOSITORY_NAME }}:0.0.0-${{ needs.versions.outputs.new-tag }} | |
| secrets: | |
| DOCKER_USERNAME: '' | |
| DOCKER_TOKEN: '' | |
| git: | |
| name: Update Git | |
| runs-on: ubuntu-latest | |
| needs: [ versions, ci ] | |
| if: needs.versions.outputs.new-tag && needs.versions.outputs.tf-version && needs.versions.outputs.tg-version && needs.versions.outputs.update-type | |
| environment: main | |
| outputs: | |
| new-tag: ${{ steps.release.outputs.new-tag }} | |
| steps: | |
| - uses: actions/create-github-app-token@v1.11.0 | |
| id: app-token | |
| with: | |
| app-id: ${{ vars.GHA_BOT_APP_ID }} | |
| private-key: ${{ secrets.GHA_BOT_PRIVATE_KEY }} | |
| repositories: | | |
| docker-terragrunt | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 1 | |
| token: ${{ steps.app-token.outputs.token }} | |
| - name: Set up Git | |
| run: | | |
| git config --global user.name 'github-actions[bot]' | |
| git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
| - name: Update Dockerfile with new versions | |
| run: | | |
| sed -i "s/^ARG TERRAFORM_VERSION=.*/ARG TERRAFORM_VERSION=${{ needs.versions.outputs.tf-version }}/" Dockerfile | |
| sed -i "s/^ARG TERRAGRUNT_VERSION=.*/ARG TERRAGRUNT_VERSION=${{ needs.versions.outputs.tg-version }}/" Dockerfile | |
| - name: Commit updated Dockerfile | |
| run: | | |
| if [[ "${{ needs.versions.outputs.update-type }}" == "major" ]]; then | |
| BC=$'\n\nBREAKING CHANGE: this include a new MAJOR version of the tools' | |
| else | |
| BC="" | |
| fi | |
| git checkout main | |
| git add Dockerfile | |
| if ! git diff-index --quiet HEAD -- Dockerfile; then | |
| git commit -m "chore(tool-versions-${{ needs.versions.outputs.update-type }}): Update Dockerfile with new Terraform and Terragrunt versions [skip ci]$BC" | |
| git push origin main | |
| fi | |
| - uses: jblab/.github/.github/actions/release@main | |
| id: release | |
| with: | |
| app-id: ${{ vars.GHA_BOT_APP_ID }} | |
| repository: ${{ github.repository }} | |
| app-private-key: ${{ secrets.GHA_BOT_PRIVATE_KEY }} | |
| skip-checkout: true | |
| build: | |
| name: Build and Push Docker Image | |
| needs: [ versions, ci, git ] | |
| if: needs.versions.outputs.new-tag && needs.versions.outputs.tf-version && needs.versions.outputs.tg-version && needs.git.outputs.new-tag | |
| strategy: | |
| matrix: | |
| include: | |
| - target: 'base' | |
| tags: | | |
| ${{ vars.DOCKER_REPOSITORY_NAME }}:${{ needs.git.outputs.new-tag }}-${{ needs.versions.outputs.new-tag }} | |
| ${{ vars.DOCKER_REPOSITORY_NAME }}:${{ needs.git.outputs.new-tag }} | |
| ${{ vars.DOCKER_REPOSITORY_NAME }}:latest | |
| version: ${{ needs.git.outputs.new-tag }} | |
| - target: 'dev' | |
| tags: | | |
| ${{ vars.DOCKER_REPOSITORY_NAME }}:${{ needs.git.outputs.new-tag }}-${{ needs.versions.outputs.new-tag }}-dev-tools | |
| ${{ vars.DOCKER_REPOSITORY_NAME }}:${{ needs.git.outputs.new-tag }}-dev-tools | |
| version: ${{ needs.git.outputs.new-tag }}-dev-tools | |
| - target: 'ado_builder' | |
| tags: | | |
| ${{ vars.DOCKER_REPOSITORY_NAME }}:${{ needs.git.outputs.new-tag }}-${{ needs.versions.outputs.new-tag }}-azdo | |
| ${{ vars.DOCKER_REPOSITORY_NAME }}:${{ needs.git.outputs.new-tag }}-azdo | |
| version: ${{ needs.git.outputs.new-tag }}-azdo | |
| uses: ./.github/workflows/docker-build.yaml | |
| with: | |
| push_image: true | |
| environment: main | |
| version: ${{ matrix.version }} | |
| tf_version: ${{ needs.versions.outputs.tf-version }} | |
| tg_version: ${{ needs.versions.outputs.tg-version }} | |
| target: ${{ matrix.target }} | |
| tags: ${{ matrix.tags }} | |
| secrets: inherit |