skip to Main Content

I’ve created a simple pipeline which is attempting to run a script and then I’ll do something else with the output, however the script (CheckTagsDates.sh) never finishes according to Jenkins. If I SSH into the Jenkins slave node, su as the jenkins user, navigate to the correct workspace folder, I can execute the command successfully.

pipeline {
    agent {label 'agent'}
    stages {
        stage('Check for releases in past 24hr') {
            steps{
                sh 'chmod +x CheckTagsDates.sh'
                script {
                    def CheckTagsDates = sh(script: './CheckTagsDates.sh', returnStdout: true)
                    echo "${CheckTagsDates}"
                }
            }
        }
    }
}

Here is the contents of the CheckTagsDates.sh file

#!/bin/bash
while read line
do
        array[ $i ]="$line"
        (( i++ ))
done < <( curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/centos/tags'|jq -r '."results"[] | "(.name)&(.last_updated)"')

for i in "${array[@]}"
do
        echo $i | cut -d '&' -f 1
        echo $i | cut -d '&' -f 2
done

Here is the output from the script in the console

latest
2020-01-18T00:42:35.531397Z
centos8.1.1911
2020-01-18T00:42:33.410905Z
centos8
2020-01-18T00:42:29.783497Z
8.1.1911
2020-01-18T00:42:19.111164Z
8
2020-01-18T00:42:16.802842Z
centos7.7.1908
2019-11-12T00:42:46.131268Z
centos7
2019-11-12T00:42:41.619579Z
7.7.1908
2019-11-12T00:42:34.744446Z
7
2019-11-12T00:42:24.00689Z
centos7.6.1810
2019-07-02T14:42:37.943412Z

2

Answers


  1. How I told you in a comment, I think that is a wrong use of the echo instruction for string interpolation.

    Jenkins Pipeline uses rules identical to Groovy for string interpolation. Groovy’s String interpolation support can be confusing to many newcomers to the language. While Groovy supports declaring a string with either single quotes, or double quotes, for example:

    def singlyQuoted = 'Hello'
    def doublyQuoted = "World"
    

    Only the latter string will support the dollar-sign ($) based string interpolation, for example:

    def username = 'Jenkins'
    echo 'Hello Mr. ${username}'
    echo "I said, Hello Mr. ${username}"
    

    Would result in:

    Hello Mr. ${username}
    I said, Hello Mr. Jenkins
    

    Understanding how to use string interpolation is vital for using some of Pipeline’s more advanced features.

    Source: https://jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation

    Login or Signup to reply.
  2. As a workaround for this case, I would suggest you to do the parsing of the json content in Groovy, instead of shell, and limit the script to only retrieving the json.

    pipeline {
        agent {label 'agent'}
        stages {
            stage('Check for releases in past 24hr') {
                steps{
                    script {
                        def TagsDates = sh(script: "curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/centos/tags'", returnStdout: true).trim()
                        TagsDates = readJSON(text: TagsDates)
                        TagsDates.result.each {
                            echo("${it.name}")
                            echo("${it.last_updated}")
                        }
                    }
                }
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search