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.
https://aka.ms/azd
MIT License
404 stars 195 forks source link

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

Open vhvb1989 opened 4 months ago

vhvb1989 commented 4 months ago

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:

See: https://github.com/Azure-Samples/azure-search-openai-demo/issues/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

pamelafox commented 4 months ago

@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

vhvb1989 commented 4 months ago

@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.

pamelafox commented 1 month ago

I just had another developer run into this on a new repo, who was very confused as to why the hooks weren't running and wasn't sure how to debug. They're trying your workaround now.