Skip to content

Commit

Permalink
Refactor: pipeline template with runtime parameters (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-tran authored Oct 5, 2023
2 parents 9e32f19 + f94534b commit 626eb25
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 47 deletions.
25 changes: 16 additions & 9 deletions terraform/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@
set -e


ENV=$1
ENV="$1"
AGENCY="$2"

if [ $# -ne 1 ]; then
echo "Usage: $0 <env>"
if [ $# -ne 2 ]; then
echo "Usage: $0 <env> <agency>"
exit 1
fi

source "$AGENCY/env"

echo "Setting the subscription for the Azure CLI..."
az account set --subscription="MST IT"
az account set --subscription="$SUBSCRIPTION"

printf "Intializing Terraform...\n\n"
terraform init
terraform init -backend-config="$AGENCY/local.tfbackend"

printf "\n\nSelecting the Terraform workspace...\n"

# matching logic in pipeline/workspace.py
if [ "$ENV" = "prod" ]; then
terraform workspace select default
else
terraform workspace select "$ENV"
WORKSPACE=$([[ "$ENV" == "prod" ]] && echo "default" || echo "$ENV")

# if the workspace exists, this check will select it
WORKSPACE_EXISTS=$(terraform workspace select "$WORKSPACE" 2> /dev/null; echo $?)
# creating a new workspace also selects it
if [ "$WORKSPACE_EXISTS" -ne 0 ]; then
terraform workspace new "$WORKSPACE"
fi
echo "Done!"
3 changes: 0 additions & 3 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ terraform {
}

backend "azurerm" {
# needs to match pipeline/azure-pipelines.yml
resource_group_name = "courtesy-cards-eligibility-terraform"
storage_account_name = "courtesycardsterraform"
container_name = "tfstate"
key = "terraform.tfstate"
}
Expand Down
31 changes: 31 additions & 0 deletions terraform/mst/azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
trigger:
branches:
include:
- dev
- test
- prod
tags:
include:
- 20??.??.?*-rc?*
- 20??.??.?*
# only run for changes to Terraform files
paths:
include:
- terraform/*

pr:
branches:
include:
- "*"
paths:
include:
- terraform/*

pool:
vmImage: ubuntu-latest

stages:
- template: ../pipeline/deploy.yml
parameters:
resource_group: mst-courtesy-cards-eligibility-terraform
storage_account: mstcourtesycardstf
1 change: 1 addition & 0 deletions terraform/mst/env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SUBSCRIPTION="MST IT"
2 changes: 2 additions & 0 deletions terraform/mst/local.tfbackend
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resource_group_name="mst-courtesy-cards-eligibility-terraform"
storage_account_name="mstcourtesycardstf"
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
trigger:
branches:
include:
- dev
- test
- prod
tags:
include:
- 20??.??.?*-rc?*
- 20??.??.?*
# only run for changes to Terraform files
paths:
include:
- terraform/*

pr:
branches:
include:
- "*"
paths:
include:
- terraform/*

pool:
vmImage: ubuntu-latest
parameters:
- name: resource_group
type: string
- name: storage_account
type: string

stages:
- stage: TerraformPlan
Expand Down Expand Up @@ -67,8 +47,8 @@ stages:
# service connection
backendServiceArm: deployer
# needs to match main.tf
backendAzureRmResourceGroupName: courtesy-cards-eligibility-terraform
backendAzureRmStorageAccountName: courtesycardsterraform
backendAzureRmResourceGroupName: ${{ parameters.resource_group }}
backendAzureRmStorageAccountName: ${{ parameters.storage_account }}
backendAzureRmContainerName: tfstate
backendAzureRmKey: terraform.tfstate
- task: TerraformTaskV3@3
Expand Down Expand Up @@ -97,11 +77,11 @@ stages:
displayName: "Publish tfplan for use in TerraformApply"
artifact: savedPlan
condition: |
or(
in(variables['Build.SourceBranchName'], 'dev', 'test', 'prod'),
eq(variables['setvars.tag_type'], 'test'),
eq(variables['setvars.tag_type'], 'prod')
)
or(
in(variables['Build.SourceBranchName'], 'dev', 'test', 'prod'),
eq(variables['setvars.tag_type'], 'test'),
eq(variables['setvars.tag_type'], 'prod')
)
- stage: TerraformApply
dependsOn: TerraformPlan
variables:
Expand Down Expand Up @@ -149,8 +129,8 @@ stages:
# service connection
backendServiceArm: deployer
# needs to match main.tf
backendAzureRmResourceGroupName: courtesy-cards-eligibility-terraform
backendAzureRmStorageAccountName: courtesycardsterraform
backendAzureRmResourceGroupName: ${{ parameters.resource_group }}
backendAzureRmStorageAccountName: ${{ parameters.storage_account }}
backendAzureRmContainerName: tfstate
backendAzureRmKey: terraform.tfstate
- task: TerraformTaskV3@3
Expand Down
2 changes: 1 addition & 1 deletion terraform/suppress.arm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"contentVersion": "1.0.0.0",
"parameters": {
"metricAlertID": {
"type": "string"
"type": "String"
}
},
"resources": [
Expand Down

0 comments on commit 626eb25

Please sign in to comment.