Skip to content

Commit

Permalink
Adding refactorm for AZURE_STORAGE_CONNECTION_STRING
Browse files Browse the repository at this point in the history
  • Loading branch information
shaheislamdfe committed Nov 24, 2024
1 parent 72949f7 commit fdc5454
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions .github/workflows/delete-v2-review-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
types: [closed]
branches: [main]

env:
SERVICE_SHORT: att
TF_VARS_PATH: terraform/aks/workspace_variables

jobs:
delete-v2-review-app:
name: Delete v2 Review App ${{ github.event.pull_request.number }}
Expand All @@ -16,37 +20,56 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Set Environment variables
run: |
terraform_version=$(awk '/{/{f=/^terraform/;next}f' terraform/aks/provider.tf | grep -o [0-9\.]*)
terraform_version=$(awk '/required_version/ {gsub(/[="]/, "", $3); print $3}' terraform/aks/provider.tf)
echo "TERRAFORM_VERSION=$terraform_version" >> $GITHUB_ENV
PR_NUMBER=${{ github.event.pull_request.number }}
TF_STATE_FILE=pr-$PR_NUMBER.tfstate
echo "DEPLOY_ENV=review_aks-$PR_NUMBER" >> $GITHUB_ENV
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
echo "TF_STATE_FILE=$TF_STATE_FILE" >> $GITHUB_ENV
pr_state_file=$(az storage blob list -c att-tfstate \
--connection-string "${{ secrets.AZURE_STORAGE_CONNECTION_STRING }}" \
--prefix $TF_STATE_FILE --query "[].name" -o tsv)
tf_vars_file=terraform/aks/workspace_variables/review_aks.tfvars.json
echo "KEY_VAULT_NAME=$(jq -r '.key_vault_name' "${tf_vars_file}")" >> $GITHUB_ENV
echo "KEY_VAULT_APP_SECRET_NAME=$(jq -r '.key_vault_app_secret_name' "${tf_vars_file}")" >> $GITHUB_ENV
echo "KEY_VAULT_INFRA_SECRET_NAME=$(jq -r '.key_vault_infra_secret_name' "${tf_vars_file}")" >> $GITHUB_ENV
if [ -n "$pr_state_file" ]; then echo "TF_STATE_EXISTS=true" >> $GITHUB_ENV; fi;
tf_backend_vars_file=terraform/aks/workspace_variables/review_aks_backend.tfvars
STORAGE_ACCOUNT_RG="s189t01-att-rv-rg"
echo "STORAGE_ACCOUNT_RG=$STORAGE_ACCOUNT_RG" >> $GITHUB_ENV
tf_vars_file=terraform/aks/workspace_variables/review_aks.tfvars.json
echo "TERRAFORM_VERSION=$terraform_version" >> $GITHUB_ENV
echo "KEY_VAULT_NAME=$(jq -r '.key_vault_name' ${tf_vars_file})" >> $GITHUB_ENV
echo "KEY_VAULT_APP_SECRET_NAME=$(jq -r '.key_vault_app_secret_name' ${tf_vars_file})" >> $GITHUB_ENV
echo "KEY_VAULT_INFRA_SECRET_NAME=$(jq -r '.key_vault_infra_secret_name' ${tf_vars_file})" >> $GITHUB_ENV
. ./global_config/review_aks.sh
echo "STORAGE_ACCOUNT_NAME=${RESOURCE_NAME_PREFIX}${SERVICE_SHORT}tfstate${CONFIG_SHORT}sa" >> $GITHUB_ENV
STORAGE_CONN_STR=$(az storage account show-connection-string \
--resource-group "$STORAGE_ACCOUNT_RG" \
--name "$STORAGE_ACCOUNT_NAME" \
--query 'connectionString' \
--output tsv)
echo "::add-mask::$STORAGE_CONN_STR"
echo "AZURE_STORAGE_CONNECTION_STRING=$STORAGE_CONN_STR" >> $GITHUB_ENV
pr_state_file=$(az storage blob list \
--container-name att-tfstate \
--connection-string "$STORAGE_CONN_STR" \
--prefix "$TF_STATE_FILE" \
--query "[].name" \
--output tsv)
if [ -n "$pr_state_file" ]; then echo "TF_STATE_EXISTS=true" >> $GITHUB_ENV; fi
- name: Use Terraform ${{ env.TERRAFORM_VERSION }}
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TERRAFORM_VERSION }}

- uses: azure/login@v2
if: env.TF_STATE_EXISTS == 'true'
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Validate Azure Key Vault secrets
if: env.TF_STATE_EXISTS == 'true'
uses: DFE-Digital/github-actions/validate-key-vault-secrets@master
Expand All @@ -70,8 +93,10 @@ jobs:
- name: Delete tf state file
if: env.TF_STATE_EXISTS == 'true'
run: |
az storage blob delete -c att-tfstate --name ${{ env.TF_STATE_FILE }} \
--connection-string "${{ secrets.AZURE_STORAGE_CONNECTION_STRING }}"
az storage blob delete \
--container-name att-tfstate \
--name "${{ env.TF_STATE_FILE }}" \
--connection-string "${{ env.AZURE_STORAGE_CONNECTION_STRING }}"
- name: Update ${{ env.DEPLOY_ENV }} status
if: always() && env.TF_STATE_EXISTS == 'true'
Expand Down

0 comments on commit fdc5454

Please sign in to comment.