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
388 stars 177 forks source link

[Feature request] Add `manual` or `workflow_dispatch` flag to hooks to allow for selective execution of hooks #3981

Open john0isaac opened 1 month ago

john0isaac commented 1 month ago

There is a command to run hooks manually azd hooks run <name> [flag].

But there is no flag in the hooks configuration to specify a hook to not run during the service lifecycle. Some of the hooks can only be ran once if you run the hook more than one time it will error.

My proposal is to have a workflow_dispatch flag just like GitHub actions to allow for manual trigger of the hook using the command azd hooks run

It'd be even better if you had a command like when from GitLab workflows. Possible options for when are:

to allow for further customizations but it would be good start if we only had a workflow_dispatch flag

ellismg commented 1 month ago

to allow for further customizations but it would be good start if we only had a workflow_dispatch flag

If these hooks are not tied to the service lifecycle, is the value here that azd hook run just runs a shell script with the values from the .env file injected into the process environment?

I'm wondering, if instead of forcing folks to write stuff in azure.yaml under a workflow_dispatch thing is what we want, or if there should be something like azd env run or something that runs a command or script with the .env files injected? That would also allow you to do something like azd env run bash to get a shell with all the values configured.

john0isaac commented 1 month ago

workflow_dispatch is just an example of what I meant but I didn't mean it in the way you understood it, this example will simplify this.

Old hooks

name: azure-search-openai-demo
metadata:
  template: azure-search-openai-demo@0.0.2-beta
services:
  backend:
    project: ./app/backend
    language: py
    host: appservice
hooks:
  postprovision:
    windows: # Run referenced script that uses environment variables (script shown below)
      shell: pwsh
      run: ./scripts/prepdocs.ps1
      interactive: true
      continueOnError: false
    posix:
      shell: sh
      run: ./scripts/prepdocs.sh
      interactive: true
      continueOnError: false
  postdeploy: # Pull environment variable inline from local device and set in .env file
      shell: sh
      run: azd env set REACT_APP_WEB_BASE_URL ${SERVICE_WEB_ENDPOINT_URL}

New hooks

name: azure-search-openai-demo
metadata:
  template: azure-search-openai-demo@0.0.2-beta
services:
  backend:
    project: ./app/backend
    language: py
    host: appservice
hooks:
  postprovision:
    windows: # Run referenced script that uses environment variables (script shown below)
      shell: pwsh
      run: ./scripts/prepdocs.ps1
      interactive: true
      continueOnError: false
      manual: true       # NEW
    posix:
      shell: sh
      run: ./scripts/prepdocs.sh
      interactive: true
      continueOnError: false
      manual: true      #NEW
  postdeploy: # Pull environment variable inline from local device and set in .env file
      shell: sh
      run: azd env set REACT_APP_WEB_BASE_URL ${SERVICE_WEB_ENDPOINT_URL}

Just a flag to stop the hook from automatically running. It should get it's env vars from azd env.

What you're suggesting is a great feature it maybe a great addition to azd in the future the env vars always bugs me. But my request is very simple, just allow me to run the hook manually using azd environment.

For example, a hook that adds data to a database may only run once if you run it multiple times it will error. So if I do azd up with the current implementation it will error as it might be in the postprovision stage.

jongio commented 2 weeks ago

If the hook is manual, then why is it associated with an action? Why should it be under postprovision if it is always manual?

john0isaac commented 3 days ago

Because that's the stage that it should be ran at as it needs the resources to be created and it needs to be executed before deploying the solution. (for example adding the data at azure-search-openai-demo) The only issue is that some hooks can be ran only once, that's why I wanted the manual flag to control this. So, if I set it to false it will run like it normally does but then after the first run I may set it to true to stop the hook from executing and erroring.

jongio commented 1 day ago

Okay, thanks for the clarification. @wbreza is the hooks original author so he should chime in.