merge to production #113
This file contains 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: "Terraform" | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- master | |
- production | |
pull_request: | |
jobs: | |
terraform: | |
name: "Terraform" | |
runs-on: ubuntu-latest | |
env: | |
TF_VAR_github_organization: ictsc | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Generate token | |
id: generate_token | |
uses: tibdex/[email protected] | |
with: | |
app_id: ${{ secrets.APP_ID }} | |
installation_id: 31536419 | |
private_key: ${{ secrets.PRIVATE_KEY }} | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v2 | |
with: | |
terraform_version: 1.3.6 | |
- name: Terraform Format | |
id: fmt | |
run: terraform fmt -recursive -check | |
- name: Terraform Init | |
id: init | |
run: terraform init -input=false | |
- name: Terraform Validate | |
run: terraform validate -no-color | |
- name: Get latest state | |
if: github.ref != 'refs/heads/production' | |
run: wget https://raw.githubusercontent.com/ictsc/ictsc-github-member/production/terraform.tfstate | |
- name: Terraform refresh state | |
env: | |
TF_VAR_github_token: ${{ steps.generate_token.outputs.token }} | |
run: terraform refresh -no-color -input=false -parallelism=30 | |
- name: Terraform Plan | |
env: | |
TF_VAR_github_token: ${{ steps.generate_token.outputs.token }} | |
id: plan | |
run: terraform plan -no-color -input=false -parallelism=30 --refresh=false | |
continue-on-error: true | |
- uses: actions/github-script@v6 | |
if: github.event_name == 'pull_request' && steps.plan.outcome != 'failure' | |
env: | |
PLAN: "${{ steps.plan.outputs.stdout }}" | |
with: | |
script: | | |
const output = ` | |
<details><summary>Terraform Plan</summary> | |
\`\`\`text | |
${process.env.PLAN}\`\`\` | |
</details>` | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output | |
}) | |
- name: Terraform Plan Status | |
if: steps.plan.outcome == 'failure' | |
run: exit 1 | |
- name: Terraform Apply | |
env: | |
TF_VAR_github_token: ${{ steps.generate_token.outputs.token }} | |
if: github.ref == 'refs/heads/production' && github.event_name == 'push' | |
run: | | |
terraform apply -auto-approve -input=false | |
- name: Backup tfstate to artifact | |
if: github.ref == 'refs/heads/production' && github.event_name == 'push' | |
uses: actions/upload-artifact@v3 | |
with: | |
name: terraform.tfstate | |
path: terraform.tfstate | |
- name : Commit and Push tfstate | |
if: github.ref == 'refs/heads/production' && github.event_name == 'push' | |
run: | | |
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
git config --global user.name "Github Action" | |
git add -f terraform.tfstate | |
git commit -m "update terraform state [ci skip]" | |
git push origin production |