From 6a460abe2733f157746f590decdd429f1789874a Mon Sep 17 00:00:00 2001 From: saumitra-dev Date: Fri, 2 Aug 2024 10:53:47 +0530 Subject: [PATCH] ci: use module template workflows --- .github/workflows/pr-title.yaml | 6 +-- .github/workflows/release.yaml | 12 ++--- .github/workflows/terraform-checks.yaml | 9 +--- .github/workflows/terraform-docs.yaml | 64 ++++++++++++++++++++----- .github/workflows/terraform.yaml | 36 ++++++++++++++ .github/workflows/update-configs.yaml | 45 +++++++++++++++++ 6 files changed, 140 insertions(+), 32 deletions(-) create mode 100644 .github/workflows/terraform.yaml create mode 100644 .github/workflows/update-configs.yaml diff --git a/.github/workflows/pr-title.yaml b/.github/workflows/pr-title.yaml index 3973df4..3c7793f 100644 --- a/.github/workflows/pr-title.yaml +++ b/.github/workflows/pr-title.yaml @@ -1,11 +1,7 @@ name: 'Validate PR title' on: - pull_request_target: - types: - - opened - - edited - - synchronize + workflow_call: jobs: main: diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index eebcf88..06d5bef 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -1,9 +1,7 @@ name: 'Module Release' on: - push: - branches: - - main + workflow_call: defaults: run: @@ -15,10 +13,10 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v2 - with: - persist-credentials: false - fetch-depth: 0 + uses: actions/checkout@v4 + + - name: Git pull + run: git pull origin main - name: Release uses: cycjimmy/semantic-release-action@v2 diff --git a/.github/workflows/terraform-checks.yaml b/.github/workflows/terraform-checks.yaml index ff84410..b8faa24 100644 --- a/.github/workflows/terraform-checks.yaml +++ b/.github/workflows/terraform-checks.yaml @@ -1,14 +1,7 @@ name: Terraform Checks on: - push: - branches: - - main - - master - pull_request: - branches: - - main - - master + workflow_call: env: TERRAFORM_DOCS_VERSION: v0.18.0 diff --git a/.github/workflows/terraform-docs.yaml b/.github/workflows/terraform-docs.yaml index 04c7312..814ac07 100644 --- a/.github/workflows/terraform-docs.yaml +++ b/.github/workflows/terraform-docs.yaml @@ -1,14 +1,7 @@ name: Terraform Docs on: - push: - branches: - - main - - master - pull_request: - branches: - - main - - master + workflow_call: env: TERRAFORM_DOCS_VERSION: v0.18.0 @@ -20,18 +13,65 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 with: - ref: ${{ github.event.pull_request.head.ref }} + ref: ${{ github.ref }} - - name: Render and Push terraform docs for main and modules + - name: Render and Push terraform docs for main module uses: terraform-docs/gh-actions@main with: working-dir: . git-push: true - config-file: .terraform-docs.yaml + output-file: README.md + output-format: markdown table + output-method: replace + recursive: false + template: | + + {{ .Content }} + + {{- printf "\n" -}} + args: "--header-from .header.md" + + - name: Check if modules folder exists + id: check_modules + run: echo "modules_exists=$(if [ -d modules ]; then echo true; else echo false; fi)" >> $GITHUB_ENV + + - name: Render and Push terraform docs for sub modules + if: env.modules_exists == 'true' + uses: terraform-docs/gh-actions@main + with: + working-dir: . + git-push: true + output-file: README.md + output-format: markdown table + output-method: replace + recursive: true + recursive-path: modules + template: | + + {{ .Content }} + + {{- printf "\n" -}} + args: "--header-from .header.md" + + + - name: Check if examples folder exists + id: check_examples + run: echo "examples_exists=$(if [ -d examples ]; then echo true; else echo false; fi)" >> $GITHUB_ENV - name: Render and Push terraform docs for examples + if: env.examples_exists == 'true' uses: terraform-docs/gh-actions@main with: working-dir: . git-push: true - config-file: .terraform-docs-example.yaml + output-file: README.md + output-format: markdown table + output-method: replace + recursive: true + recursive-path: examples + template: | + + {{ .Content }} + + {{- printf "\n" -}} + args: "--header-from .header.md" diff --git a/.github/workflows/terraform.yaml b/.github/workflows/terraform.yaml new file mode 100644 index 0000000..c1158ef --- /dev/null +++ b/.github/workflows/terraform.yaml @@ -0,0 +1,36 @@ +name: Terraform Master Workflow + +on: + push: + branches: + - main + - master + pull_request_target: + types: + - opened + - edited + - synchronize + pull_request: + branches: + - main + - master +jobs: + prTitlecheck: + name: PR title check + if: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.base.ref == 'main' }} + uses: ./.github/workflows/pr-title.yaml + + preCommitCheck: + name: Terraform Checks + uses: ./.github/workflows/terraform-checks.yaml + + generateDocs: + name: Generate Terraform Docs + needs: preCommitCheck + uses: ./.github/workflows/terraform-docs.yaml + + release: + name: Release module + needs: generateDocs + if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} + uses: ./.github/workflows/release.yaml diff --git a/.github/workflows/update-configs.yaml b/.github/workflows/update-configs.yaml new file mode 100644 index 0000000..5fccd28 --- /dev/null +++ b/.github/workflows/update-configs.yaml @@ -0,0 +1,45 @@ + name: Update Configuration from Template + + on: + workflow_dispatch: + + jobs: + update: + runs-on: ubuntu-latest + + steps: + - name: Checkout template repository + uses: actions/checkout@v3 + with: + repository: nimisha-gj/terraform-module-template + token: ${{ secrets.GITHUB_TOKEN }} + path: template-repo + + - name: Checkout target repository + uses: actions/checkout@v3 + with: + repository: ${{ github.repository }} + token: ${{ secrets.GITHUB_TOKEN }} + path: target-repo + + - name: Set up Git + run: | + git config --global user.name "github-actions" + git config --global user.email "github-actions@github.com" + + - name: Copy files from template repository + run: | + cp template-repo/.editorconfig target-repo/ + cp template-repo/.pre-commit-config.yaml target-repo/ + cp template-repo/LICENSE target-repo/ + cp template-repo/.tflint.hcl target-repo/ + cp template-repo/.releaserc.json target-repo/ + cp template-repo/CODE_OF_CONDUCT.md target-repo/ + cp template-repo/CONTRIBUTING.md target-repo/ + + - name: Commit and push changes + run: | + cd target-repo + git add . + git commit -m "Update files from terraform-module-template" + git push