-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/HappyPathway/terraform-gith…
- Loading branch information
Showing
1 changed file
with
16 additions
and
84 deletions.
There are no files selected for viewing
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
| 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 }} |