簡體   English   中英

在 Jenkins 后面運行 Curl 命令

[英]Running Curl Command in Jenkins Behind Corporative Proxy

當我嘗試在 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 發送電報消息。

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 機器上測試了它並且它有效。

如果我需要添加更多信息,請告訴我,在此先感謝。

由於 Jenkins 在企業代理后面,您必須將代理信息傳遞給 curl 才能連接到目標服務。

curl手冊頁說,您可以使用--proxy-x (快捷方式)參數傳遞代理信息。

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

這也可以通過環境變量http_proxy / https_proxy

如果代理需要基本身份驗證,則可以像<protocol>://<proxy-username>:<proxy-password@><proxy-host>:<proxy-port>這樣傳遞

最后,在調試curl時,刪除-s參數很重要,因為它會靜默靜音 output。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM