Issue
I'm having an issue when I try to execute a CURL command in one of the steps of a Jenkinsfile when it's working behind a proxy.
I'm working on Ubuntu 18 and I run the Jenkins container in like this:
docker run -d
-u root --privileged
-v jenkins_home:/var/jenkins_home
-v /var/run/docker.sock:/var/run/docker.sock
-v "$HOME":/home
-e JENKINS_OPTS="--prefix=/jenkins"
--group-add 997
-p 8080:8080
-p 50000:50000
--name jenkins
jenkinsci/blueocean
And then I have a simple Jenkinsfile
that clones the code from a git repository, makes an image, pushes it to a registry and finally sends a Telegram message using curl.
pipeline {
agent any
environment {
dockerImage = ''
}
stages {
stage('Testing') {
steps {
echo 'testing'
}
}
stage('Build image') {
steps {
script{
dockerImage = docker.build("registry.***.com.ar/hellonode")
}
}
}
stage('Push image') {
steps{
script {
docker.withRegistry('https://registry.***.com.ar', 'registryCredentials') {
dockerImage.push("${env.BUILD_NUMBER}")
dockerImage.push("latest")
}
}
}
}
stage('Push Notification') {
steps {
script{
withCredentials([string(credentialsId: 'telegramToken', variable: 'TOKEN'),
string(credentialsId: 'telegramChatId', variable: 'CHAT_ID')]) {
sh '''
curl -s -X \
POST https://api.telegram.org/bot${TOKEN}/sendMessage \
-d chat_id=${CHAT_ID} \
-d parse_mode="HTML" \
-d text="🚀 <b>Jenkins CI:</b> <b>Iniciando build $BUILD_DISPLAY_NAME</b> $JOB_NAME"
'''
}
}
}
}
}
}
And it fails when executing the curl command (I get an ERROR: script returned exit code 7
).
But I think that it should be related to Linux or corporative Proxy, because I tested the same in my Windows machine without proxy and it worked.
Please let me know if I need to add further information, thanks in advance.
Solution
Since Jenkins is behind the corporate proxy, you have to pass proxy information to curl to connect to target services.
curl man page says, you can pass proxy information with either --proxy
or -x
(shortcut) argument.
sh '''
curl -s --proxy <protocol>://<proxy-host>:<proxy-port> -X \
POST https://api.telegram.org/bot${TOKEN}/sendMessage \
-d chat_id=${CHAT_ID} \
-d parse_mode="HTML" \
-d text="🚀 <b>Jenkins CI:</b> <b>Iniciando build $BUILD_DISPLAY_NAME</b> $JOB_NAME"
'''
This can also set via env vars http_proxy
/https_proxy
.
In case if proxy expects basic auth, it can be passed like <protocol>://<proxy-username>:<proxy-password@><proxy-host>:<proxy-port>
Finally, while debugging curl
, it's important to remove -s
argument as it silently mutes the output.
Answered By - harshavmb
Answer Checked By - David Goodson (JavaFixing Volunteer)