Skip to content

Commit a563e7e

Browse files
authored
Refactor: terraform app service deployment (#2123)
2 parents 30725eb + b1bf8b4 commit a563e7e

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

docs/deployment/README.md

+3-9
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ The Django application gets built into a [Docker image][dockerfile] with [NGINX]
1010
The application is deployed to an [Azure Web App Container][az-webapp] using three separate environments for `dev`, `test`,
1111
and `prod`.
1212

13-
A [GitHub Action][gh-actions] per environment is responsible for building that branch's image and pushing to [GitHub Container
14-
Registry (GHCR)][ghcr].
15-
16-
GitHub POSTs a [webhook][gh-webhooks] to the Azure Web App when an [image is published to GHCR][gh-webhook-event], telling
17-
Azure to restart the app and pull the latest image.
13+
The [Deploy](deploy-workflow) workflow is responsible for building that branch's image and pushing to [GitHub Container
14+
Registry (GHCR)][ghcr]. It also deploys to the Azure Web App, telling Azure to restart the app and pull the latest image.
1815

1916
You can view what Git commit is deployed for a given environment by visitng the URL path `/static/sha.txt`.
2017

@@ -30,14 +27,11 @@ Docker images for each of the deploy branches are available from GitHub Containe
3027

3128
- [Repository Package page](https://github.com/cal-itp/benefits/pkgs/container/benefits)
3229
- Image path: `ghcr.io/cal-itp/benefits`
33-
- Image tags: `dev`, `test`, `prod`
3430

3531
[oet]: https://techblog.cdt.ca.gov/2020/06/cdt-taking-the-lead-in-digital-transformation/
3632
[app-service-containers]: https://docs.microsoft.com/en-us/azure/app-service/configure-custom-container
3733
[app-service]: https://docs.microsoft.com/en-us/azure/app-service/overview
34+
[deploy-workflow]: https://github.com/cal-itp/benefits/blob/dev/.github/workflows/deploy.yml
3835
[dockerfile]: https://github.com/cal-itp/benefits/blob/dev/Dockerfile
3936
[az-webapp]: https://azure.microsoft.com/en-us/services/app-service/containers/
40-
[gh-actions]: https://docs.github.com/en/actions
41-
[gh-webhook-event]: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#package
42-
[gh-webhooks]: https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhooks
4337
[ghcr]: https://github.com/features/packages

docs/deployment/infrastructure.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,10 @@ Use the following shorthand for conveying the Resource Type as part of the Resou
180180

181181
## Azure environment setup
182182

183-
The following steps are required to set up the environment, with linked issues to automate them:
183+
The following steps are required to set up the environment:
184184

185185
- `terraform apply`
186-
- Set up Slack notifications by [creating a Slack email](https://slack.com/help/articles/206819278-Send-emails-to-Slack) for the [#benefits-notify](https://cal-itp.slack.com/archives/C022HHSEE3F) channel, then [setting it as a Secret in the Key Vault](https://learn.microsoft.com/en-us/azure/key-vault/secrets/quick-create-portal#add-a-secret-to-key-vault) named `slack-benefits-notify-email`
186+
- Set up Slack notifications by [creating a Slack email](https://slack.com/help/articles/206819278-Send-emails-to-Slack) for the [#notify-benefits](https://cal-itp.slack.com/archives/C022HHSEE3F) channel, then [setting it as a Secret in the Key Vault](https://learn.microsoft.com/en-us/azure/key-vault/secrets/quick-create-portal#add-a-secret-to-key-vault) named `slack-benefits-notify-email`
187187
- Set required [App Service configuration](../configuration/environment-variables.md) and [configuration](../configuration/data.md) by setting values in Key Vault (the mapping is defined in [app_service.tf](https://github.com/cal-itp/benefits/blob/dev/terraform/app_service.tf))
188-
- [Set up webhook from GitHub](https://github.com/cal-itp/benefits/settings/hooks) to [App Service Deployment Center](https://learn.microsoft.com/en-us/azure/app-service/deploy-ci-cd-custom-container?tabs=acr&pivots=container-linux) for the `Packages` event
189188

190189
This is not a complete step-by-step guide; more a list of things to remember. This may be useful as part of [incident response](https://docs.google.com/document/d/1qtev8qItPiTB4Tp9FQ87XsLtWZ4HlNXqoe9vF2VuGcY/edit#).

docs/deployment/workflows.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ Using the `github.actor` and built-in `GITHUB_TOKEN` secret
2323

2424
### 3. Build and push image to GitHub Container Registry (GHCR)
2525

26-
Build the root [`Dockerfile`][dockerfile], tagging with both the branch name (e.g. `dev`) and the SHA from the HEAD commit.
26+
Build the root [`Dockerfile`][dockerfile], tagging with the SHA from the HEAD commit.
2727

2828
Push this image:tag into [GHCR][ghcr].
2929

3030
### 4. App Service deploy
3131

32-
Each Azure App Service instance is configured to [listen to a webhook from GitHub, then deploy the image][webhook].
32+
Push the new image:tag to the Azure App Service instance.
3333

3434
[deploy]: https://github.com/cal-itp/benefits/blob/dev/.github/workflows/deploy.yml
3535
[dockerfile]: https://github.com/cal-itp/benefits/blob/dev/Dockerfile
3636
[ghcr]: https://github.com/features/packages
3737
[gh-actions-trigger]: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
38-
[webhook]: https://docs.microsoft.com/en-us/azure/app-service/deploy-ci-cd-custom-container

terraform/app_service.tf

+17-6
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ resource "azurerm_linux_web_app" "main" {
2525
site_config {
2626
ftps_state = "Disabled"
2727
vnet_route_all_enabled = true
28-
application_stack {
29-
docker_image = "ghcr.io/cal-itp/benefits"
30-
docker_image_tag = local.env_name
31-
}
3228
}
3329

3430
identity {
@@ -52,8 +48,7 @@ resource "azurerm_linux_web_app" "main" {
5248
# app setting used solely for refreshing secrets - see https://github.com/MicrosoftDocs/azure-docs/issues/79855#issuecomment-1265664801
5349
"change_me_to_refresh_secrets" = "change me in the portal to refresh all secrets",
5450

55-
"DOCKER_ENABLE_CI" = "true",
56-
"DOCKER_REGISTRY_SERVER_URL" = "https://ghcr.io/",
51+
"DOCKER_ENABLE_CI" = "false",
5752
"WEBSITE_HTTPLOGGING_RETENTION_DAYS" = "99999",
5853
"WEBSITE_TIME_ZONE" = "America/Los_Angeles",
5954
"WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false",
@@ -110,6 +105,22 @@ resource "azurerm_linux_web_app" "main" {
110105
}
111106
}
112107

108+
resource "azurerm_app_service_source_control" "main" {
109+
app_id = azurerm_linux_web_app.main.id
110+
repo_url = "https://github.com/cal-itp/benefits"
111+
branch = local.env_name
112+
rollback_enabled = true
113+
114+
github_action_configuration {
115+
generate_workflow_file = false
116+
117+
container_configuration {
118+
image_name = "cal-itp/benefits"
119+
registry_url = "https://ghcr.io/"
120+
}
121+
}
122+
}
123+
113124
resource "azurerm_app_service_custom_hostname_binding" "main" {
114125
hostname = local.hostname
115126
app_service_name = azurerm_linux_web_app.main.name

0 commit comments

Comments
 (0)