Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
arnol377 committed Mar 25, 2025
2 parents 1c0e7be + e3373b5 commit c031ba3
Showing 1 changed file with 16 additions and 84 deletions.
100 changes: 16 additions & 84 deletions .github/workflows/terraform-test.yml
Original file line number Diff line number Diff line change
@@ -1,99 +1,31 @@
name: "Terraform Test and Tag"

name: Terraform CI/CD
on:
push:
branches:
- main
pull_request:
types: [closed]
branches:
- main
workflow_dispatch:

# Set concurrency to prevent overlapping workflow runs
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false

permissions:
contents: write
pull-requests: write

jobs:
terraform:
name: "Terraform Test"
terraform-ci-cd:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
GITHUB_OWNER: ${{ vars.GH_ORG }}
permissions:
contents: write

steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: "~>1.6.0"
terraform_wrapper: false

- name: Format Terraform files
run: terraform fmt -recursive
id: fmt
terraform_version: "1.10.5" # Specify your desired version

- name: Check for file changes
id: check_changes
run: |
if [ -n "$(git status --porcelain)" ]; then
echo "changes_detected=true" >> $GITHUB_ENV
else
echo "changes_detected=false" >> $GITHUB_ENV
fi
- name: Commit and push changes
if: env.changes_detected == 'true'
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add .
git commit -m "chore: format terraform files"
# For push events on main branch, create a PR instead of pushing directly
if [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then
# Create a new branch for the formatting changes
BRANCH_NAME="format-terraform-$(date +%Y%m%d%H%M%S)"
git checkout -b $BRANCH_NAME
git push -u origin $BRANCH_NAME
# Create a pull request using the GitHub CLI or API
gh pr create --title "chore: format terraform files" \
--body "This PR contains automatic formatting changes from the CI workflow." \
--head $BRANCH_NAME \
--base main
elif [[ "${{ github.event_name }}" == "pull_request" && "${{ github.event.pull_request.head.repo.fork }}" == "false" ]]; then
# Only push to the PR branch if it's from the same repository (not a fork)
git push origin "HEAD:${{ github.event.pull_request.head.ref }}"
else
echo "Changes detected but not pushing to a fork PR. Please update your PR with these formatting changes."
fi
- name: Terraform Init
id: init
run: terraform init -backend=false

- name: Terraform Validate
id: validate
run: terraform validate

- name: Run Terraform Tests
id: test
run: terraform test

- name: Bump version and push tag
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
uses: anothrNick/github-tag-action@1.67.0
- name: Run Terraform Module Release Action
uses: HappyPathway/terraform-module-release@main
with:
github-token: ${{ secrets.GH_TOKEN }}
working-directory: '.'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEFAULT_BUMP: patch
WITH_V: true
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
GITHUB_OWNER: ${{ vars.GH_ORG }}

0 comments on commit c031ba3

Please sign in to comment.