Search code examples
githubgithub-actions

run step in github actions when command is not found


I want to setup X if X is not found in github action. X being anything like java, node, etc.

To do so I currently use:


    - name: Check java found
      run: java --version
      id: check_java
      shell: bash
      continue-on-error: true

    - name: Install java
      if: steps.check_java.outcome == 'failure'
      uses: actions/setup-java@v3
      with:
        distribution: "zulu"
        java-version: "17.x"
        cache: "gradle"

While this works, it outputs an error Error: Process completed with exit code 1.

Is there a way to do the above without an error written ?


Solution

  • You could replace your first step with something like this, and then use the step output:

        - name: Check java found
          id: check_java
          run: |
            if java --version; then
                echo "installed=true" >> "$GITHUB_OUTPUT"
            else
                echo "installed=false" >> "$GITHUB_OUTPUT"
            fi
    
        - name: Install java
          if: steps.check_java.outputs.installed == 'false'
          uses: actions/setup-java@v3
          with:
            distribution: "zulu"
            java-version: "17.x"
            cache: "gradle"
    

    Because the command is in an if clause, it won't trigger the "error on exit" condition.