當我嘗試在 Jenkinsfile 的其中一個步驟中執行 CURL 命令時,它在代理后面作業時遇到問題。
我正在使用 Ubuntu 18 并像這樣運行 Jenkins 容器:
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
然后我有一個簡單Jenkinsfile的方法,它從 git 存盤庫中克隆代碼,制作影像,將其推送到注冊表,最后使用 curl 發送 Telegram 訊息。
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"
'''
}
}
}
}
}
}
執行 curl 命令時失敗(我得到一個ERROR: script returned exit code 7)。但我認為它應該與 Linux 或公司代理有關,因為我在沒有代理的 Windows 機器上測驗了相同的,并且它有效。
如果我需要添加更多資訊,請告訴我,提前謝謝。
uj5u.com熱心網友回復:
由于 Jenkins 位于企業代理背后,因此您必須將代理資訊傳遞給 curl 才能連接到目標服務。
curl手冊頁說,您可以使用--proxyor -x(shortcut) 引數傳遞代理資訊。
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"
'''
這也可以通過 env vars http_proxy/設定https_proxy。
如果代理需要基本身份驗證,則可以像這樣傳遞<protocol>://<proxy-username>:<proxy-password@><proxy-host>:<proxy-port>
最后,在除錯curl時,洗掉-s引數很重要,因為它會默默地使輸出靜音。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/457896.html
