|
| 1 | +on: |
| 2 | + workflow_call: |
| 3 | + inputs: |
| 4 | + environment: |
| 5 | + type: string |
| 6 | + required: true |
| 7 | + tf_environment: |
| 8 | + type: string |
| 9 | + required: false |
| 10 | + dir: |
| 11 | + type: string |
| 12 | + required: true |
| 13 | + env_vars: |
| 14 | + type: string |
| 15 | + required: false |
| 16 | + description: List of environment variables to set up, given in env=value format. |
| 17 | + |
| 18 | +env: |
| 19 | + ARM_SUBSCRIPTION_ID: ${{ vars.ARM_SUBSCRIPTION_ID }} |
| 20 | + ARM_TENANT_ID: ${{ vars.ARM_TENANT_ID }} |
| 21 | + ARM_USE_OIDC: true |
| 22 | + ARM_USE_AZUREAD: true |
| 23 | + ARM_STORAGE_USE_AZUREAD: true |
| 24 | + TERRAFORM_ENVIRONMENT: ${{ inputs.tf_environment || inputs.environment }} |
| 25 | + |
| 26 | +jobs: |
| 27 | + tf_plan: |
| 28 | + name: "Terraform Plan" |
| 29 | + runs-on: ubuntu-20.04 |
| 30 | + environment: ${{ inputs.environment }}-ci |
| 31 | + permissions: |
| 32 | + id-token: write |
| 33 | + contents: read |
| 34 | + outputs: |
| 35 | + terraform_version: ${{ steps.set-terraform-version.outputs.terraform_version }} |
| 36 | + |
| 37 | + steps: |
| 38 | + - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 39 | + name: Checkout |
| 40 | + |
| 41 | + - name: Set Environment Variables |
| 42 | + if: ${{ inputs.env_vars }} |
| 43 | + run: | |
| 44 | + for i in "${{ inputs.env_vars }}" |
| 45 | + do |
| 46 | + printf "%s\n" $i >> $GITHUB_ENV |
| 47 | + done |
| 48 | +
|
| 49 | + - name: Set Terraform Version |
| 50 | + id: set-terraform-version |
| 51 | + run: | |
| 52 | + echo "terraform_version=$(cat .terraform-version)" >> $GITHUB_OUTPUT |
| 53 | +
|
| 54 | + - uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0 |
| 55 | + name: Setup Terraform |
| 56 | + with: |
| 57 | + terraform_version: ${{ steps.set-terraform-version.outputs.terraform_version }} |
| 58 | + |
| 59 | + - name: Terraform Plan |
| 60 | + uses: pagopa/terraform-preapply-azure-action@54ded8cda3437c3f6a9f46baf69cb321ce82f5cd |
| 61 | + with: |
| 62 | + client_id: ${{ secrets.ARM_CLIENT_ID }} |
| 63 | + tenant_id: ${{ vars.ARM_TENANT_ID }} |
| 64 | + subscription_id: ${{ vars.ARM_SUBSCRIPTION_ID }} |
| 65 | + dir: ${{ inputs.dir }} |
| 66 | + azure_environment: ${{ env.TERRAFORM_ENVIRONMENT }} |
| 67 | + env: |
| 68 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 69 | + |
| 70 | + - name: "Upload Terraform Plan as Artifact" |
| 71 | + uses: actions/upload-artifact@694cdabd8bdb0f10b2cea11669e1bf5453eed0a6 # v4.2.0 |
| 72 | + with: |
| 73 | + name: tfplan |
| 74 | + path: ${{ inputs.dir }}/tfplan-${{ env.TERRAFORM_ENVIRONMENT }}-${{ github.sha }} |
| 75 | + if-no-files-found: error |
| 76 | + retention-days: 14 |
| 77 | + |
| 78 | + apply: |
| 79 | + name: "Terraform Apply" |
| 80 | + runs-on: ubuntu-20.04 |
| 81 | + needs: [tf_plan] |
| 82 | + environment: ${{ inputs.environment }}-cd |
| 83 | + permissions: |
| 84 | + id-token: write |
| 85 | + contents: read |
| 86 | + env: |
| 87 | + ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }} |
| 88 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 89 | + |
| 90 | + steps: |
| 91 | + - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 92 | + name: Checkout |
| 93 | + |
| 94 | + - name: Azure Login |
| 95 | + uses: azure/login@cb79c773a3cfa27f31f25eb3f677781210c9ce3d # v1.6.1 |
| 96 | + with: |
| 97 | + client-id: ${{ secrets.ARM_CLIENT_ID }} |
| 98 | + tenant-id: ${{ vars.ARM_TENANT_ID }} |
| 99 | + subscription-id: ${{ vars.ARM_SUBSCRIPTION_ID }} |
| 100 | + |
| 101 | + - name: Download Terraform Plan as Artifact |
| 102 | + uses: actions/download-artifact@6b208ae046db98c579e8a3aa621ab581ff575935 # v4.1.1 |
| 103 | + with: |
| 104 | + name: tfplan |
| 105 | + path: ${{ inputs.dir }} |
| 106 | + |
| 107 | + - name: Retrieve Terraform Modules from Cache |
| 108 | + id: cache-terraform-modules |
| 109 | + uses: actions/cache@13aacd865c20de90d75de3b17ebe84f7a17d57d2 # v4.0.0 |
| 110 | + with: |
| 111 | + path: ${{ inputs.dir }}/.terraform |
| 112 | + key: terraform-${{ inputs.dir }}-${{ github.sha }} |
| 113 | + restore-keys: | |
| 114 | + terraform-${{ inputs.dir }} |
| 115 | +
|
| 116 | + - uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0 |
| 117 | + name: Setup Terraform |
| 118 | + with: |
| 119 | + terraform_version: ${{ needs.tf_plan.outputs.terraform_version }} |
| 120 | + |
| 121 | + - name: Terraform Init |
| 122 | + shell: bash |
| 123 | + working-directory: ${{ inputs.dir }} |
| 124 | + run: | |
| 125 | + bash ./terraform.sh init ${{ env.TERRAFORM_ENVIRONMENT }} |
| 126 | +
|
| 127 | + - name: Terraform Apply |
| 128 | + shell: bash |
| 129 | + working-directory: ${{ inputs.dir }} |
| 130 | + run: | |
| 131 | + terraform apply -lock-timeout=3000s -auto-approve -input=false tfplan-${{ env.TERRAFORM_ENVIRONMENT }}-${{ github.sha }} |
0 commit comments