|
| 1 | +run-name: 'Terraform workflow' |
| 2 | +on: |
| 3 | + workflow_call: |
| 4 | + inputs: |
| 5 | + working_directory: |
| 6 | + required: true |
| 7 | + type: string |
| 8 | + description: 'Root directory of the terraform where all resources exist.' |
| 9 | + provider: |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + default: aws |
| 13 | + description: 'Cloud provider to run the workflow. e.g. azurerm, aws, gcp or digitalocean' |
| 14 | + aws_region: |
| 15 | + required: false |
| 16 | + type: string |
| 17 | + default: us-east-2 |
| 18 | + description: 'AWS region of terraform deployment.' |
| 19 | + gcp_region: |
| 20 | + required: false |
| 21 | + type: string |
| 22 | + description: 'GCP region of terraform deployment.' |
| 23 | + var_file: |
| 24 | + required: false |
| 25 | + type: string |
| 26 | + description: 'Terraform var file directory. e.g. vars/dev.tfvars' |
| 27 | + destroy: |
| 28 | + type: string |
| 29 | + default: false |
| 30 | + description: 'you want to destroy infra or not' |
| 31 | + approvers: |
| 32 | + required: false |
| 33 | + type: string |
| 34 | + description: 'Approvals list to approve apply or destroy' |
| 35 | + terraform_version: |
| 36 | + type: string |
| 37 | + default: 1.3.6 |
| 38 | + description: 'Required erraform version ' |
| 39 | + timeout: |
| 40 | + required: false |
| 41 | + type: number |
| 42 | + default: 10 |
| 43 | + description: 'Timeout for approval step' |
| 44 | + secrets: |
| 45 | + AZURE_CREDENTIALS: |
| 46 | + required: false |
| 47 | + description: 'Azure Credentials to install Azure in github runner.' |
| 48 | + AWS_ACCESS_KEY_ID: |
| 49 | + required: false |
| 50 | + description: 'AWS Access Key ID to install AWS CLI.' |
| 51 | + BUILD_ROLE: |
| 52 | + required: false |
| 53 | + description: 'AWS OIDC role for aws authentication.' |
| 54 | + AWS_SECRET_ACCESS_KEY: |
| 55 | + required: false |
| 56 | + description: 'AWS Secret access key to install AWS CLI' |
| 57 | + AWS_SESSION_TOKEN: |
| 58 | + required: false |
| 59 | + description: 'AWS Session Token to install AWS CLI' |
| 60 | + GCP_SA_KEY: |
| 61 | + required: false |
| 62 | + description: 'GCP service account Secret access key to install GCP CLI' |
| 63 | + PROJECT_ID: |
| 64 | + required: false |
| 65 | + description: 'GCP Secret access key to install GCP CLI' |
| 66 | + DIGITALOCEAN_ACCESS_TOKEN: |
| 67 | + required: false |
| 68 | + description: 'Digitalocean access Token to install Digitalocean CLI' |
| 69 | + SPACES_ACCESS_KEY_ID: |
| 70 | + required: false |
| 71 | + description: 'Spaces access key ID for digitalocean if required' |
| 72 | + SPACES_SECRET_ACCESS_KEY: |
| 73 | + required: false |
| 74 | + description: 'Spaces secret access key for digitalocean if required' |
| 75 | + |
| 76 | +jobs: |
| 77 | + terraform-workflow: |
| 78 | + runs-on: ubuntu-latest |
| 79 | + env: |
| 80 | + ARM_SKIP_PROVIDER_REGISTRATION: true |
| 81 | + DIGITALOCEAN_TOKEN: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} |
| 82 | + SPACES_ACCESS_KEY_ID: ${{ secrets.SPACES_ACCESS_KEY_ID }} |
| 83 | + SPACES_SECRET_ACCESS_KEY: ${{ secrets.SPACES_SECRET_ACCESS_KEY }} |
| 84 | + outputs: |
| 85 | + tfplanExitCode: ${{ steps.tf-plan.outputs.exitcode }} |
| 86 | + |
| 87 | + steps: |
| 88 | + - name: Checkout |
| 89 | + uses: actions/checkout@v3 |
| 90 | + |
| 91 | + - name: Install AWS CLI |
| 92 | + if: ${{ inputs.provider == 'aws' }} |
| 93 | + uses: aws-actions/configure-aws-credentials@v2 |
| 94 | + with: |
| 95 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 96 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 97 | + aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }} |
| 98 | + role-to-assume: ${{ secrets.BUILD_ROLE }} |
| 99 | + aws-region: ${{ inputs.aws_region }} |
| 100 | + role-duration-seconds: 900 |
| 101 | + role-skip-session-tagging: true |
| 102 | + |
| 103 | + - name: Install Azure CLI |
| 104 | + if: ${{ inputs.provider == 'azurerm' }} |
| 105 | + uses: azure/login@v1 |
| 106 | + with: |
| 107 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 108 | + |
| 109 | + - name: Install GCP CLI |
| 110 | + if: ${{ inputs.provider == 'gcp' }} |
| 111 | + uses: google-github-actions/auth@v0 |
| 112 | + with: |
| 113 | + service_account_key: ${{ secrets.GCP_SA_KEY }} |
| 114 | + project_id: ${{ secrets.PROJECT_ID }} |
| 115 | + region: ${{ inputs.gcp_region }} |
| 116 | + |
| 117 | + - name: Install doctl |
| 118 | + if: ${{ inputs.provider == 'digitalocean' }} |
| 119 | + uses: digitalocean/action-doctl@v2 |
| 120 | + with: |
| 121 | + token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} |
| 122 | + |
| 123 | + - name: Set up Terraform |
| 124 | + uses: hashicorp/setup-terraform@v2 |
| 125 | + with: |
| 126 | + terraform_version: ${{ inputs.terraform_version }} |
| 127 | + |
| 128 | + - name: 'Terraform Format' |
| 129 | + if: ${{ inputs.destroy != 'true' }} |
| 130 | + id: fmt |
| 131 | + uses: 'dflook/terraform-fmt-check@v1' |
| 132 | + with: |
| 133 | + actions_subcommand: 'fmt' |
| 134 | + path: ${{ inputs.working_directory }} |
| 135 | + |
| 136 | + - name: terraform init |
| 137 | + run: | |
| 138 | + cd ${{ inputs.working_directory }} |
| 139 | + terraform init |
| 140 | + |
| 141 | + - name: 'Terraform validate' |
| 142 | + if: ${{ inputs.destroy != 'true' }} |
| 143 | + id: validate |
| 144 | + uses: dflook/terraform-validate@v1 |
| 145 | + with: |
| 146 | + tf_actions_working_dir: ${{ inputs.working_directory }} |
| 147 | + |
| 148 | + - name: Terraform Plan |
| 149 | + id: tf-plan |
| 150 | + run: | |
| 151 | + export exitcode=0 |
| 152 | + cd ${{ inputs.working_directory }} |
| 153 | + if [ "${{ inputs.destroy }}" = "true" ]; then |
| 154 | + if [ -n "${{ inputs.var_file }}" ]; then |
| 155 | + terraform plan -destroy -out tfplan --var-file=${{ inputs.var_file }} |
| 156 | + else |
| 157 | + terraform plan -destroy -out tfplan |
| 158 | + fi |
| 159 | + else |
| 160 | + if [ -n "${{ inputs.var_file }}" ]; then |
| 161 | + terraform plan -out tfplan --var-file=${{ inputs.var_file }} |
| 162 | + else |
| 163 | + terraform plan -out tfplan |
| 164 | + fi |
| 165 | + fi |
| 166 | +
|
| 167 | + - name: Publish Terraform Plan |
| 168 | + uses: actions/upload-artifact@v3 |
| 169 | + with: |
| 170 | + name: tfplan |
| 171 | + path: ${{ inputs.working_directory }}/tfplan |
| 172 | + |
| 173 | + - name: Create String Output |
| 174 | + id: tf-plan-string |
| 175 | + run: | |
| 176 | + cd ${{ inputs.working_directory }} |
| 177 | + TERRAFORM_PLAN=$(terraform show -no-color tfplan) |
| 178 | + delimiter="$(openssl rand -hex 8)" |
| 179 | + echo "summary<<${delimiter}" >> $GITHUB_OUTPUT |
| 180 | + echo "## Terraform Plan Output" >> $GITHUB_OUTPUT |
| 181 | + echo "<details><summary>Click to expand</summary>" >> $GITHUB_OUTPUT |
| 182 | + echo "" >> $GITHUB_OUTPUT |
| 183 | + echo '```terraform' >> $GITHUB_OUTPUT |
| 184 | + echo "$TERRAFORM_PLAN" >> $GITHUB_OUTPUT |
| 185 | + echo '```' >> $GITHUB_OUTPUT |
| 186 | + echo "</details>" >> $GITHUB_OUTPUT |
| 187 | + echo "${delimiter}" >> $GITHUB_OUTPUT |
| 188 | + |
| 189 | + - name: "Accept plan or deny" |
| 190 | + uses: trstringer/manual-approval@v1 |
| 191 | + timeout-minutes: ${{ inputs.timeout }} |
| 192 | + with: |
| 193 | + secret: ${{ github.TOKEN }} |
| 194 | + approvers: ${{ inputs.approvers }} |
| 195 | + issue-title: "Terraform Plan for Infrastructure Update" |
| 196 | + issue-body: ${{ steps.tf-plan-string.outputs.summary }} |
| 197 | + |
| 198 | + - name: terraform apply |
| 199 | + if: ${{ inputs.destroy != 'true' }} |
| 200 | + run: | |
| 201 | + if [ -n "${{ inputs.var_file }}" ]; then |
| 202 | + cd ${{ inputs.working_directory }} |
| 203 | + terraform apply -var-file="${{ inputs.var_file }}" -auto-approve |
| 204 | + else |
| 205 | + cd ${{ inputs.working_directory }} |
| 206 | + terraform apply -auto-approve |
| 207 | + fi |
| 208 | + |
| 209 | + - name: Terraform destroy |
| 210 | + if: ${{ inputs.destroy == 'true' }} |
| 211 | + id: destroy |
| 212 | + run: | |
| 213 | + if [ -n "${{ inputs.var_file }}" ]; then |
| 214 | + cd ${{ inputs.working_directory }} |
| 215 | + terraform destroy -var-file="${{ inputs.var_file }}" -auto-approve |
| 216 | + else |
| 217 | + cd ${{ inputs.working_directory }} |
| 218 | + terraform destroy -auto-approve |
| 219 | + fi |
0 commit comments