|
| 1 | +name: Terraform Template |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + environment: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + description: "Specifies the environment of the deployment." |
| 10 | + config: |
| 11 | + required: true |
| 12 | + type: string |
| 13 | + description: "Specifies the configuration folder for the deployment." |
| 14 | + terraform_version: |
| 15 | + required: true |
| 16 | + type: string |
| 17 | + description: "Specifies the terraform version." |
| 18 | + node_version: |
| 19 | + required: true |
| 20 | + type: number |
| 21 | + description: "Specifies the node version." |
| 22 | + working_directory: |
| 23 | + required: true |
| 24 | + type: string |
| 25 | + description: "Specifies the working directory." |
| 26 | + tenant_id: |
| 27 | + required: true |
| 28 | + type: string |
| 29 | + description: "Specifies the tenant id of the deployment." |
| 30 | + subscription_id: |
| 31 | + required: true |
| 32 | + type: string |
| 33 | + description: "Specifies the subscription id of the deployment." |
| 34 | + secrets: |
| 35 | + CLIENT_ID: |
| 36 | + required: true |
| 37 | + description: "Specifies the client id." |
| 38 | + |
| 39 | +permissions: |
| 40 | + id-token: write |
| 41 | + contents: read |
| 42 | + pull-requests: write |
| 43 | + |
| 44 | +jobs: |
| 45 | + lint: |
| 46 | + name: Terraform Lint |
| 47 | + runs-on: [ubuntu-latest] |
| 48 | + continue-on-error: false |
| 49 | + |
| 50 | + steps: |
| 51 | + # Setup Terraform |
| 52 | + - name: Setup Terraform |
| 53 | + id: terraform_setup |
| 54 | + uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 |
| 55 | + with: |
| 56 | + terraform_version: ${{ inputs.terraform_version }} |
| 57 | + terraform_wrapper: true |
| 58 | + |
| 59 | + # Check Out Repository |
| 60 | + - name: Check Out Repository |
| 61 | + id: checkout_repository |
| 62 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 63 | + |
| 64 | + # Terraform Format |
| 65 | + - name: Terraform Format |
| 66 | + id: terraform_format |
| 67 | + working-directory: ${{ inputs.working_directory }} |
| 68 | + run: | |
| 69 | + terraform fmt -check -recursive |
| 70 | +
|
| 71 | + # Add Pull Request Comment |
| 72 | + - name: Add Pull Request Comment |
| 73 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 74 | + id: pr_comment |
| 75 | + if: github.event_name == 'pull_request' |
| 76 | + with: |
| 77 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + script: | |
| 79 | + const output = `#### Terraform Lint Results |
| 80 | + * Terraform Version 📎\`${{ inputs.terraform_version }}\` |
| 81 | + * Working Directory 📂\`${{ inputs.working_directory }}\` |
| 82 | + * Terraform Format and Style 🖌\`${{ steps.terraform_format.outcome }}\``; |
| 83 | +
|
| 84 | + github.rest.issues.createComment({ |
| 85 | + issue_number: context.issue.number, |
| 86 | + owner: context.repo.owner, |
| 87 | + repo: context.repo.repo, |
| 88 | + body: output |
| 89 | + }) |
| 90 | +
|
| 91 | + plan: |
| 92 | + name: Terraform Plan |
| 93 | + runs-on: [self-hosted] |
| 94 | + continue-on-error: false |
| 95 | + environment: ${{ inputs.environment }} |
| 96 | + needs: [lint] |
| 97 | + concurrency: |
| 98 | + group: terraform-${{ inputs.config }}-${{ inputs.environment }} |
| 99 | + cancel-in-progress: false |
| 100 | + |
| 101 | + env: |
| 102 | + ARM_TENANT_ID: ${{ inputs.tenant_id }} |
| 103 | + ARM_SUBSCRIPTION_ID: ${{ inputs.subscription_id }} |
| 104 | + ARM_CLIENT_ID: ${{ secrets.CLIENT_ID }} |
| 105 | + ARM_USE_OIDC: true |
| 106 | + |
| 107 | + steps: |
| 108 | + # Setup Node |
| 109 | + - name: Setup Node |
| 110 | + id: node_setup |
| 111 | + uses: actions/setup-node@2028fbc5c25fe9cf00d9f06a71cc4710d4507903 # v6.0.0 |
| 112 | + with: |
| 113 | + node-version: ${{ inputs.node_version }} |
| 114 | + |
| 115 | + # Setup Terraform |
| 116 | + - name: Setup Terraform |
| 117 | + id: terraform_setup |
| 118 | + uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 |
| 119 | + with: |
| 120 | + terraform_version: ${{ inputs.terraform_version }} |
| 121 | + terraform_wrapper: true |
| 122 | + |
| 123 | + # Check Out Repository |
| 124 | + - name: Check Out Repository |
| 125 | + id: checkout_repository |
| 126 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 127 | + |
| 128 | + # Terraform Init |
| 129 | + - name: Terraform Init |
| 130 | + id: terraform_init |
| 131 | + working-directory: ${{ inputs.working_directory }} |
| 132 | + run: | |
| 133 | + terraform init -backend-config=../../config/${CONFIG}/azurerm.tfbackend |
| 134 | + env: |
| 135 | + CONFIG: ${{ inputs.config }} |
| 136 | + |
| 137 | + # Terraform Validate |
| 138 | + - name: Terraform Validate |
| 139 | + id: terraform_validate |
| 140 | + working-directory: ${{ inputs.working_directory }} |
| 141 | + run: | |
| 142 | + terraform validate |
| 143 | +
|
| 144 | + # Terraform Plan |
| 145 | + - name: Terraform Plan |
| 146 | + id: terraform_plan |
| 147 | + working-directory: ${{ inputs.working_directory }} |
| 148 | + run: | |
| 149 | + terraform plan -var-file="../../config/${CONFIG}/vars.tfvars" -input=false |
| 150 | + env: |
| 151 | + CONFIG: ${{ inputs.config }} |
| 152 | + |
| 153 | + # Add Pull Request Comment |
| 154 | + - name: Add Pull Request Comment |
| 155 | + id: pr_comment |
| 156 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 157 | + if: github.event_name == 'pull_request' |
| 158 | + continue-on-error: true |
| 159 | + env: |
| 160 | + PLAN: "terraform\n${{ steps.terraform_plan.outputs.stdout }}" |
| 161 | + with: |
| 162 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 163 | + script: | |
| 164 | + const output = `#### Terraform Validation & Plan Results |
| 165 | + * Terraform Version 📎\`${{ inputs.terraform_version }}\` |
| 166 | + * Working Directory 📂\`${{ inputs.working_directory }}\` |
| 167 | + * Terraform Initialization ⚙️\`${{ steps.terraform_init.outcome }}\` |
| 168 | + * Terraform Validation 🤖\`${{ steps.terraform_validate.outcome }}\` |
| 169 | + * Terraform Plan 📖\`${{ steps.terraform_plan.outcome }}\` |
| 170 | +
|
| 171 | + <details><summary>Show Plan</summary> |
| 172 | +
|
| 173 | + \`\`\`\n |
| 174 | + ${process.env.PLAN} |
| 175 | + \`\`\` |
| 176 | +
|
| 177 | + </details>`; |
| 178 | +
|
| 179 | + github.rest.issues.createComment({ |
| 180 | + issue_number: context.issue.number, |
| 181 | + owner: context.repo.owner, |
| 182 | + repo: context.repo.repo, |
| 183 | + body: output |
| 184 | + }) |
| 185 | +
|
| 186 | + apply: |
| 187 | + name: Terraform Apply |
| 188 | + runs-on: [self-hosted] |
| 189 | + continue-on-error: false |
| 190 | + environment: ${{ inputs.environment }} |
| 191 | + # if: github.event_name == 'push' || github.event_name == 'release' |
| 192 | + needs: [plan] |
| 193 | + concurrency: |
| 194 | + group: terraform-${{ inputs.config }}-${{ inputs.environment }} |
| 195 | + cancel-in-progress: false |
| 196 | + |
| 197 | + env: |
| 198 | + ARM_TENANT_ID: ${{ inputs.tenant_id }} |
| 199 | + ARM_SUBSCRIPTION_ID: ${{ inputs.subscription_id }} |
| 200 | + ARM_CLIENT_ID: ${{ secrets.CLIENT_ID }} |
| 201 | + ARM_USE_OIDC: true |
| 202 | + |
| 203 | + steps: |
| 204 | + # Setup Node |
| 205 | + - name: Setup Node |
| 206 | + id: node_setup |
| 207 | + uses: actions/setup-node@2028fbc5c25fe9cf00d9f06a71cc4710d4507903 # v6.0.0 |
| 208 | + with: |
| 209 | + node-version: ${{ inputs.node_version }} |
| 210 | + |
| 211 | + # Setup Terraform |
| 212 | + - name: Setup Terraform |
| 213 | + id: terraform_setup |
| 214 | + uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2 |
| 215 | + with: |
| 216 | + terraform_version: ${{ inputs.terraform_version }} |
| 217 | + terraform_wrapper: true |
| 218 | + |
| 219 | + # Check Out Repository |
| 220 | + - name: Check Out Repository |
| 221 | + id: checkout_repository |
| 222 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 223 | + |
| 224 | + # Terraform Init |
| 225 | + - name: Terraform Init |
| 226 | + working-directory: ${{ inputs.working_directory }} |
| 227 | + run: | |
| 228 | + terraform init -backend-config=../../config/${CONFIG}/azurerm.tfbackend |
| 229 | + env: |
| 230 | + CONFIG: ${{ inputs.config }} |
| 231 | + |
| 232 | + # Terraform Apply |
| 233 | + - name: Terraform Apply |
| 234 | + working-directory: ${{ inputs.working_directory }} |
| 235 | + run: | |
| 236 | + terraform apply -var-file="../../config/${CONFIG}/vars.tfvars" -auto-approve -input=false |
| 237 | + env: |
| 238 | + CONFIG: ${{ inputs.config }} |
0 commit comments