|
| 1 | +name: Deploy monitoring stack |
| 2 | +run-name: Deploy monitoring stack for ${{ inputs.environment }} |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + environment: |
| 8 | + description: Deployment environment |
| 9 | + required: true |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - development |
| 13 | + - production |
| 14 | + git_ref_to_deploy: |
| 15 | + description: Git reference to deploy |
| 16 | + required: false |
| 17 | + type: string |
| 18 | + |
| 19 | +permissions: {} |
| 20 | + |
| 21 | +concurrency: |
| 22 | + group: deploy-monitoring-${{ inputs.environment }} |
| 23 | + |
| 24 | +env: |
| 25 | + aws_role: ${{ inputs.environment == 'production' |
| 26 | + && 'arn:aws:iam::820242920762:role/GithubDeployMonitoring' |
| 27 | + || 'arn:aws:iam::393416225559:role/GithubDeployMonitoring' }} |
| 28 | + aws_account_id: ${{ inputs.environment == 'production' |
| 29 | + && '820242920762' || '393416225559' }} |
| 30 | + git_ref_to_deploy: ${{ inputs.git_ref_to_deploy || github.ref_name }} |
| 31 | + |
| 32 | +jobs: |
| 33 | + plan-aws: |
| 34 | + name: Terraform plan (AWS) |
| 35 | + runs-on: ubuntu-latest |
| 36 | + permissions: |
| 37 | + id-token: write |
| 38 | + outputs: |
| 39 | + has_changes: ${{ steps.plan.outputs.has_changes }} |
| 40 | + defaults: |
| 41 | + run: |
| 42 | + working-directory: terraform/monitoring/aws |
| 43 | + steps: |
| 44 | + - name: Checkout code |
| 45 | + uses: actions/checkout@v4 |
| 46 | + with: |
| 47 | + ref: ${{ env.git_ref_to_deploy }} |
| 48 | + - name: Configure AWS Credentials |
| 49 | + uses: aws-actions/configure-aws-credentials@v4 |
| 50 | + with: |
| 51 | + role-to-assume: ${{ env.aws_role }} |
| 52 | + aws-region: eu-west-2 |
| 53 | + - name: Install terraform |
| 54 | + uses: hashicorp/setup-terraform@v3 |
| 55 | + with: |
| 56 | + terraform_version: 1.11.4 |
| 57 | + - name: Terraform Plan |
| 58 | + id: plan |
| 59 | + run: | |
| 60 | + set -e |
| 61 | + terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade |
| 62 | + terraform plan -var-file="env/${{ inputs.environment }}.tfvars" \ |
| 63 | + -out ${{ runner.temp }}/tfplan-aws | tee ${{ runner.temp }}/tf_stdout_aws |
| 64 | + TF_EXIT_CODE=${PIPESTATUS[0]} |
| 65 | + cat ${{ runner.temp }}/tf_stdout_aws |
| 66 | + if [ $TF_EXIT_CODE -eq 1 ]; then |
| 67 | + exit $TF_EXIT_CODE |
| 68 | + fi |
| 69 | +
|
| 70 | + # Check if there are changes to apply |
| 71 | + if grep -q "Your infrastructure matches the configuration." ${{ runner.temp }}/tf_stdout_aws; then |
| 72 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 73 | + echo "No infrastructure changes detected" |
| 74 | + else |
| 75 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 76 | + echo "Infrastructure changes detected" |
| 77 | + fi |
| 78 | + - name: Upload AWS plan artifact |
| 79 | + uses: actions/upload-artifact@v4 |
| 80 | + with: |
| 81 | + name: tfplan_monitoring_aws-${{ inputs.environment }} |
| 82 | + path: ${{ runner.temp }}/tfplan-aws |
| 83 | + |
| 84 | + apply-aws: |
| 85 | + name: Terraform apply (AWS) |
| 86 | + runs-on: ubuntu-latest |
| 87 | + needs: plan-aws |
| 88 | + if: needs.plan-aws.outputs.has_changes == 'true' |
| 89 | + environment: ${{ inputs.environment }} |
| 90 | + permissions: |
| 91 | + id-token: write |
| 92 | + defaults: |
| 93 | + run: |
| 94 | + working-directory: terraform/monitoring/aws |
| 95 | + steps: |
| 96 | + - name: Checkout code |
| 97 | + uses: actions/checkout@v4 |
| 98 | + with: |
| 99 | + ref: ${{ env.git_ref_to_deploy }} |
| 100 | + - name: Configure AWS Credentials |
| 101 | + uses: aws-actions/configure-aws-credentials@v4 |
| 102 | + with: |
| 103 | + role-to-assume: ${{ env.aws_role }} |
| 104 | + aws-region: eu-west-2 |
| 105 | + - name: Download AWS plan artifact |
| 106 | + uses: actions/download-artifact@v4 |
| 107 | + with: |
| 108 | + name: tfplan_monitoring_aws-${{ inputs.environment }} |
| 109 | + path: ${{ runner.temp }} |
| 110 | + - name: Install terraform |
| 111 | + uses: hashicorp/setup-terraform@v3 |
| 112 | + with: |
| 113 | + terraform_version: 1.11.4 |
| 114 | + - name: Apply AWS changes |
| 115 | + run: | |
| 116 | + set -e |
| 117 | + terraform init -backend-config="env/${{ inputs.environment }}-backend.hcl" -upgrade |
| 118 | + terraform apply ${{ runner.temp }}/tfplan-aws |
| 119 | +
|
| 120 | + apply-grafana: |
| 121 | + name: Terraform apply (Grafana) |
| 122 | + runs-on: ubuntu-latest |
| 123 | + needs: [plan-aws, apply-aws] |
| 124 | + if: always() && needs.plan-aws-aws.result == 'success' && (needs.apply-aws.result == 'success' || needs.apply-aws.result == 'skipped') |
| 125 | + permissions: |
| 126 | + id-token: write |
| 127 | + defaults: |
| 128 | + run: |
| 129 | + working-directory: terraform/monitoring |
| 130 | + steps: |
| 131 | + - name: Checkout code |
| 132 | + uses: actions/checkout@v4 |
| 133 | + with: |
| 134 | + ref: ${{ env.git_ref_to_deploy }} |
| 135 | + - name: Configure AWS Credentials |
| 136 | + uses: aws-actions/configure-aws-credentials@v4 |
| 137 | + with: |
| 138 | + role-to-assume: ${{ env.aws_role }} |
| 139 | + aws-region: eu-west-2 |
| 140 | + - name: Install terraform |
| 141 | + uses: hashicorp/setup-terraform@v3 |
| 142 | + with: |
| 143 | + terraform_version: 1.11.4 |
| 144 | + - name: Install dependencies |
| 145 | + run: | |
| 146 | + sudo apt-get update |
| 147 | + sudo apt-get install -y jq uuid-runtime |
| 148 | + - name: Deploy Grafana using tf_grafana.sh |
| 149 | + run: | |
| 150 | + ./tf_grafana.sh ${{ inputs.environment }} plan --plan-file ${{ runner.temp }}/out |
| 151 | + ./tf_grafana.sh ${{ inputs.environment }} apply --plan-file ${{ runner.temp }}/out |
0 commit comments