diff --git a/.github/workflows/terraform-test.yml b/.github/workflows/terraform-test.yml index d462958..2196566 100644 --- a/.github/workflows/terraform-test.yml +++ b/.github/workflows/terraform-test.yml @@ -11,7 +11,7 @@ on: permissions: contents: write - pull-requests: read + pull-requests: write jobs: terraform: @@ -27,6 +27,12 @@ jobs: with: fetch-depth: 0 + - name: Setup GitHub CLI + run: | + # GitHub CLI is pre-installed on GitHub Actions runners + # Authenticate GitHub CLI with the provided token + echo "${{ secrets.GH_TOKEN }}" | gh auth login --with-token + - name: Setup Terraform uses: hashicorp/setup-terraform@v3 with: @@ -54,10 +60,19 @@ jobs: git add . git commit -m "chore: format terraform files" - # Only push directly if we're on the main branch - if [[ "${{ github.event_name }}" == "push" ]]; then - git push - elif [[ "${{ github.event.pull_request.head.repo.fork }}" == "false" ]]; then + # 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