Merge pull request #5 from kozhabergenova/add_skip_ver #13
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 Workflow" | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
permissions: | |
id-token: write # This is required for aws oidc(openid connect) connection | |
contents: read # This is required for actions/checkout | |
pull-requests: write # This is required for github bot to comment PR | |
env: | |
# TF_LOG: INFO | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
NETBOX_API_TOKEN: ${{ secrets.NETBOX_API_TOKEN }} | |
FMC_USERNAME: ${{ secrets.FMC_USERNAME }} | |
FMC_PASSWORD: ${{ secrets.FMC_PASSWORD }} | |
jobs: | |
plan: | |
runs-on: ubuntu-latest | |
if: github.event_name == 'pull_request' | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Configure AWS credentials from AWS account | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: ${{ secrets.AWS_ROLE }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
role-session-name: GitHub-OIDC-TERRAFORM | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: 1.2.5 | |
- name: Terraform fmt | |
id: fmt | |
run: terraform fmt -check | |
continue-on-error: true | |
- name: Terraform Init | |
id: init | |
env: | |
AWS_BUCKET_KEY_NAME: ${{ secrets.AWS_BUCKET_KEY_NAME }} | |
run: terraform init -backend-config="key=${AWS_BUCKET_KEY_NAME}" | |
- name: Terraform Validate | |
id: validate | |
run: terraform validate -no-color | |
- name: Terraform Plan | |
id: plan | |
run: terraform plan -var="aci_username=${{ secrets.ACI_USERNAME }}" -var="aci_password=${{ secrets.ACI_PASSWORD }}" -no-color -out=tfplan.tfplan | |
continue-on-error: true | |
- uses: actions/github-script@v6 | |
env: | |
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}" | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\` | |
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\` | |
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\` | |
<details><summary>Validation Output</summary> | |
\`\`\`\n | |
${{ steps.validate.outputs.stdout }} | |
\`\`\` | |
</details> | |
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\` | |
<details><summary>Show Plan</summary> | |
\`\`\`\n | |
${process.env.PLAN} | |
\`\`\` | |
</details> | |
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; | |
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 | |
apply: | |
runs-on: ubuntu-latest | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: 1.2.5 | |
- name: Configure AWS credentials from AWS account | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: ${{ secrets.AWS_ROLE }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
role-session-name: GitHub-OIDC-TERRAFORM | |
- name: Terraform Init | |
id: init | |
env: | |
AWS_BUCKET_KEY_NAME: ${{ secrets.AWS_BUCKET_KEY_NAME }} | |
run: terraform init -backend-config="key=${AWS_BUCKET_KEY_NAME}" | |
- name: Terraform Apply | |
id: apply | |
env: | |
TF_VAR_aci_username: ${{ secrets.ACI_USERNAME }} | |
TF_VAR_aci_password: ${{ secrets.ACI_PASSWORD }} | |
run: terraform apply -auto-approve | |