26

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.

2
  • 1
    I've exactly the same question, did you got an answer somewhere ? Commented Nov 25, 2020 at 9:43
  • 1
    No, unfortunately not. I usually work around this with Bash scripts. Commented Nov 26, 2020 at 1:46

6 Answers 6

10

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}}
Sign up to request clarification or add additional context in comments.

6 Comments

What about toUpperCase?
This is showing a blank output when I do the echo, why?
Unfortunately, the output is blank.
There MUST be semicolon before echo command INPUT=${{ env.UPPERCASE_VAR }}; echo .....
::set-output is deprecated, see github.blog/changelog/…
|
5

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}}

Comments

5

After encountering some issues with the top voted answer, I found that it was not working (showing a blank string) because it only works inside the context. Here is the solution I found:

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

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

Here, we're defining the UPPERCASE_VAR environment variable as an input variable of the toLowerCase step. Then, in the run command, we're using the ${UPPERCASE_VAR,,} syntax to convert the value of the variable to lowercase and assign it to the lowerCaseValue output variable. Finally, we're defining the UPPERCASE_VAR environment variable inside the step, so that it's available within the shell command.

Comments

5

A new variant converting a string to lowercase without deprecated set-output.

The new environment variable >>${GITHUB_OUTPUT} is in use.

env:
  VAR_UPPERCASE: "HELLO"

jobs:
  MyJob:
    steps:
    - name: Convert to lowercase
      id: convert2lowercase
      run: INPUT=$VAR_UPPERCASE; echo "VAR_LOWERCASE=${INPUT,,}">>${GITHUB_OUTPUT}
    - run: echo ${{steps.convert2lowercase.outputs.VAR_LOWERCASE}}

Comments

4

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

Comments

1

use the tr command in the runner OS

somejob:
  name: a job with a step that needs lower case
  runs-on: ubuntu-latest
  steps:
      - name: this is the job that needs lower case
        env:
          all_lower_case: $(echo ${{ inputs.upper_and_lower_case }} |  tr '[:upper:]' '[:lower:]')
        run: |
          echo "this should be like toLower: ${{env.all_lower_case }}"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.