From 9aaacb261b6568c47046e95c8d56e0ed214c729b Mon Sep 17 00:00:00 2001 From: "OPENSESAME\\sara.voss" Date: Wed, 25 Sep 2024 15:49:48 -0400 Subject: [PATCH 1/5] added composite action for select-branch-workspace --- select-branch-workspace/README.md | 5 +++++ select-branch-workspace/action.yml | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 select-branch-workspace/README.md create mode 100644 select-branch-workspace/action.yml diff --git a/select-branch-workspace/README.md b/select-branch-workspace/README.md new file mode 100644 index 0000000..f3c5a0c --- /dev/null +++ b/select-branch-workspace/README.md @@ -0,0 +1,5 @@ +# Overview + +Takes in a string input meant to be the name of a branch. +The branch name is sanitized to be used as a workspace name. +The resulting workspace name is then used to select the workspace, creating it if it does not exist. diff --git a/select-branch-workspace/action.yml b/select-branch-workspace/action.yml new file mode 100644 index 0000000..5ee1bc9 --- /dev/null +++ b/select-branch-workspace/action.yml @@ -0,0 +1,26 @@ +name: Sanitize Branch Name for Terraform Workspace +description: Removes everything before the final "/" from the branch name and limits the characters. +inputs: + branch_name: + description: The name of the branch to be sanitized + required: true +outputs: + workspace_name: + description: "Branch name after the final forward slash" + value: ${{ steps.sanitize_branch.outputs.sanitized_branch_name }} + +runs: + using: "composite" + steps: + - name: Sanitize input branch + id: sanitize_branch + shell: bash + run: | + SANITIZED_BRANCH_NAME=$(echo ${{ github.ref_name }} | sed 's/.*\///' | sed 's/\(^.\{1,50\}\).*/\1/') + echo "Sanitized branch name: $SANITIZED_BRANCH_NAME" + terraform workspace select -or-create $SANITIZED_BRANCH_NAME + echo "sanitized_branch_name=$SANITIZED_BRANCH_NAME" >> $GITHUB_OUTPUT + + # sed 's/.*\///' removes everything before the final slash + # sed 's/\(^.\{1,50\}\).*/\1/' #Character limit is 50. Terraform workspace allows up to 90 characters + # TODO: Remove special characters. Eg sed 's/[#$%*@]//g' From 53df2f2d64f7ddaaa57bccae25e450c5cb69bc2c Mon Sep 17 00:00:00 2001 From: "OPENSESAME\\sara.voss" Date: Wed, 25 Sep 2024 17:34:52 -0400 Subject: [PATCH 2/5] add optional build artifact directory for tf plan --- .github/workflows/tf_validate_plan_env_roots.yml | 7 ++++++- .github/workflows/tf_validate_plan_single_root.yml | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tf_validate_plan_env_roots.yml b/.github/workflows/tf_validate_plan_env_roots.yml index cfd58cd..a91044e 100644 --- a/.github/workflows/tf_validate_plan_env_roots.yml +++ b/.github/workflows/tf_validate_plan_env_roots.yml @@ -15,6 +15,10 @@ on: description: Terraform version to use type: string required: true + build-artifact-directory: + description: Optional directory to create before running terraform plan. Path should be relative to the terraform envirionment root directories + type: string + required: false secrets: ORG_READ_ONLY_SSH_KEY: required: true @@ -24,7 +28,7 @@ on: jobs: TF-Validate-Plan-Roots: name: TF Validate/Plan ENV Roots - uses: ./.github/workflows/tf_validate_plan_single_root.yml + uses: opensesame/core-github-actions/.github/workflows/tf_validate_plan_single_root.yml@beta strategy: fail-fast: false # continues to run jobs even if one fails matrix: @@ -42,6 +46,7 @@ jobs: terraform-workspace: ${{ inputs.terraform-workspace || matrix.environment }} terraform-root: terraform/${{ matrix.environment }} terraform-version: ${{ inputs.terraform-version }} + build-artifact-directory: ${{ inputs.build-artifact-directory }} secrets: ORG_READ_ONLY_SSH_KEY: ${{ secrets.ORG_READ_ONLY_SSH_KEY }} ORG_GITHUB_PACKAGES_READ_ONLY_TOKEN: ${{ secrets.ORG_GITHUB_PACKAGES_READ_ONLY_TOKEN }} diff --git a/.github/workflows/tf_validate_plan_single_root.yml b/.github/workflows/tf_validate_plan_single_root.yml index d58ac60..3d84068 100644 --- a/.github/workflows/tf_validate_plan_single_root.yml +++ b/.github/workflows/tf_validate_plan_single_root.yml @@ -31,6 +31,10 @@ on: description: Terraform version to use type: string required: true + build-artifact-directory: + description: Optional directory to create before running terraform plan. Path should be relative to the terraform-root supplied + type: string + required: false secrets: ORG_READ_ONLY_SSH_KEY: required: true @@ -98,6 +102,11 @@ jobs: terraform_version: ${{ inputs.terraform-version }} terraform_wrapper: false # required to access terraform outputs after apply + # Step to create the directory if the build-artifact-directory input is provided + - name: Ensure Build Artifact Directory Exists + if: ${{ inputs.build-artifact-directory != '' }} + run: mkdir -p ${{ inputs.build-artifact-directory }} + - name: Terraform Init run: terraform init -upgrade @@ -107,7 +116,7 @@ jobs: - name: Select Workplace run: terraform workspace select -or-create ${{ inputs.terraform-workspace }} - - name: Terrafrom Re-Init + - name: Terraform Re-Init run: terraform init -upgrade - name: Terraform Plan From d2dc319abd6f948764b9938ab5d49c2e7e3698a6 Mon Sep 17 00:00:00 2001 From: "OPENSESAME\\sara.voss" Date: Wed, 25 Sep 2024 18:03:19 -0400 Subject: [PATCH 3/5] branch name based on input --- select-branch-workspace/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/select-branch-workspace/action.yml b/select-branch-workspace/action.yml index 5ee1bc9..13a8aea 100644 --- a/select-branch-workspace/action.yml +++ b/select-branch-workspace/action.yml @@ -16,7 +16,7 @@ runs: id: sanitize_branch shell: bash run: | - SANITIZED_BRANCH_NAME=$(echo ${{ github.ref_name }} | sed 's/.*\///' | sed 's/\(^.\{1,50\}\).*/\1/') + SANITIZED_BRANCH_NAME=$(echo ${{ inputs.branch_name }} | sed 's/.*\///' | sed 's/\(^.\{1,50\}\).*/\1/') echo "Sanitized branch name: $SANITIZED_BRANCH_NAME" terraform workspace select -or-create $SANITIZED_BRANCH_NAME echo "sanitized_branch_name=$SANITIZED_BRANCH_NAME" >> $GITHUB_OUTPUT From a3845e44d4eb437fae50c6bbe7279ccf81f47e1f Mon Sep 17 00:00:00 2001 From: "OPENSESAME\\sara.voss" Date: Wed, 25 Sep 2024 18:25:24 -0400 Subject: [PATCH 4/5] add troubleshooting values --- select-branch-workspace/action.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/select-branch-workspace/action.yml b/select-branch-workspace/action.yml index 13a8aea..d6dbfdd 100644 --- a/select-branch-workspace/action.yml +++ b/select-branch-workspace/action.yml @@ -16,6 +16,8 @@ runs: id: sanitize_branch shell: bash run: | + pwd + terraform workspace list SANITIZED_BRANCH_NAME=$(echo ${{ inputs.branch_name }} | sed 's/.*\///' | sed 's/\(^.\{1,50\}\).*/\1/') echo "Sanitized branch name: $SANITIZED_BRANCH_NAME" terraform workspace select -or-create $SANITIZED_BRANCH_NAME From 3e769cbbfbbaff8ca927bc63e14f31d0f9e28f68 Mon Sep 17 00:00:00 2001 From: "OPENSESAME\\sara.voss" Date: Wed, 25 Sep 2024 18:28:45 -0400 Subject: [PATCH 5/5] add working directory input to select workspace --- select-branch-workspace/action.yml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/select-branch-workspace/action.yml b/select-branch-workspace/action.yml index d6dbfdd..6edaae6 100644 --- a/select-branch-workspace/action.yml +++ b/select-branch-workspace/action.yml @@ -1,11 +1,14 @@ name: Sanitize Branch Name for Terraform Workspace description: Removes everything before the final "/" from the branch name and limits the characters. inputs: - branch_name: + branch-name: description: The name of the branch to be sanitized required: true + working-directory: + description: the directory the step should be run from + required: true outputs: - workspace_name: + workspace-name: description: "Branch name after the final forward slash" value: ${{ steps.sanitize_branch.outputs.sanitized_branch_name }} @@ -16,9 +19,11 @@ runs: id: sanitize_branch shell: bash run: | + pwd + cd ${{ inputs.working-directory }} pwd terraform workspace list - SANITIZED_BRANCH_NAME=$(echo ${{ inputs.branch_name }} | sed 's/.*\///' | sed 's/\(^.\{1,50\}\).*/\1/') + SANITIZED_BRANCH_NAME=$(echo ${{ inputs.branch-name }} | sed 's/.*\///' | sed 's/\(^.\{1,50\}\).*/\1/') echo "Sanitized branch name: $SANITIZED_BRANCH_NAME" terraform workspace select -or-create $SANITIZED_BRANCH_NAME echo "sanitized_branch_name=$SANITIZED_BRANCH_NAME" >> $GITHUB_OUTPUT