GitHub Actions expression functions: string manipulation?

Viewed 4378

In a GitHub Actions workflow definition file, there's a set of built-in functions that you can use in expressions.

For example: ${{ toJson(github) }}

Are there any string manipulation functions that can be used in an expression, such as toLowerCase?

The documentation page doesn't mention any. However, I'm wondering if Github uses some sort of standard templating / expression eval library under the hood which has provides a larger set of functions out of the box.

3 Answers

Impossible. GitHub expressions doesn't allow string modification, only concatenation.

You could do almost the same with a custom step in a build job, but this means that you won't be able to use that variable everywhere (for example "processed" environment name is out of the question).

env:
  UPPERCASE_VAR: "HELLO"
steps:
  - id: toLowerCase
    run: INPUT=${{ env.UPPERCASE_VAR }} echo "::set-output name=lowerCaseValue::${INPUT,,}"

  - run: echo ${{steps.toLowerCase.outputs.lowerCaseValue}}

I wanted to replace some chars in git version strings and was able to make a step like so:

  - name: prepare version string
    id: prep_version
    run: |
      export test_version=$(echo ${{ steps.tag_version.outputs.new_version }} |  sed 's/[^0-9,a-z,A-Z]/\-/g')
      echo ::set-output name=version::$test_version

that worked pretty good for me... so really we have anything that we could put on a cmd line

I usually start by setting all global variables that I will use throughout the workflow.

jobs:
  variables: 
    outputs:
      tag_name: ${{ steps.var.outputs.tag_name}}
    runs-on: "ubuntu-latest"
    steps:
      - name: Setting global variables
        uses: actions/github-script@v6
        id: var
        with:
          script: |
            core.setOutput('tag_name', '${{ github.head_ref }}'.toLowerCase().replaceAll(/[/.]/g, '-').trim('-'));

Usage:

  deploy:
    needs: [build, variables]
    runs-on: [ self-hosted, preview ]
    env:
      TAG_NAME: ${{ needs.variables.outputs.tag_name }}
    step:
      -name: Echo variables 
       run: |
         echo ${{ needs.variables.outputs.tag_name }}
         echo ${{ env.TAG_NAME}}
Related