diff --git a/terraform/init.sh b/terraform/init.sh index ece98f21..bdb4bff2 100755 --- a/terraform/init.sh +++ b/terraform/init.sh @@ -3,25 +3,32 @@ set -e -ENV=$1 +ENV="$1" +AGENCY="$2" -if [ $# -ne 1 ]; then - echo "Usage: $0 " +if [ $# -ne 2 ]; then + echo "Usage: $0 " 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!" diff --git a/terraform/main.tf b/terraform/main.tf index 0403446e..0343b806 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -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" } diff --git a/terraform/mst/azure-pipelines.yml b/terraform/mst/azure-pipelines.yml new file mode 100644 index 00000000..90c1b67f --- /dev/null +++ b/terraform/mst/azure-pipelines.yml @@ -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 diff --git a/terraform/mst/env b/terraform/mst/env new file mode 100644 index 00000000..29896c6d --- /dev/null +++ b/terraform/mst/env @@ -0,0 +1 @@ +SUBSCRIPTION="MST IT" diff --git a/terraform/mst/local.tfbackend b/terraform/mst/local.tfbackend new file mode 100644 index 00000000..39ede935 --- /dev/null +++ b/terraform/mst/local.tfbackend @@ -0,0 +1,2 @@ +resource_group_name="mst-courtesy-cards-eligibility-terraform" +storage_account_name="mstcourtesycardstf" diff --git a/terraform/pipeline/azure-pipelines.yml b/terraform/pipeline/deploy.yml similarity index 88% rename from terraform/pipeline/azure-pipelines.yml rename to terraform/pipeline/deploy.yml index 16b7aa3c..bf89e83f 100644 --- a/terraform/pipeline/azure-pipelines.yml +++ b/terraform/pipeline/deploy.yml @@ -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 @@ -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 @@ -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: @@ -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 diff --git a/terraform/suppress.arm.json b/terraform/suppress.arm.json index 95fd5b7d..51bfb6a3 100644 --- a/terraform/suppress.arm.json +++ b/terraform/suppress.arm.json @@ -3,7 +3,7 @@ "contentVersion": "1.0.0.0", "parameters": { "metricAlertID": { - "type": "string" + "type": "String" } }, "resources": [