azure-dev icon indicating copy to clipboard operation
azure-dev copied to clipboard

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

Open vhvb1989 opened this issue 1 year ago • 2 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: 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

  • 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 avatar May 16 '24 20:05 vhvb1989

@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 avatar May 17 '24 22:05 pamelafox

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

vhvb1989 avatar May 18 '24 00:05 vhvb1989

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.

pamelafox avatar Aug 21 '24 15:08 pamelafox

Any updates on the hooks for CI/CD?

mohapatras avatar Mar 06 '25 11:03 mohapatras

@vhvb1989 , want to double check with you. I believe we can close this. True?

Dev to prod blog series: GH Action & Azure pipeline

puicchan avatar Aug 21 '25 20:08 puicchan

This is still a bug @puicchan .

vhvb1989 avatar Aug 26 '25 22:08 vhvb1989

Hi @vhvb1989. Thanks for sharing this workaround! I spent quite a bit of time trying to figure out why my hook wasn’t working before I found this issue.

ronaldbosma avatar Oct 10 '25 14:10 ronaldbosma

@JeffreyCA can you take a look at this one? @nzthiago - functions team ran into this isssue and were looking for a fix.

rajeshkamal5050 avatar Nov 21 '25 17:11 rajeshkamal5050

@JeffreyCA can you take a look at this one? @nzthiago - functions team ran into this isssue and were looking for a fix.

Sure, it looks like there were several attempts to fix this issue before, but neither made it in:

  • https://github.com/Azure/azure-dev/pull/4011
  • https://github.com/Azure/azure-dev/pull/3940

Are we open to revisiting them? At a glance it looks like #4011 is a more contained change that should address this specific issue and #3940 is a larger one that introduces a new middleware layer.

JeffreyCA avatar Nov 21 '25 19:11 JeffreyCA

Thanks @JeffreyCA let us try doing a contained change and we can ask functions team to try it?

rajeshkamal5050 avatar Nov 21 '25 21:11 rajeshkamal5050

Thank you @JeffreyCA and team - just tested with the AZD CLI version in the PR and it fixed our issue, post-provision scripts now ran even if an environment didn't exist before running the azd provision. Should we expect this to be in the next AZD CLI release?

nzthiago avatar Nov 25 '25 17:11 nzthiago

Thank you @JeffreyCA and team - just tested with the AZD CLI version in the PR and it fixed our issue, post-provision scripts now ran even if an environment didn't exist before running the azd provision. Should we expect this to be in the next AZD CLI release?

Thanks for confirming @nzthiago! Yes, we're planning our next release to be early December - will keep you posted

JeffreyCA avatar Nov 25 '25 18:11 JeffreyCA