resp78
resp78

Reputation: 1534

How to get inputs in github yml

I would like to setup a workflow in github yml such that I have some default values for variables and also would like to be able to manually provide the values to these variables when running the workflow manually.

I understood that we can use workflow_dispatch to set some input variables when running manually. However, when the workflow is executed as part of a code push, these variables (runTests and uploadArtifacts) are coming as null.

name: Example
on:
  workflow_dispatch:
    inputs:
       runTests:
         description: run tests
         required: true
         default: true
         type: Boolean
       uploadArtifacts:
         description: upload artifacts 
         required: true
         default: false
         type: Boolean
  push:
    branches:
    - master
    - main 
    - release/*

jobs:
  Build_Job:
    runs-on: [self-hosted, raya]
    steps:

    - name: Publish drop artifact
      if: ${{ inputs.uploadArtifacts }} 
      uses: actions/upload-artifact@v2
      with:
        name: Installer
        path: "${{ runner.temp }}/AppxPackages/"

Upvotes: 0

Views: 153

Answers (1)

GuiFalourd
GuiFalourd

Reputation: 23190

It's the expected behavior, as the inputs will be set only if the workflow_dispacth event is used to trigger the workflow.

If you want the workflow to perform a default operation when the code is pushed, you would need to implement the if condition differently.

Example:

on:
  push:
  workflow_dispatch:
    inputs:
       test1:
         description: test1
         required: false
         default: false
         type: boolean
       test2:
         description: test2 
         required: false
         default: true
         type: boolean

jobs:
  job1: # will always run
    runs-on: ubuntu-latest
    steps:
        - run: |
            echo ${{ inputs.test1 }}
            echo ${{ inputs.test2 }}
            echo ${{ github.event_name }}

  job2: # will only run on a workflow_dispatch event, if test1 input is true
    runs-on: ubuntu-latest
    if: ${{ inputs.test1 }}
    steps:
        - run: |
            echo ${{ inputs.test1 }}
            echo ${{ inputs.test2 }}
            echo ${{ github.event_name }}

  job3: # will only run on a workflow_dispatch event, if test2 input is true
    runs-on: ubuntu-latest
    if: ${{ inputs.test2 }}
    steps:
        - run: |
            echo ${{ inputs.test1 }}
            echo ${{ inputs.test2 }}
            echo ${{ github.event_name }}

  job4: # will only run on a push event
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'push' }}
    steps:
        - run: |
            echo ${{ inputs.test1 }}
            echo ${{ inputs.test2 }}
            echo ${{ github.event_name }}

  job5: # will only run on a push event OR if inputs.test2 is true on a workflow_dispatch event
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'push' || inputs.test2 }}
    steps:
        - run: |
            echo ${{ inputs.test1 }}
            echo ${{ inputs.test2 }}
            echo ${{ github.event_name }}

I understand that what you want to achieve is something similar to the job5 example above (you could even add a github.ref context variable to the expression if you only want a job to be executed if the branch name is something specific).

I made some tests if you want to have a look:

Upvotes: 1

Related Questions