0

I have unsuccessfully tried to use bash variables in Jenkins pipeline.

My first attempt

sh """#!/bin/bash
    for file in *.map; do
        filename=`basename $file .map`
        echo "##### uploading ${$filename}"

        curl  -X POST ${SERVER_URL}/assets/v1/sourcemaps \
              -F service_name="${SERVICE_NAME}" \
              -F service_version="${revision}" \
              -F bundle_filepath="${main_url}${filename}" \
              -F sourcemap="@${filename}.map" &
    done
    wait
"""

Resulted in exception: MissingPropertyException: No such property: file

The second attempt, after seeing this answer https://stackoverflow.com/a/35047530/9590251

sh """#!/bin/bash
    for file in *.map; do
        filename=`basename \$file .map`
        echo "##### uploading \$filename"

        curl  -X POST ${SERVER_URL}/assets/v1/sourcemaps \
              -F service_name="${SERVICE_NAME}" \
              -F service_version="${revision}" \
              -F bundle_filepath="${main_url}\$filename" \
              -F sourcemap="@\$filename.map" &
    done
    wait
"""

Simply omitted bash variables. So $filename was empty.

How do I need to property encode bash variables in this scenario?

Timothy
  • 3,213
  • 2
  • 21
  • 34
  • Would it be possible to copy your bash script onto the Jenkins server, and then run the bash script like that? Then you wouldn't need to try to use escapes, and all of your Groovy variables should be accessible as environment variables in your bash script. – Shane Bishop Mar 05 '21 at 20:10
  • No, it's absolutely not possible. – Timothy Mar 05 '21 at 21:30

1 Answers1

1

Try this:

sh """#!/bin/bash
    set -x    

    for file in *.map; do
        filename="\$(basename "\$file" .map)"
        echo "Uploading \$filename"

        curl  -X POST "${SERVER_URL}/assets/v1/sourcemaps" \
              -F service_name="${SERVICE_NAME}" \
              -F service_version="${revision}" \
              -F bundle_filepath="${main_url}\$filename" \
              -F sourcemap="@\${filename}.map" &
    done
    wait
"""
Shane Bishop
  • 3,905
  • 4
  • 17
  • 47