Concatenate env variables and strings

Hello community,

I want to check in my workflow if a specific file starts with a specific string. But for the string I have to concatenate an expression and a hashtag and this syntax isn’t right.

How does the right syntax looks like?

Wrong example:

if: startsWith( '# $version', $file-content)

Thank you!

The easiest solution that comes to my mind is to assemble both parts in environment variables or outputs, whichever you prefer. Example with environment variables, assuming you already have version as an environment variable and that the first line of the file is sufficient for the check:

  - name: setup version check
    run: |
      echo "::set-env name=version_prefix::# ${version}"
      echo "::set-env name=file_content::$(head -n 1 ./file)"
  - name: that conditional step
    if: startsWith(env.file_content, env.version_prefix)
    run: # do stuff here

Note that I switched the order of file content and version prefix: According to the documentation the first argument to startsWith() is the string to search in, and the second one the value it must start with to return true.

Hi @benwick91,

If it’s step level if expression, please follow the comment from airtower-luna.
If it’s job level if expression, please try below code:

    runs-on: ubuntu-latest
      version: ${{ steps.step1.outputs.version }}
      filecontent: ${{ steps.step2.outputs.filecontent }}
    - id: step1
      run: echo "::set-output name=version::# ${version}"
    - id: step2
      run: echo "::set-output name=filecontent::$(head -n 1 ./file)"
    runs-on: ubuntu-latest
    needs: job1
    if: startsWith(needs.job1.outputs.filecontent, needs.job1.outputs.version)


1 Like

Without the file content aspect, it would be possible like so:

if: startsWith(format('# {0}', env.CONTENT), env.VERSION)

If I understand the docs correctly, then the env context only knows about environment variables that are set in the workflow itself or via ::set-env however, not ones that you may have set on the system e.g. in a bash script with set or export.