-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tf deplotment changes #17093
Tf deplotment changes #17093
Changes from 2 commits
0a96c5a
5d81c83
ec92b67
3ba4c87
5766b9f
b4991ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this file doesn't need modifications, we should exclude it from the PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's something wrong here, this file does not need modifications and doesn't need to be in this PR. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,126 @@ | ||
name: Cleanup ACR images | ||
name: Deploy Terraform | ||
|
||
on: | ||
schedule: | ||
- cron: "0 0 * * *" # Runs daily at midnight UTC | ||
|
||
push: | ||
branches: | ||
- main | ||
- production | ||
paths: | ||
- '**.tf' | ||
|
||
env: | ||
AZURE_CREDENTIALS: '{"clientId":"${{ secrets.AZURE_CLIENT_ID }}","clientSecret":"${{ secrets.AZURE_CLIENT_SECRET }}","subscriptionId":"${{ secrets.AZURE_SUBSCRIPTION_ID }}","tenantId":"${{ secrets.AZURE_TENANT_ID }}"}' | ||
|
||
jobs: | ||
cleanup_images: | ||
pre_job: | ||
name: Set Build Environment | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ needs.pre_job.outputs.env_name }} | ||
cancel-in-progress: true | ||
runs-on: ubuntu-latest | ||
strategy: | ||
max-parallel: 1 | ||
matrix: | ||
env: [staging, prod] | ||
outputs: | ||
env_name: ${{ steps.build_vars.outputs.env_name }} | ||
tf_change: ${{ steps.build_vars.outputs.has_terraform_change }} | ||
steps: | ||
- name: "Check out changes" | ||
- name: Check out changes | ||
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 | ||
- name: Build vars | ||
id: build_vars | ||
uses: ./.github/actions/build-vars | ||
|
||
- name: Connect to VPN & Login into Azure | ||
confirm_changes: | ||
name: Check Terraform Stats - ${{ needs.pre_job.outputs.env_name }} | ||
if: ${{ needs.pre_job.outputs.tf_change == 'true' }} | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ needs.pre_job.outputs.env_name }} | ||
cancel-in-progress: true | ||
needs: | ||
- pre_job | ||
environment: ${{ needs.pre_job.outputs.env_name }} | ||
runs-on: ubuntu-latest | ||
outputs: | ||
change_count: ${{ steps.stats1.outputs.change-count }} | ||
steps: | ||
- name: Check Out Changes | ||
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 | ||
- name: Connect to VPN and login to Azure | ||
uses: ./.github/actions/vpn-azure | ||
with: | ||
env-name: ${{ needs.pre_job.outputs.env_name }} | ||
tls-key: ${{ secrets.TLS_KEY }} | ||
ca-cert: ${{ secrets.CA_CRT}} | ||
user-crt: ${{ secrets.USER_CRT }} | ||
user-key: ${{ secrets.USER_KEY }} | ||
sp-creds: ${{ env.AZURE_CREDENTIALS }} | ||
tf-auth: true | ||
- name: Collect Terraform stats | ||
uses: josiahsiegel/terraform-stats@68b8cbe42c494333fbf6f8d90ac86da1fb69dcc2 | ||
id: stats1 | ||
with: | ||
terraform-directory: operations/app/terraform/vars/${{ needs.pre_job.outputs.env_name }} | ||
terraform-version: 1.7.4 | ||
add-args: "-refresh=false" | ||
- name: Run Terraform Plan | ||
run: | | ||
terraform init -input=false | ||
terraform validate | ||
terraform fmt -recursive | ||
terraform plan -out=tf.plan | ||
- name: Run Terraform Apply | ||
run: | | ||
terraform apply -input=false -no-color -lock-timeout=600s -auto-approve tf.plan | ||
|
||
- name: List ${{ matrix.env }} repository images | ||
run: | | ||
az acr login --name pdh${{ matrix.env }}containerregistry | ||
images=$(az acr repository show-tags --name pdh${{ matrix.env }}containerregistry --repository pdh${{ matrix.env }} --orderby time_asc --output table) | ||
echo "Reserving latest 2 from:" | ||
echo "$images" | ||
echo "$images" | head -n -2 > ${{ matrix.env }}-images.txt | ||
sed -i '1,2d' ${{ matrix.env }}-images.txt | ||
|
||
- name: Delete old images in ${{ matrix.env }} env | ||
env: | ||
IMAGE_FILE: ${{ matrix.env }}-images.txt | ||
run: | | ||
if [ -e "$IMAGE_FILE" ]; then | ||
while IFS= read -r image_id; do | ||
az acr repository delete --name pdh${{ matrix.env }}containerregistry --image pdh${{ matrix.env }}:$image_id --yes | ||
if [ $? -eq 0 ]; then | ||
echo "Deleted image: pdh${{ matrix.env }}containerregistry:$image_id" | ||
else | ||
echo "Failed to delete image: pdh${{ matrix.env }}containerregistry:$image_id" | ||
fi | ||
done < "$IMAGE_FILE" | ||
else | ||
echo "File not found: $IMAGE_FILE" | ||
fi | ||
|
||
# Pushing a modified image using an existing tag untags the previously pushed image, | ||
# resulting in an orphaned (or "dangling") image. | ||
# The previously pushed image's manifest--and its layer data--remains in the registry. | ||
# They still need to be removed | ||
|
||
- name: List image manifests in ${{ matrix.env }} env | ||
run: | | ||
az acr login --name pdh${{ matrix.env }}containerregistry | ||
manifest=$(az acr manifest list-metadata -r pdh${{ matrix.env }}containerregistry -n pdh${{ matrix.env }} --orderby time_asc --output tsv --query "[*].{Digest:digest}") | ||
echo "Reserving latest 4 from:" | ||
echo "$manifest" | ||
echo "$manifest" | head -n -4 > ${{ matrix.env }}-untaged-images.txt | ||
approve_deploy: | ||
name: Approve Deploy - ${{ needs.pre_job.outputs.env_name }} | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ needs.pre_job.outputs.env_name }} | ||
cancel-in-progress: true | ||
needs: | ||
- pre_job | ||
- confirm_changes | ||
if: needs.confirm_changes.outputs.change_count > '0' | ||
runs-on: ubuntu-latest | ||
environment: ${{ needs.pre_job.outputs.env_name }}_terraform | ||
steps: | ||
- name: Echo change count | ||
run: echo ${{ needs.confirm_changes.outputs.change_count }} | ||
|
||
- name: Delete image manifest in ${{ matrix.env }} env | ||
env: | ||
UNTAGED_FILE: ${{ matrix.env }}-untaged-images.txt | ||
run_deploy: | ||
name: Run Deploy - ${{ needs.pre_job.outputs.env_name }} | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ needs.pre_job.outputs.env_name }} | ||
cancel-in-progress: true | ||
needs: | ||
- pre_job | ||
- approve_deploy | ||
if: needs.confirm_changes.outputs.change_count > '0' | ||
runs-on: ubuntu-latest | ||
environment: ${{ needs.pre_job.outputs.env_name }} | ||
defaults: | ||
run: | ||
working-directory: operations/app/terraform/vars/${{ needs.pre_job.outputs.env_name }} | ||
steps: | ||
- name: Check Out Changes | ||
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 | ||
- name: Connect to VPN and login to Azure | ||
uses: ./.github/actions/vpn-azure | ||
with: | ||
env-name: ${{ needs.pre_job.outputs.env_name }} | ||
tls-key: ${{ secrets.TLS_KEY }} | ||
ca-cert: ${{ secrets.CA_CRT}} | ||
user-crt: ${{ secrets.USER_CRT }} | ||
user-key: ${{ secrets.USER_KEY }} | ||
sp-creds: ${{ env.AZURE_CREDENTIALS }} | ||
tf-auth: true | ||
- name: Use specific version of Terraform | ||
uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd | ||
with: | ||
terraform_version: 1.7.4 | ||
terraform_wrapper: false | ||
- name: Run Terraform | ||
run: | | ||
if [ -e "$UNTAGED_FILE" ]; then | ||
while IFS= read -r manifest_id; do | ||
az acr repository delete --name pdh${{ matrix.env }}containerregistry --image pdh${{ matrix.env }}@$manifest_id --yes | ||
if [ $? -eq 0 ]; then | ||
echo "Deleted image: pdh${{ matrix.env }}:$manifest_id" | ||
else | ||
echo "Failed to delete image: pdh${{ matrix.env }}:$manifest_id" | ||
fi | ||
done < "$UNTAGED_FILE" | ||
else | ||
echo "File not found: $UNTAGED_FILE" | ||
fi | ||
terraform init -input=false | ||
terraform validate | ||
terraform fmt -recursive | ||
terraform plan -out ${{ needs.pre_job.outputs.env_name }}-tf.plan | ||
terraform apply -input=false -no-color -lock-timeout=600s -auto-approve ${{ needs.pre_job.outputs.env_name }}-tf.plan |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,16 @@ jobs: | |
terraform-directory: operations/app/terraform/vars/${{ needs.pre_job.outputs.env_name }} | ||
terraform-version: 1.7.4 | ||
add-args: "-refresh=false" | ||
|
||
-name: Run Terraform Plan | ||
run: | | ||
terraform init -input=false | ||
terraform validate | ||
terraform fmt -recursive | ||
terraform plan -out=tf.plan | ||
- name: Run Terraform Apply | ||
run: | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good, just one thing - I think this apply step should be conditional on the plan step, using |
||
terraform apply -input=false -no-color -lock-timeout=600s -auto-approve tf.plan | ||
|
||
approve_deploy: | ||
name: Approve Deploy - ${{ needs.pre_job.outputs.env_name }} | ||
concurrency: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was included in the commit by mistake/typo, it should not be in this PR.