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 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..6edaae6 --- /dev/null +++ b/select-branch-workspace/action.yml @@ -0,0 +1,33 @@ +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 + working-directory: + description: the directory the step should be run from + 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: | + pwd + cd ${{ inputs.working-directory }} + 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 + 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'