Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to replace slashes with dashes and set it an environment variable in github actions

I am trying to use a combination of branch name and github run number as the image tag in my github actions ci/cd process

Long story short: I want to have MODIFIED_BRANCH_NAME set as an environment variable and use it later

this is my workflow file

name: CI/CD mktplc-catalog

on:
  push:
env:
  BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
  MODIFIED_BRANCH_NAME: # What goes here?
.
.
.

suppose the branch name is feature/add-foo I need MODIFIED_BRANCH_NAME to be set as feature-add-foo

How should I do this?

like image 882
Amin Ba Avatar asked Feb 21 '26 06:02

Amin Ba


1 Answers

You could use shell parameter expansion adding an extra step, like:

    steps:
      - name: Sets MODIFIED_BRANCH_NAME
        env:
          name: "${{env.BRANCH_NAME}}"
        run: |
          echo "MODIFIED_BRANCH_NAME=${name/\//-}" >> $GITHUB_ENV

Tested here https://github.com/mbiagetti/github-action-poc/pull/1

You could test shell expansion with the code:

>name=feature/add-foo
>MODIFIED_BRANCH_NAME=${name/\//-}
>echo $MODIFIED_BRANCH_NAME
feature-add-foo

UPDATE

For multiple jobs requirement, check this PR https://github.com/mbiagetti/github-action-poc/pull/2

like image 122
Matteo Avatar answered Feb 24 '26 13:02

Matteo