-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Append PR number to TF Workspace Name when available (#342)
* Append PR number to TF Workspace Name when available * Re-order names to preserve full WS name on resources with shorter maxlength * Resolves 'Vault name must be between 3-24 alphanumeric characters. The name must begin with a letter, end with a letter or digit, and not contain consecutive hyphens.' * Avoid double dashes in names when 'name' var is not present * scripted workspace name computation * Configurable PR and resource name isolation
- Loading branch information
1 parent
a5ff6b7
commit bf4963a
Showing
13 changed files
with
128 additions
and
26 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
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
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
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
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
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
58 changes: 58 additions & 0 deletions
58
devops/providers/azure-devops/templates/infrastructure/scripts/compute-workspace.yml
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
parameters: | ||
terraformWorkspacePrefix: '' | ||
environmentName: '' | ||
enablePrIsolation: true | ||
stepName: '' | ||
|
||
steps: | ||
- task: Bash@3 | ||
name: ${{ parameters.stepName }} | ||
displayName: Compute and output pipeline variable for Terraform Workspace | ||
env: | ||
TERRAFORM_WORKSPACE_PREFIX: ${{ parameters.terraformWorkspacePrefix }} | ||
ENVIRONMENT_NAME: ${{ parameters.environmentName }} | ||
PR_ISOLATION: ${{ parameters.enablePrIsolation }} | ||
inputs: | ||
targetType: 'inline' | ||
script: | | ||
#!/usr/bin/env bash | ||
# Parses the PullRequest number from sourcebranch, if this is a PR build. Otherwise | ||
# produces a simple combination of prefix+environment to be used as the workspace name. | ||
PR_NUMBER="" | ||
if [[ $PR_ISOLATION == true ]]; then | ||
# BUILD_SOURCEBRANCH will look like "refs/pull/1/merge" if this is a PR branch | ||
IFS='/' # delimiter | ||
read -ra PARTS <<< "$BUILD_SOURCEBRANCH" # split the branch into parts | ||
if [[ "${#PARTS[@]}" == "4" && "${PARTS[0]}" == "refs" && "${PARTS[1]}" == "pull" && "${PARTS[3]}" == "merge" ]]; then | ||
PR_NUMBER="${PARTS[2]}" | ||
fi | ||
fi | ||
OUTVAR="" | ||
if [[ "$PR_NUMBER" != "" ]]; then | ||
if [[ "$TERRAFORM_WORKSPACE_PREFIX" != "" ]]; then | ||
OUTVAR="pr$PR_NUMBER-$TERRAFORM_WORKSPACE_PREFIX" | ||
else | ||
OUTVAR="pr$PR_NUMBER" | ||
fi | ||
else | ||
if [[ "$TERRAFORM_WORKSPACE_PREFIX" != "" ]]; then | ||
OUTVAR="$TERRAFORM_WORKSPACE_PREFIX-$ENVIRONMENT_NAME" | ||
else | ||
OUTVAR="$ENVIRONMENT_NAME" | ||
fi | ||
fi | ||
echo "BUILD_SOURCEBRANCH: $BUILD_SOURCEBRANCH" | ||
echo "TERRAFORM_WORKSPACE_PREFIX: $TERRAFORM_WORKSPACE_PREFIX" | ||
echo "ENVIRONMENT_NAME: $ENVIRONMENT_NAME" | ||
echo "PR_ISOLATION: $PR_ISOLATION" | ||
echo "PR_NUMBER: $PR_NUMBER" | ||
echo "OUTVAR: $OUTVAR" | ||
echo "##vso[task.setvariable variable=TF_WORKSPACE_NAME;isOutput=true]$OUTVAR" | ||
exit 0 |
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
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
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
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
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
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