Project hooks don't work when there's not environment, like in CI/CD
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
.azurefolder: 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 initwith minimal template) - Add a
preprovisionhook 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-promptand 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.
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.
Any updates on the hooks for CI/CD?
@vhvb1989 , want to double check with you. I believe we can close this. True?
Dev to prod blog series: GH Action & Azure pipeline
This is still a bug @puicchan .
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.
@JeffreyCA can you take a look at this one? @nzthiago - functions team ran into this isssue and were looking for a fix.
@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.
Thanks @JeffreyCA let us try doing a contained change and we can ask functions team to try it?
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?
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