How to Check if a string exist in another string with github action?

68 views Asked by At

I have a github action, i have a list of words like this:

'["alpha", "beta", "preview", "experiment"]'
 

and i have a suffix string like this:

suffix = steps.get-version.outputs.version-suffix

now i want to check if any of words in list exist in suffix or not? currently i am using this:

 - run: echo "IS_PRE_RELEASE=true" >> $env:GITHUB_ENV    
   if: contains(fromJson('["alpha", "beta", "preview", "experiment"]'), steps.get-version.outputs.version-suffix)


but there is an issue:

if

suffix = beta
output: IS_PRE_RELEASE updated to true

if

suffix = beta1
output: IS_PRE_RELEASE does not set

so issue is, only beta (alpha,...) accepted and other type of string does not accepted (beta1, beta.1,....)

i tried to change contains to startsWith but i get same result. how i can fix this?

1

There are 1 answers

0
Katana On BEST ANSWER

If you are using a windows os in your action, you can use powershell commands:

  • name: Run PowerShell script run: | # Define the list of strings $list = @("beta", "alpha", "preview")

        # Define the suffix variable
        $suffix = "Beta134"
    
        # Loop through the list and check if the suffix starts with any item
        foreach ($item in $list) {
          # Convert both strings to lower case for case-insensitive comparison
          if ($suffix.ToLower().StartsWith($item.ToLower())) {
            # Set the PREX environment variable to true and exit the loop
    
            echo "IS_PRE_RELEASE=true" >> $env:GITHUB_ENV
            break
          }
        }