Search code examples
stringpowershellreplaceenvironment-variablesgithub-actions

Replace string in Windows Git workflow actions


I wish to replace both characters \\ with / and : with <blank> from variable deploy_path

  - name: Echo server_list
    shell: cmd
    run: echo ${{ steps.dotenv.outputs.deploy_path }}

Output:

C:\\inetpub\\wwwroot\\soc.qa

I'm using Windows 2019 cmd shell. However, I'm fine with a solution using powershell.

I tried the below:

  - name: character-replacement-test
    run: newstr=$(echo ${{ steps.dotenv.outputs.deploy_path//[\:]// }})      

Unfortunately the above does not give me the desired output which is C/inetpub/wwwroot/soc.qa

I would eventually assign the desired output to environment variable newstr

I was able to get the powershell to get me the desired output:

C:\Users\meuser>powershell (echo 'C:\\inetpub\\wwwroot\\soc.qa') -replace ':','' -replace '\\\\','/'

C/inetpub/wwwroot/soc.qa

However, I m not able to incorporate this in Gtihug action run step

  - name: character-replacement-test

    run: powershell (echo '${ steps.dotenv.outputs.deploy_path }') -replace ':','' -replace '\\\\','/'

Output: shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"

Can you please suggest?


Solution

  • This

    ${ steps.dotenv.outputs.deploy_path }
    

    should be

    ${{ steps.dotenv.outputs.deploy_path }}
    

    In a step, it may simple be like this:

    name: pwsh_transform_path
    
    on: workflow_dispatch
    
    jobs:
      job:
        runs-on: windows-latest
    
        steps:
          - name: Transform path
            env:
              deploy_path: '${{ steps.dotenv.outputs.deploy_path }}'
            run: |
              $new_path=$ENV:deploy_path.replace('\\','/').replace(':','')
              echo $new_path
    

    Here's an example with hardcoded deploy_path as an env var:

    output