Create Infrastructure Pipeline #13
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: Create Infrastructure Pipeline | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| apply_changes: | |
| description: "Apply Terraform Changes?" | |
| required: true | |
| type: boolean | |
| default: false | |
| permissions: | |
| id-token: write # Needed for Azure CLI Login | |
| env: | |
| TF_ENV_VARS_FILE_PATH: "environments/dev.tfvars" # This would need to be environment-specific | |
| TF_PLAN_FILE_PATH: "/tmp/tf_plan" | |
| jobs: | |
| run-tf: | |
| name: "Run Terraform Code" | |
| runs-on: ubuntu-latest | |
| environment: development # This would need to be environment-specific | |
| defaults: | |
| run: | |
| working-directory: ./terraform | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| - name: Azure Login | |
| uses: azure/login@v2 | |
| with: | |
| client-id: ${{ secrets.INFRA_ARM_CLIENT_ID }} | |
| tenant-id: ${{ secrets.ARM_TENANT_ID }} | |
| subscription-id: ${{ secrets.ARM_SUBSCRIPTION_ID }} | |
| - name: Init Terraform CLI | |
| uses: hashicorp/setup-terraform@v3 | |
| with: | |
| terraform_version: "1.12.1" | |
| - name: Terraform Init | |
| run: | | |
| terraform init \ | |
| --backend-config=client_id=${{ secrets.INFRA_ARM_CLIENT_ID }} \ | |
| --backend-config=tenant_id=${{ secrets.ARM_TENANT_ID }} | |
| - name: Get Image Version | |
| id: get_image_version | |
| run: | | |
| IMAGE_VERSION=$(grep '^version = ' $GITHUB_WORKSPACE/pyproject.toml | awk -F" " {'print $3'} | sed s/\"//g) | |
| echo "image_version=$IMAGE_VERSION" >> $GITHUB_OUTPUT | |
| - name: Terraform Plan | |
| run: | | |
| terraform plan \ | |
| -var-file=$TF_ENV_VARS_FILE_PATH \ | |
| -var 'dockerhub_username=${{ secrets.DOCKERHUB_USERNAME }}' \ | |
| -var 'dockerhub_password=${{ secrets.DOCKERHUB_TOKEN }}' \ | |
| -var 'container_image_version=${{ steps.get_image_version.outputs.image_version }}' \ | |
| -out=$TF_PLAN_FILE_PATH | |
| - name: Terraform Apply | |
| if: ${{ github.event.inputs.apply_changes == 'true' }} | |
| run: | | |
| terraform apply \ | |
| -var-file=$TF_ENV_VARS_FILE_PATH \ | |
| -var 'dockerhub_username=${{ secrets.DOCKERHUB_USERNAME }}' \ | |
| -var 'dockerhub_password=${{ secrets.DOCKERHUB_TOKEN }}' \ | |
| -var 'container_image_version=${{ steps.get_image_version.outputs.image_version }}' \ | |
| -auto-approve \ | |
| $TF_PLAN_FILE_PATH | |
| - name: Terraform Output | |
| if: ${{ github.event.inputs.apply_changes == 'true' }} | |
| id: tf_output | |
| run: | | |
| echo "container_ip=$(terraform output -raw container_ipv4_address)" >> $GITHUB_OUTPUT | |
| - name: Healthcheck | |
| if: ${{ github.event.inputs.apply_changes == 'true' }} | |
| run: | | |
| NUM_TRIES=10 | |
| BACKOFF_SEC=5 | |
| echo "Checking health of container at IP '${{ steps.tf_output.outputs.container_ip }}'" | |
| for try in $(seq $NUM_TRIES); do | |
| sleep $BACKOFF_SEC | |
| HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" ${{ steps.tf_output.outputs.container_ip }}:8080/healthcheck) | |
| if [ "$HTTP_CODE" -eq 200 ]; then | |
| echo "Healthcheck successful" | |
| exit 0 | |
| else | |
| echo "Try $try failed - Got HTTP code $HTTP_CODE" | |
| fi | |
| done | |
| echo "Could not verify health of container" | |
| exit 1 |