5

Using hashicorp/azurerm provider I want to set up a webhook that would trigger a deployment.

I have a web app that loads the newest Docker image on restart. However, I need a scmUri of the resource to setup the webhook:

resource "azurerm_linux_web_app" "web" {
  app_settings = {
    DOCKER_REGISTRY_SERVER_URL      = url
    DOCKER_REGISTRY_SERVER_USERNAME = data.azurerm_key_vault_secret.acr-service-principal-id.value
    DOCKER_REGISTRY_SERVER_PASSWORD = data.azurerm_key_vault_secret.acr-service-principal-password.value
    ...
  }
  ...
}

resource "azurerm_container_registry_webhook" "webhook" {
  service_uri = "<scm URL of the azurerm_linux_web_app.web>/docker/hook"
  ...
}

I have found a way to query the scmUri using various other languages... but nothing using the azurepm provider:

After everything is deployed I can also copy the scmUri manually from Azure, but I would prefer a fully automated setup:

Deployment Center

2
  • 3
    I've now also found an option to define the scmUri as: https://${azurerm_linux_web_app.web.site_credential.0.username}:${azurerm_linux_web_app.web.site_credential.0.password}@${azurerm_linux_web_app.web.name}.scm.azurewebsites.net/docker/hook Commented Feb 1, 2023 at 9:53
  • 1
    You should probably write this as an answer as it looks like the only solution Commented May 28, 2023 at 5:06

1 Answer 1

5

This is what worked for me:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.28.0"
    }
    azapi = {
      source  = "Azure/azapi"
      version = "1.2.0"
    }
  }
}

...

resource "azurerm_linux_web_app" "web" {
  name                = "my-webapp"
  app_settings = {
    DOCKER_ENABLE_CI = "true"
    DOCKER_REGISTRY_SERVER_URL : "*****"
    WEBSITES_PORT : "8080"
  }
  ... # other settings
}


resource "azurerm_container_registry_webhook" "webhook" {
  service_uri         = "https://${azurerm_linux_web_app.web.site_credential.0.name}:${azurerm_linux_web_app.web.site_credential.0.password}@${azurerm_linux_web_app.web.name}.scm.azurewebsites.net/api/registry/webhook"
  location            = "*****"
  registry_name       = "*****"
  name                = "${replace(azurerm_linux_web_app.web.name, "/-|_|\\W/", "")}hook" # only alphanumeric characters allowed
  actions             = ["push"]
  resource_group_name = "*****"
}

The service_uri is a bit different than the URL in the comment under the question (so make sure to check how your webhook URL looks in the web application's deployment center section) and I had to use .web.site_credential.0.name instead of .web.site_credential.0.username to access the correct attribute.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.