工作中要求使用pipeline去docker build多个Dockerfile,其中Dockerfile中涉及到apt-get,git clone,wget等下载操作,因为公司网络的原因,需要设置代理,在本地的机器(jenkins中的一台node)上手动操作上述步奏没有问题,查看环境变量:
[root@xxxx]# export
.
.
.
declare -x ftp_proxy="xxx.xxx.xxx:xxx/"
declare -x http_proxy="xxx.xxx.xxx:xxx/"
declare -x https_proxy="xxx.xxx.xxx:xxx3/"
declare -x no_proxy="xxx.com,.xxx.com,localhost,127.0.0.1"
但是一旦在jenkins中执行上诉操作,总是报错(执行机为上诉手动执行的机器),报错信息有:
Err:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::21). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::17). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1560:8001::11). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1560:8001::14). - connect (101: Network is unreachable) Could not connect to archive.ubuntu.com:80 (91.189.88.149), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.88.161), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.88.152), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.88.162), connection timed out
Err:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::21). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::17). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1560:8001::11). - connect (101: Network is unreachable) Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1560:8001::14). - connect (101: Network is unreachable)
.
.
.
E: Unable to locate package python3-tornado
E: Unable to locate package python3-kafka
E: Unable to locate package python3-kazoo
E: Unable to locate package python3-requests
我很奇怪,在同一台机器上执行,为啥手动执行ok,通过jenkins pipeline就不行呢?
从报错信息上看是执行Dockerfile文件中的apt-get时,无法连接到“http://archive.ubuntu.com/ubuntu”,导致包下载失败。
执行机上的代理环境变量设置肯定是ok的,我想jenkins在编译过程中可能有自己的一套环境变量,并不会取node上的环境变量,于是我在pipeline中加上了一句sh 'export' 来查看执行中的环境变量,得到的结果中果然没有代理相关的环境变量,确信是jenkins自己的一套环境变量。
+ export
export BUILD_DISPLAY_NAME="#30"
export BUILD_ID="30"
export BUILD_NUMBER="30"
.
.
.
export _="/bin/sh"
export enable_mem_share="false"
export release_version="0.1
于是我在jenkins pipeline中进行了这样的修改,结果还是一样:
sh "export http_proxy='http://xxx.xxx.xxx:xxx/' && export https_proxy='http://xxx.xxx.xxx:xxx/' && export ftp_proxy='http://xxx.xxx.xxx:xxx/'"
后来才反应过来,这样不还是设置的是node的环境变量,jenkins不会取得,后来采用这种方法问题解决,使用environment :
pipeline{
agent {label 'xxx01'}
environment {
def http_proxy="http://xxx.xxx.xxx:xxx/"
def https_proxy="http://xxx.xxx.xxx:xxx/"
def ftp_proxy="http://xxx.xxx.xxx:xxx/"
def no_proxy="xxx.com,.xxx.com,localhost,127.0.0.1"
}
stages{
stage('step 1'){
steps {
script{
xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxx
}
}
}
}
}