Skip to content

Commit

Permalink
Enhance GitHub Actions workflow to allow pull requests and format Ter…
Browse files Browse the repository at this point in the history
…raform files; update permissions and add branch creation for formatting changes
  • Loading branch information
Dave Arnold committed Mar 22, 2025
1 parent 8606bd2 commit ac20664
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions .github/workflows/terraform-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:

permissions:
contents: write
pull-requests: read
pull-requests: write

jobs:
terraform:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ac20664

Please sign in to comment.