Skip to content

add conditional execution of action steps #834

Closed

Description

@igagis

Describe the enhancement
Add possibility to use if: option for action steps of composite actions.

Code Snippet

inputs:
  echo-bla:
    description: 'echo bla-bla-bla if not empty'
    required: true
runs:
  using: "composite"
  steps:
    - run: echo "start"
      shell: bash
    - run: echo "bla bla bla"
      shell: bash
      if: ${{ inputs.echo-bla }}
    - run: echo "stop"
      shell: bash

Activity

  1. louisgv commented on Feb 2, 2021

    @louisgv

    The specific case described in the ticket can be done with plain shell script. Something like the example below:

          run: |
            npm install
            [[ "${{ inputs.package }}" = true ]] && npm run package || true
    

    More complex - well we shall wait for #646

  2. igagis commented on Feb 2, 2021

    @igagis
    Author

    @louisgv Well, ok, actually, I want the if: in composite actions to avoid running in shell which are not present in the system. So that I could make universal actions which would run in linux images, as well as windows ones (i.e. bash/powershell).
    So, implementing check in bash is not an option for me since bash can be absent.

  3. dcharles-godaddy commented on Jul 29, 2021

    @dcharles-godaddy

    I'd like to have an action that does different things depending upon cancelled(), failure(), or success(), and these functions do not appear to be available inside of a run:

    The workflow is not valid. .github/workflows/success.yaml (Line: 8, Col: 14): Unrecognized function: 'success'. Located at position 1 within expression: success()
    
  4. tahirmt commented on Aug 18, 2021

    @tahirmt

    Is there any plan to add if conditionals to composite actions now that they support uses

  5. potatoqualitee commented on Aug 19, 2021

    @potatoqualitee

    lol, didn't even realize i needed an if conditional as well. I'm basically building a wrapper for actions/cache@v2 which requires if: steps.cacher.outputs.cache-hit != 'true' within my composite action. looking forward to this implementation!

  6. smokedlinq commented on Aug 25, 2021

    @smokedlinq

    Also have a need for this. It's the last thing keeping me from using composites similar to Azure DevOps pipeline templates...

  7. sondrelg commented on Aug 27, 2021

    @sondrelg

    actions/cache@v2 which requires if: steps.cacher.outputs.cache-hit != 'true' within my composite action

    I came here looking for an answer to this - and with some inspiration from @louisgv I think I might have been able to come up with a temporary workaround until ifs are fully supported. Posting it here just in case anyone finds it useful:

    Instead of this:

    - uses: actions/cache@v2
      id: cache-venv
      with:
        path: .venv
        key: some-key-0
    - run: poetry install --no-interaction --no-root
      if: steps.cache-venv.outputs.cache-hit != 'true'

    It looks like this would also work

    - uses: actions/cache@v2
      id: cache-venv
      with:
        path: .venv
        key: some-key-${{ inputs.pip-cache-key }}
    - run: |
        if echo ${{ steps.cache-venv.outputs.cache-hit }} | grep -c "true"
        then
          echo "Cache hit - skipping dependency installation"
        else
          poetry install --no-interaction --no-root
        fi
      shell: bash

    Here is a full example workflow. Here is an example of a project using the remote workflow with a cold cache, and here with a warm cache 👍

    It seems to work the same at least 🤞

  8. potatoqualitee commented on Aug 27, 2021

    @potatoqualitee

    super cool interim solution, thanks for sharing, @sondrelg !

  9. sebastianluszczek commented on Aug 27, 2021

    @sebastianluszczek

    @sondrelg would like to use your result but have a little different problem

    I got some input (not required)

    inputs:
        test:
            description: "Test command to run"
            required: false
    runs:
        using: "composite"
        steps:
            - id: install
              run: |
                  npm ci
              shell: bash
            - id: test
              run: |
                  if [ -z "${{ inputs.test }}" ]
                  then
                    echo -e "test exist: ${{ inputs.test }}"
                    "${{ inputs.test }}"
                  else
                    echo -e "\nno test specified"
                  fi
              shell: bash
            - id: build
              run: |
                  mkdir dist
                  npm run build
              shell: bash
    

    but it always get into first block even if I not specify test while using action

  10. sondrelg commented on Aug 27, 2021

    @sondrelg

    I had some trouble with missing values as well @sebastianluszczek - perhaps it would help to set a default value for your input, and check against that, rather than using -z?

  11. added a commit that references this issue on Aug 27, 2021
  12. 50 remaining items

  13. electriquo commented on Nov 25, 2021

    @electriquo

    why do one need to find this thread and not read it in the original documentation?! :(

  14. solarmosaic-kflorence commented on Nov 29, 2021

    @solarmosaic-kflorence

    @foolioo it is being tracked here now github/docs#12378

  15. AlexDCraig commented on Dec 10, 2021

    @AlexDCraig

    Does the if conditional not work for outputs of previous steps?

    For example:

        - name: Apply Tag of Commit Hash to Master
          id: tag_master_with_commit
          if: ${{ steps.tag_check.outputs.tag_exists }} != true
          uses: mathieudutour/github-tag-action@v5.6
          with:
            github_token: ${{ inputs.github_token }}
            custom_tag: ${{ steps.short_sha.outputs.short_sha }}
            tag_prefix: 'master-'
    

    This doesn't appear to actually run a conditional statement against the previous step's output and thus always runs.

  16. solarmosaic-kflorence commented on Dec 10, 2021

    @solarmosaic-kflorence

    @AlexDHoffer it does work, although I have also had problems with matching on boolean true. I have solved it by ensuring that the output is always cast as a string (e.g. "true"), and then matching instead on the string value, like steps.tag_check.outputs.tag_exists != 'true'. I feel like this might be a bug.

    EDIT: I think it's this one #1483

  17. ccolella-mdc commented on Feb 7, 2022

    @ccolella-mdc

    I found the following works:

    if: ${{ steps.approvals.outputs.VALUE  == 'integration' }}
    

    While the below does not:

    if: ${{ steps.approvals.outputs.VALUE  }} == 'integration'
    

    Is this the expected behavior?

    yes. This should also work.

    if: steps.approvals.outputs.VALUE  == 'integration'
    
  18. added 4 commits that reference this issue on Feb 23, 2022
  19. albert-k-nylas commented on May 4, 2022

    @albert-k-nylas

    Is it expected that secrets are not available inside a conditional step? I'm running the following and it gives me the result below. Notice that credentials were not passed (no *** printed)
    Screen Shot 2022-05-04 at 1 26 33 PM

    - name: Set up Cloud SDK to access cluster ${{ env.GKE_STAGING_CLUSTER_NAME }}
            if: ${{ inputs.cluster == 'staging' }}
            uses: google-github-actions/get-gke-credentials@fb08709ba27618c31c09e014e1d8364b02e5042e #v0.2.1
            with:
              project_id: ${{ env.PROJECT_ID }}
              cluster_name: ${{ env.GKE_STAGING_CLUSTER_NAME }}
              location: ${{ env.LOCATION }}
              credentials: ${{ secrets.GKE_STAGING_SECRET }}
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions