Azure / azure-dev

A developer CLI that reduces the time it takes for you to get started on Azure. The Azure Developer CLI (azd) provides a set of developer-friendly commands that map to key stages in your workflow - code, build, deploy, monitor, repeat.

Home Page:https://aka.ms/azd

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Project hooks don't work when there's not environment, like in CI/CD

vhvb1989 opened this issue · comments

Consider the next azd project sample:

name: ehooks
hooks:
  preprovision:
    shell: sh
    continueOnError: false
    interactive: true
    run: echo "Preprovisioning..."

If we run azd provision --no-prompt and:

  • there's is not an .azure folder: The hook is not triggered b/c there is no environment.

hooks.go:52: azd environment is not available, skipping all hook registrations

See: Azure-Samples/azure-search-openai-demo#1603

This is an issue when azd runs in CI, because the .azure folder is not in the repo. Instead, azd uses AZURE_ENV_NAME to create the .azure folder with a new env using the name from that env var.
However, the hooks registration happens before the environment is created, so the hooks are ignored.

Workaround:

For CI, add one step to create the environment before calling azd provision, like:

      - name: Create azd env
        run: azd env new $AZURE_ENV_NAME
        env:
          AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}

The --no-prompt flag is not supported for azd env new, so, we need to use the env var from github as the argument for creating the environment.

Reproduce locally

  • Create a simple azd project, (for example, use azd init with minimal template)
  • Add a preprovision hook like the one mentioned above
  • delete folder .azure (leaving the state as it would be in CI)
  • set env var AZURE_ENV_VAR to something
  • run azd provision --no-prompt and observe how hook is ignored and the .azure folder is created. If you run it again, the hook is now honored (b/c the env is there)

@vhvb1989 Huh, I'm confused, as I had to write code today in another project to explicitly disable the preprovision/postprovision hooks as they were not able to run in CI. Not sure if I had a different azd version or something?
https://github.com/Azure-Samples/openai-chat-app-entra-auth-local/actions/runs/9132623645/job/25114347007

@pamelafox , in your case, your pipeline has a call to azd env set FOOO something here: https://github.com/Azure-Samples/openai-chat-app-entra-auth-local/blob/main/.github/workflows/azure-dev.yaml#L80

That's is creating the azd env.. then when azd provision runs, hooks are correctly invoked.