Reputation: 9879
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.
Upvotes: 23
Views: 36587
Reputation: 2968
A new variant converting a string to lowercase without deprecated set-output
.
A new operator >>${GITHUB_OUTPUT}
is in use.
env:
VAR_UPPERCASE: "HELLO"
jobs:
MyJob:
steps:
- name: Convert to lowercase
id: convert2lowercase
run: INPUT=${{ env.VAR_UPPERCASE }}; echo "VAR_LOWERCASE=${INPUT,,}">>${GITHUB_OUTPUT}
- run: echo ${{steps.convert2lowercase.outputs.VAR_LOWERCASE}}
Upvotes: 3
Reputation: 410
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.
Upvotes: 4
Reputation: 89
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}}
Upvotes: 3
Reputation: 61
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
Upvotes: 4
Reputation: 804
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}}
Upvotes: 10