Substring function?

Hello there,

Assume an action that triggers when a PR is made to a branch “ticket-123”, where “ticket-” is fixed. I can easily make a conditional step, using “if: startsWith()”.

Question: I need to pass “123” as a parameter to an action:

- uses: an/action
    parameter-name: ???

How do I get “123” from “ticket-123”. I cannot seem to find in the docs anything about substring being supported, so there should be some other way.

Any help will be greatly appreciated. Thanks!

Unfortunately, there’s currently no built-in substring function. As a workaround, you can use another action and set the substring as an output variable:

- id: get-id
  run: |
    id=$(echo ${{github.event.client_payload.value}} | cut -d- -f2)
    echo "::set-output name=id::$id"
- run: echo "${{}}"

In this example, I’ve added a step with the ID get-id. That step splits the value that I want to split using cut and then sets it as an output parameter, which can be read and used by subsequent actions.

If you’re not familiar with cut, it’s a shell command that splits a string based on a delimiter (-d) and outputs a specific field (-f). You could use any shell we support, or you could even easily use JavaScript with the actions/github-script action:

- id: get-id
  uses: actions/github-script@0.9.0
    script: |
      const id = context.payload.client_payload.value.split('-')[1]
      core.setOutput('id', id)
- run: echo "${{}}"
1 Like

@jclemthank you very much :slight_smile: Using outputs from one step as inputs to another will work perfectly fine as a workaround!

Thank you, github-script action is awesome, if you take a closer look at what it can do, you can do some fun things like:

but, what is even better, you can store this script in file in your repository and test it locally by just running it with required environment variables being defined with INTPUT_ prefix

1 Like