Regular expression pattern matching is possible in github actions?

Is regular expression pattern matching is possible in Github actions?

I have few release branches with name release/v1.1, release/v1.2 etc…

Let’s say my current branch is release/v1.1. How do i pass it as a regular expression with the pattern that matches my branch ?

if: contains(github.ref, ‘release/*’)

or

if [[ ${{ env.BRANCH }} =~ release/.* ]]

contains() is an Actions function, but it doesn’t support regular expressions. If you just want to know if the branch name starts with release/, you could do something like this:

if: startsWith(github.ref, 'refs/heads/release/')

That is Bash syntax, you can use it within a run step if using Bash as the shell (default on Linux).

2 Likes

Somehow the below condition doesn’t work for me if i use it in run step.

if [[ ${{ env.BRANCH }} =~ release/.* ]]

Is it the right way to declare ?

You’ll have to provide more context to answer that, ideally the full workflow and a description of how it doesn’t work, like in a bug report: What do you expect, what actually happens?

And of course that only works if you actually have BRANCH environment variable. :slightly_smiling_face:

Thanks. I tried with the below one and it worked.

if: startsWith(github.ref, ‘refs/heads/release/’)

Considering base branch as release, how can i check for the pull request level ?

1 Like

I’m not sure what you mean, could you explain? Something related to merging into a release branch?

Ok. Let’s say i have multiple release branches with the name release/v1.1, release/v1.2 (This is at the branch level the below condition works

if: startsWith(github.ref, ‘refs/heads/release/’)

Similarly if developer raises a Pull request for any of these release branches release/v1.1 or release/v1.2. I know something like below will work for exact matching branch…How can i have the if condition to check for multiple pull request release branches ?

if: github.event.pull_request.base.ref == ‘release/v1.1’
env:
LABEL: test

Because based on the above PR for the respective release branches, i need to set an environment variable. But for all the release branches i am going to use the same environment variable as test.

One option would be to use startWith() on github.event.pull_request.base.ref.

If you need more complex logic the best thing might be to feed the base ref to a custom command/script, and decide what to write to GITHUB_ENV in there. That way you have a lot more options to write the logic you need.

Thanks. This helps!!!

1 Like

This post was flagged by the community and is temporarily hidden.