Skip to content

Commit e2f5d83

Browse files
add terraform init to test pipeline
1 parent 946a6b5 commit e2f5d83

File tree

4 files changed

+113
-79
lines changed

4 files changed

+113
-79
lines changed

.github/workflows/test.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ jobs:
2121
client-id: ${{ secrets.AZURE_CLIENT_ID }}
2222
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
2323
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
24+
25+
- name: Setup Terraform
26+
uses: hashicorp/setup-terraform@v3
27+
with:
28+
terraform_wrapper: false
29+
terraform_version: 1.11.4
30+
31+
- name: Terraform Init
32+
run: make terraform-init
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
ENVIRONMENT=poc
22
AZURE_SUBSCRIPTION="Lung Cancer Screening - Dev"
3+
HUB_SUBSCRIPTION="Lung Cancer Screening - Dev"
4+
STORAGE_ACCOUNT_RG=rg-tfstate-poc-uks
35
TERRAFORM_MODULES_REF=main
46
ENABLE_SOFT_DELETE=false
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
terraform {
2+
required_providers {
3+
azurerm = {
4+
source = "hashicorp/azurerm"
5+
version = "4.34.0"
6+
}
7+
azuread = {
8+
source = "hashicorp/azuread"
9+
version = "3.4.0"
10+
}
11+
}
12+
backend "azurerm" {
13+
container_name = "terraform-state"
14+
}
15+
}
16+
17+
provider "azurerm" {
18+
features {}
19+
}
20+
21+
provider "azurerm" {
22+
alias = "hub"
23+
subscription_id = var.hub_subscription_id
24+
25+
features {}
26+
}
27+
28+
provider "azuread" {}

scripts/terraform/terraform.mk

Lines changed: 74 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,74 @@
1-
# This file is for you! Edit it to implement your own Terraform make targets.
2-
3-
# ==============================================================================
4-
# Custom implementation - implementation of a make target should not exceed 5 lines of effective code.
5-
# In most cases there should be no need to modify the existing make targets.
6-
7-
TF_ENV ?= dev
8-
STACK ?= ${stack}
9-
TERRAFORM_STACK ?= $(or ${STACK}, infrastructure/environments/${TF_ENV})
10-
dir ?= ${TERRAFORM_STACK}
11-
12-
terraform-init: # Initialise Terraform - optional: terraform_dir|dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set], terraform_opts|opts=[options to pass to the Terraform init command, default is none/empty] @Development
13-
make _terraform cmd="init" \
14-
dir=$(or ${terraform_dir}, ${dir}) \
15-
opts=$(or ${terraform_opts}, ${opts})
16-
17-
terraform-plan: # Plan Terraform changes - optional: terraform_dir|dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set], terraform_opts|opts=[options to pass to the Terraform plan command, default is none/empty] @Development
18-
make _terraform cmd="plan" \
19-
dir=$(or ${terraform_dir}, ${dir}) \
20-
opts=$(or ${terraform_opts}, ${opts})
21-
22-
terraform-apply: # Apply Terraform changes - optional: terraform_dir|dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set], terraform_opts|opts=[options to pass to the Terraform apply command, default is none/empty] @Development
23-
make _terraform cmd="apply" \
24-
dir=$(or ${terraform_dir}, ${dir}) \
25-
opts=$(or ${terraform_opts}, ${opts})
26-
27-
terraform-destroy: # Destroy Terraform resources - optional: terraform_dir|dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set], terraform_opts|opts=[options to pass to the Terraform destroy command, default is none/empty] @Development
28-
make _terraform \
29-
cmd="destroy" \
30-
dir=$(or ${terraform_dir}, ${dir}) \
31-
opts=$(or ${terraform_opts}, ${opts})
32-
33-
terraform-fmt: # Format Terraform files - optional: terraform_dir|dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set], terraform_opts|opts=[options to pass to the Terraform fmt command, default is '-recursive'] @Quality
34-
make _terraform cmd="fmt" \
35-
dir=$(or ${terraform_dir}, ${dir}) \
36-
opts=$(or ${terraform_opts}, ${opts})
37-
38-
terraform-validate: # Validate Terraform configuration - optional: terraform_dir|dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set], terraform_opts|opts=[options to pass to the Terraform validate command, default is none/empty] @Quality
39-
make _terraform cmd="validate" \
40-
dir=$(or ${terraform_dir}, ${dir}) \
41-
opts=$(or ${terraform_opts}, ${opts})
42-
43-
clean:: # Remove Terraform files (terraform) - optional: terraform_dir|dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set] @Operations
44-
make _terraform cmd="clean" \
45-
dir=$(or ${terraform_dir}, ${dir}) \
46-
opts=$(or ${terraform_opts}, ${opts})
47-
48-
_terraform: # Terraform command wrapper - mandatory: cmd=[command to execute]; optional: dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set], opts=[options to pass to the Terraform command, default is none/empty]
49-
dir=$(or ${dir}, ${TERRAFORM_STACK})
50-
source scripts/terraform/terraform.lib.sh
51-
terraform-${cmd} # 'dir' and 'opts' are accessible by the function as environment variables, if set
52-
53-
# ==============================================================================
54-
# Quality checks - please DO NOT edit this section!
55-
56-
terraform-shellscript-lint: # Lint all Terraform module shell scripts @Quality
57-
for file in $$(find scripts/terraform -type f -name "*.sh"); do
58-
file=$${file} scripts/shellscript-linter.sh
59-
done
60-
61-
# ==============================================================================
62-
# Configuration - please DO NOT edit this section!
63-
64-
terraform-install: # Install Terraform @Installation
65-
make _install-dependency name="terraform"
66-
67-
# ==============================================================================
68-
69-
${VERBOSE}.SILENT: \
70-
_terraform \
71-
clean \
72-
terraform-apply \
73-
terraform-destroy \
74-
terraform-fmt \
75-
terraform-init \
76-
terraform-install \
77-
terraform-plan \
78-
terraform-shellscript-lint \
79-
terraform-validate \
1+
DOCKER_IMAGE=
2+
REGION=UK South
3+
APP_SHORT_NAME=luncs
4+
5+
poc: # Target the poc environment - make poc <action>
6+
$(eval include infrastructure/environments/poc/variables.sh)
7+
8+
dev: # Target the dev environment - make dev <action>
9+
$(eval include infrastructure/environments/dev/variables.sh)
10+
11+
preprod: # Target the preprod environment - make preprod <action>
12+
$(eval include infrastructure/environments/preprod/variables.sh)
13+
14+
review: # Target the review infrastructure, or a review app if PR_NUMBER is used - make review <action> [PR_NUMBER=<pr_number>]
15+
$(eval include infrastructure/environments/review/variables.sh)
16+
$(if ${PR_NUMBER}, $(eval export TF_VAR_deploy_infra=false), $(eval export TF_VAR_deploy_container_apps=false))
17+
$(if ${PR_NUMBER}, $(eval export ENVIRONMENT=pr-${PR_NUMBER}), $(eval export ENVIRONMENT=review))
18+
19+
db-setup:
20+
$(if ${TF_VAR_deploy_container_apps},, scripts/bash/db_run_job.sh ${ENVIRONMENT} ${PR_NUMBER})
21+
22+
ci: # Skip manual approvals when running in CI - make ci <env> <action>
23+
$(eval AUTO_APPROVE=-auto-approve)
24+
$(eval SKIP_AZURE_LOGIN=true)
25+
26+
set-azure-account: # Set the Azure account for the environment - make <env> set-azure-account
27+
[ "${SKIP_AZURE_LOGIN}" != "true" ] && az account set -s ${AZURE_SUBSCRIPTION} || true
28+
29+
resource-group-init: set-azure-account get-subscription-ids # Initialise the resources required by terraform - make <env> resource-group-init
30+
$(eval STORAGE_ACCOUNT_NAME=sa${APP_SHORT_NAME}${ENV_CONFIG}tfstate)
31+
scripts/bash/resource_group_init.sh "${REGION}" "${HUB_SUBSCRIPTION_ID}" "${ENABLE_SOFT_DELETE}" "${ENV_CONFIG}" "${STORAGE_ACCOUNT_RG}" "${STORAGE_ACCOUNT_NAME}" "${APP_SHORT_NAME}" "${ARM_SUBSCRIPTION_ID}"
32+
33+
get-subscription-ids: # Retrieve the hub subscription ID based on the subscription name in ${HUB_SUBSCRIPTION} - make <env> get-subscription-ids
34+
$(eval HUB_SUBSCRIPTION_ID=$(shell az account show --query id --output tsv --name ${HUB_SUBSCRIPTION}))
35+
$(if ${ARM_SUBSCRIPTION_ID},,$(eval export ARM_SUBSCRIPTION_ID=$(shell az account show --query id --output tsv)))
36+
37+
terraform-init-no-backend: # Initialise terraform modules only and update terraform lock file - make <env> terraform-init-no-backend
38+
rm -rf infrastructure/modules/dtos-devops-templates
39+
git -c advice.detachedHead=false clone --depth=1 --single-branch --branch ${TERRAFORM_MODULES_REF} \
40+
https://github.com/NHSDigital/dtos-devops-templates.git infrastructure/modules/dtos-devops-templates
41+
terraform -chdir=infrastructure/terraform init -upgrade -backend=false
42+
43+
terraform-init: set-azure-account get-subscription-ids # Initialise Terraform - make <env> terraform-init
44+
$(eval STORAGE_ACCOUNT_NAME=sa${APP_SHORT_NAME}${ENV_CONFIG}tfstate)
45+
$(eval export ARM_USE_AZUREAD=true)
46+
47+
rm -rf infrastructure/modules/dtos-devops-templates
48+
git -c advice.detachedHead=false clone --depth=1 --single-branch --branch ${TERRAFORM_MODULES_REF} \
49+
https://github.com/NHSDigital/dtos-devops-templates.git infrastructure/modules/dtos-devops-templates
50+
51+
terraform -chdir=infrastructure/terraform init -upgrade -reconfigure \
52+
-backend-config=subscription_id=${HUB_SUBSCRIPTION_ID} \
53+
-backend-config=resource_group_name=${STORAGE_ACCOUNT_RG} \
54+
-backend-config=storage_account_name=${STORAGE_ACCOUNT_NAME} \
55+
-backend-config=key=${ENVIRONMENT}.tfstate
56+
57+
$(eval export TF_VAR_app_short_name=${APP_SHORT_NAME})
58+
# $(eval export TF_VAR_docker_image=${DOCKER_IMAGE}:${DOCKER_IMAGE_TAG})
59+
$(eval export TF_VAR_environment=${ENVIRONMENT})
60+
# $(eval export TF_VAR_env_config=${ENV_CONFIG})
61+
# $(eval export TF_VAR_hub=${HUB})
62+
# $(eval export TF_VAR_hub_subscription_id=${HUB_SUBSCRIPTION_ID})
63+
64+
terraform-plan: terraform-init # Plan Terraform changes - make <env> terraform-plan DOCKER_IMAGE_TAG=abcd123
65+
terraform -chdir=infrastructure/terraform plan -var-file ../environments/${ENV_CONFIG}/variables.tfvars
66+
67+
terraform-apply: terraform-init # Apply Terraform changes - make <env> terraform-apply DOCKER_IMAGE_TAG=abcd123
68+
terraform -chdir=infrastructure/terraform apply -var-file ../environments/${ENV_CONFIG}/variables.tfvars ${AUTO_APPROVE}
69+
70+
terraform-destroy: terraform-init # Destroy Terraform resources - make <env> terraform-destroy
71+
terraform -chdir=infrastructure/terraform destroy -var-file ../environments/${ENV_CONFIG}/variables.tfvars ${AUTO_APPROVE}
72+
73+
terraform-validate: terraform-init-no-backend # Validate Terraform changes - make <env> terraform-validate
74+
terraform -chdir=infrastructure/terraform validate

0 commit comments

Comments
 (0)