Timothy
Timothy

Reputation: 3593

How to use bash variables in Jenkins multi-line shell script

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?

Upvotes: 0

Views: 465

Answers (1)

Shane Bishop
Shane Bishop

Reputation: 4750

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

Upvotes: 1

Related Questions