3

I am trying to setup a custom action for GitHub Actions and currently this is what I have:

name: 'Install Dependencies'

inputs:
    os:
        description: 'The OS to fetch the dependencies for'
        required: True
    

runs:
    using: "composite"
    steps:
        - run: echo I am a test
          shell: bash
          if: startsWith(os, 'Linux')

What I am trying to do is to eventually have a bunch of different shell scripts that are tailored to do the job for a specific OS and therefore in my action, I want to select the appropriate script based on the os parameter that was passed into it.

When I am invoking the action as shown above though, I get Unexpected value 'if'. So my question is: How can I only run execute the run command if a condition is met?

1

2 Answers 2

2

The simplest option is probably to use a naming convention for your scripts and run call them with the name of the os:

myscript.$(os).sh
Sign up to request clarification or add additional context in comments.

2 Comments

Nope the if still doesn't work. The example you showed was for specifying the job within a workflow, but I am trying to do this in an action that can then be invoked from the workflow. Using such a naming scheme is actually a great idea though...
Hm yeah I guess that would be an option. However I wanted to create the action in order to not have to put all different cases into my main workflow and encapsulate it into a separate action instead... But I think I'll stick to the naming convention thing. So if you update your answer so that it only suggests that, I'll accept is as the correct one (don't want anyone to mistakenly assume that the if approach here was working) :)
1

A workaround is to use bash's || and negating the desired condition to ensure the command that follows is only executed if the condition is true (which is negated to false, which runs the command after ||):

- run: ${{ !startsWith(os, 'Linux') }} || echo "I am a test"
  shell: bash

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.