Substring function? #26625
-
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:
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! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 17 replies
-
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:
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:
|
Beta Was this translation helpful? Give feedback.
-
@jclemthank you very much :slight_smile: Using outputs from one step as inputs to another will work perfectly fine as a workaround! |
Beta Was this translation helpful? Give feedback.
-
Thank you, 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 |
Beta Was this translation helpful? Give feedback.
-
Now you can have named runs based on github context, a built-in substring function would be very useful. For example using |
Beta Was this translation helpful? Give feedback.
-
I'm also looking for a way to obtain the first 7 of the github_sha. I was first surprised that this value didn't exist in github contexts or environment variables, and secondly surprised that github yaml doesn't have any substring or length function. The only way to have that first 7, aka short sha, in multiple jobs, is to either recalculate it on every job, or add overhead to pass it between every job, both of which seem cumbersome. |
Beta Was this translation helpful? Give feedback.
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:
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 suppo…