Using github.inputs and variables in workflows - Stack Overflow
Support list input type
GitHub Actions Check Empty Dispatch Input - Stack Overflow
PS Script with user inputs / GitHub WorkFlow
Videos
Good morning / Evening All, happy Friday 😄
Looking for a few pointers on how to best achieve a problem using a PS Script / Workflow.
I have a PS Script that will term users accounts in AD. When the script executes it will prompt the IT personal to enter the username of the term, which the users input, and then goes on it's merry way to term the account.
I have been giving the task of executing the script within a GitHub workflow. The end goal will be automation from HR system which will trigger the workflow and complete the term, without the need to run through term script, but this is a long way off.
In the interim, we will need the manually run the workflow. Which is not that end of the world.
My problem is however, how best to get around the user input part in the script. From my initial research, u cannot use inputs within the Workflow.
Would u have any workarounds to this?
Any help would be much appreciated
inputs are available to workflows triggered by the workflow_dispatch event only (and to any workflows that are called by dispatched workflows).
When a workflow with inputs is triggered on a schedule (or on push/pull) all the input values are null. Any defaults that are set in the inputs section have no effect.
Therefore you need to set the defaults at the point where they are needed in the workflow. A convenient method for doing this uses Github expressions.
For string and choice inputs you can use the || expression operator to set the default value.
For boolean inputs that default to false you can use || expression to set the default value.
For boolean inputs that default to true you can use the contains() function to set the default value.
For example:
on:
schedule:
- cron: '15 0,18 * * 0-5'
workflow_dispatch:
inputs:
springProfile:
required: true
type: choice
options:
- staging
- production
logLevel:
required: true
type: string
isFalseWhenScheduled:
required: true
type: boolean
isTrueWhenScheduled:
required: true
type: boolean
jobs:
test:
uses: ./.github/workflows/run-job-with-params.yml
secrets: inherit
with:
springProfile: ${{ inputs.springProfile || 'staging' }}
logLevel: ${{ inputs.logLevel || 'DEBUG' }}
isFalseWhenScheduled: ${{ inputs.isFalseWhenScheduled || false }}
isTrueWhenScheduled: ${{ !contains(inputs.isTrueWhenScheduled, 'false') }}
In the above example, if the workflow is triggered on schedule:
- the
springProfileparameter is set to 'staging'. - the
logLevelparameter is set to 'DEBUG'. - the
isFalseWhenScheduledparameter is set to false - the
isTrueWhenScheduledparameter is set to true
If the job is triggered on workflow_dispatch: then the above parameters are set to the inputs: values.
Boolean value defaults
The contains function casts its first parameter to a string, and null is cast to the empty string ''. As mentioned, when triggered on schedule, inputs parameters are all set to null.
So:
| Trigger method | isTrueWhenScheduled value | input value resolves to |
|---|---|---|
| workflow_dispatch | true | !contains('true', 'false') = true |
| workflow_dispatch | false | !contains('false', 'false') = false (*) |
| schedule | null | !contains('', 'false') = true |
(*) This combination is what necessitates the use of contains. If contains is not used and you do ${{ inputs.isTrueWhenScheduled || true }} this will force this value to true even if it is set false in the workflow dispatch event.
And:
| Trigger method | isFalseWhenScheduled value | input value resolves to |
|---|---|---|
| workflow_dispatch | true | true || false = true |
| workflow_dispatch | false | false || false = false |
| schedule | null | null || false = false |
You can set an env variable with a default value:
env:
typeOfTesting: ${{ github.event_name == 'schedule' && 'stage-test-local-All' || github.event.inputs.typeOfTesting }}
then use env.typeOfTesting instead of inputs.typeOfTesting.
Where can I find all available input types for workflow_dispatch? There is only example.