Skip to content

Commit

Permalink
feat(scaffold-module): require module templates (#1341)
Browse files Browse the repository at this point in the history
* feat(scaffold-module): require module templates

* fix(test-module): stop running `terraform init

* fix(create-scaffold-module-pr): replace the input module_path with TFACTION_MODULE_PATH
  • Loading branch information
suzuki-shunsuke committed Nov 25, 2023
1 parent f85894c commit d45bc33
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 63 deletions.
27 changes: 8 additions & 19 deletions create-scaffold-module-pr/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ inputs:
pull-requests:write - Create pull requests
required: true

module_path:
description: 'Module path'
required: true

runs:
using: composite
steps:
Expand All @@ -22,39 +18,34 @@ runs:
shell: bash
if: inputs.github_token == ''
- run: |
echo "::error ::module_path is required"
echo "::error ::env.TFACTION_MODULE_PATH is required"
exit 1
shell: bash
if: inputs.module_path == ''
if: env.TFACTION_MODULE_PATH == ''
- uses: suzuki-shunsuke/tfaction/get-global-config@main
id: global-config

- run: aqua i -l -a
shell: bash
working-directory: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}

- run: |
echo "branch=scaffold-module-${MODULE_PATH/\//_}-$(date +%Y%m%dT%H%M%S)" >> "$GITHUB_OUTPUT"
echo "branch=scaffold-module-${TFACTION_MODULE_PATH/\//_}-$(date +%Y%m%dT%H%M%S)" >> "$GITHUB_OUTPUT"
id: branch
shell: bash
env:
MODULE_PATH: ${{inputs.module_path}}
- run: git add "$MODULE_PATH"
- run: git add "$TFACTION_MODULE_PATH"
shell: bash
env:
MODULE_PATH: ${{inputs.module_path}}

- run: |
git diff --cached --name-only |
xargs ghcp commit -r "$GITHUB_REPOSITORY" \
-b "${{steps.branch.outputs.branch}}" \
-m "chore($MODULE_PATH): scaffold a Terraform Module"
-m "chore($TFACTION_MODULE_PATH): scaffold a Terraform Module"
shell: bash
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
MODULE_PATH: ${{inputs.module_path}}
- run: |
draft_pr=""
Expand All @@ -70,7 +61,7 @@ runs:
$CODE_BLOCK
gh pr create -R "$GITHUB_REPOSITORY" $draft_pr\\
-H "${{steps.branch.outputs.branch}}" \\
-t "Scaffold a Terraform Module (${MODULE_PATH})" \\
-t "Scaffold a Terraform Module (${TFACTION_MODULE_PATH})" \\
-b "This pull request was created by [GitHub Actions]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID)"
$CODE_BLOCK
Expand All @@ -79,11 +70,10 @@ runs:
if: "fromJSON(steps.global-config.outputs.skip_create_pr)"
shell: bash
env:
MODULE_PATH: ${{inputs.module_path}}
CODE_BLOCK: "```"
- run: |
opts=(-H "${{steps.branch.outputs.branch}}" -a "$GITHUB_ACTOR" -t "Scaffold a Terraform Module (${MODULE_PATH})" -b "@$GITHUB_ACTOR This pull request was created by [GitHub Actions workflow_dispatch event]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID)")
opts=(-H "${{steps.branch.outputs.branch}}" -a "$GITHUB_ACTOR" -t "Scaffold a Terraform Module (${TFACTION_MODULE_PATH})" -b "@$GITHUB_ACTOR This pull request was created by [GitHub Actions workflow_dispatch event]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID)")
if [ "${{steps.global-config.outputs.draft_pr}}" = "true" ]; then
opts+=( -d )
fi
Expand All @@ -92,4 +82,3 @@ runs:
if: "!fromJSON(steps.global-config.outputs.skip_create_pr)"
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
MODULE_PATH: ${{inputs.module_path}}
70 changes: 36 additions & 34 deletions scaffold-module/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ inputs:
pull-requests:write - Create pull requests
required: true

module_path:
description: 'Module path'
required: true

runs:
using: composite
steps:
Expand All @@ -22,87 +18,93 @@ runs:
shell: bash
if: inputs.github_token == ''
- run: |
echo "::error ::module_path is required"
echo "::error ::env.TFACTION_MODULE_PATH is required"
exit 1
shell: bash
if: env.TFACTION_MODULE_PATH == ''
- run: |
echo "::error ::env.TFACTION_MODULE_TEMPLATE_DIR is required"
exit 1
shell: bash
if: inputs.module_path == ''
if: env.TFACTION_MODULE_TEMPLATE_DIR == ''
- run: |
if [ -e "${MODULE_PATH}" ]; then
if [ -e "${TFACTION_MODULE_PATH}" ]; then
echo "::error ::file exists"
exit 1
fi
shell: bash
env:
MODULE_PATH: ${{inputs.module_path}}
- run: |
if [ ! -d "${TFACTION_MODULE_TEMPLATE_DIR}" ]; then
echo "::error ::$TFACTION_MODULE_TEMPLATE_DIR doesn't exist"
exit 1
fi
shell: bash
- uses: suzuki-shunsuke/tfaction/get-global-config@main
id: global-config

- run: |
mkdir -p "$(dirname "$MODULE_PATH")"
mkdir -p "$(dirname "$TFACTION_MODULE_PATH")"
shell: bash
- run: cp -R "${TFACTION_MODULE_TEMPLATE_DIR}" "$TFACTION_MODULE_PATH"
shell: bash
env:
MODULE_PATH: ${{inputs.module_path}}

- run: cp -R "${GITHUB_ACTION_PATH}/template" "$MODULE_PATH"
- run: git add .
shell: bash
env:
MODULE_PATH: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}

- run: |
sed -i "s|%%MODULE_NAME%%|$(basename "$MODULE_PATH")|g" "${MODULE_PATH}/docs/header.md"
git ls-files | xargs -n 1 sed -i "s|%%MODULE_NAME%%|$(basename "$TFACTION_MODULE_PATH")|g"
shell: bash
env:
MODULE_PATH: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}
- run: |
sed -i "s|%%MODULE_PATH%%|$MODULE_PATH|g" "${MODULE_PATH}/docs/header.md"
git ls-files | xargs -n 1 sed -i "s|%%MODULE_PATH%%|$MODULE_PATH|g"
shell: bash
env:
MODULE_PATH: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}
- run: |
sed -i "s|%%GITHUB_REPOSITORY%%|$GITHUB_REPOSITORY|g" "${MODULE_PATH}/docs/header.md"
git ls-files | xargs -n 1 sed -i "s|%%GITHUB_REPOSITORY%%|$GITHUB_REPOSITORY|g"
shell: bash
env:
MODULE_PATH: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}
- run: |
sed -i "s|%%REF%%|module_${MODULE_PATH/\//_}_v0.1.0|g" "${MODULE_PATH}/docs/header.md"
git ls-files | xargs -n 1 sed -i "s|%%REF%%|module_${MODULE_PATH/\//_}_v0.1.0|g"
shell: bash
env:
MODULE_PATH: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}
- run: aqua init
shell: bash
working-directory: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}
if: env.TFACTION_SKIP_ADDING_AQUA_PACKAGES != 'true'

- run: aqua g -i hashicorp/terraform
shell: bash
working-directory: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}
if: env.TFACTION_SKIP_ADDING_AQUA_PACKAGES != 'true'

- run: aqua g -i aquasecurity/tfsec
shell: bash
working-directory: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}
if: env.TFACTION_SKIP_ADDING_AQUA_PACKAGES != 'true' && fromJSON(steps.global-config.outputs.enable_tfsec)

- run: aqua g -i aquasecurity/trivy
shell: bash
working-directory: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}
if: env.TFACTION_SKIP_ADDING_AQUA_PACKAGES != 'true' && fromJSON(steps.global-config.outputs.enable_trivy)

- run: aqua g -i terraform-linters/tflint
shell: bash
working-directory: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}
if: env.TFACTION_SKIP_ADDING_AQUA_PACKAGES != 'true' && fromJSON(steps.global-config.outputs.enable_tflint)

- run: aqua i -l -a
shell: bash
working-directory: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}

- run: terraform-docs . > README.md
shell: bash
working-directory: ${{inputs.module_path}}
working-directory: ${{env.TFACTION_MODULE_PATH}}
10 changes: 0 additions & 10 deletions test-module/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ runs:
- uses: suzuki-shunsuke/tfaction/get-global-config@main
id: global-config

- run: |
github-comment exec \
-config "${GITHUB_ACTION_PATH}/github-comment.yaml" \
-var "tfaction_target:${TFACTION_TARGET}" \
-- terraform init -input=false
shell: bash
working-directory: ${{ env.TFACTION_TARGET }}
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
- uses: suzuki-shunsuke/trivy-config-action@53935fc85b2ba4e195ad91b3be0ef454e33f905d # v0.2.0
if: fromJSON(steps.global-config.outputs.enable_trivy)
with:
Expand Down

0 comments on commit d45bc33

Please sign in to comment.