我有这个.gitlab-ci.yml文件内容,比如:
.install-pre-reqs: &install-pre-reqs
image: ubuntu:20.04
before_script:
- apt-get update -y
- apt-get upgrade -y
- apt-get install -y zip unzip curl fastjar
- chmod +x deploy.sh
test:
<<: *install-pre-reqs
stage: test
script:
- echo 'test'
- ./deploy.sh
- echo "content"
- exit 0
以及包含curl命令的deploy.sh脚本:
#!/bin/sh
curl -u "admin:admin" -X POST "http://localhost:9998/rest/admin/system/restart"
我希望能够通过CI/CD运行curl命令。当我在本地使用curl直接运行该命令时,它可以正常工作。但是,配置的CI/CD管道会触发以下错误消息:
Executing "step_script" stage of the job script
00:32
Using docker image sha256:3bc6e9f30f51d2bbf9307fc9d0bdfc30caa38cf4e3b05a714230f9a9b3381d84 for ubuntu:20.04 with digest ubuntu@sha256:af5efa9c28de78b754777af9b4d850112cad01899a5d37d2617bb94dc63a49aa ...
$ apt-get update -y
Err:1 http://security.ubuntu.com/ubuntu focal-security InRelease
Could not connect to security.ubuntu.com:80 (185.125.190.39), connection timed out Could not connect to security.ubuntu.com:80 (91.189.91.38), connection timed out Could not connect to security.ubuntu.com:80 (185.125.190.36), connection timed out Could not connect to security.ubuntu.com:80 (91.189.91.39), connection timed out
Err:2 http://archive.ubuntu.com/ubuntu focal InRelease
Could not connect to archive.ubuntu.com:80 (91.189.91.39), connection timed out Could not connect to archive.ubuntu.com:80 (185.125.190.36), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.91.38), connection timed out Could not connect to archive.ubuntu.com:80 (185.125.190.39), connection timed out
Err:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Unable to connect to archive.ubuntu.com:http:
Err:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Unable to connect to archive.ubuntu.com:http:
Reading package lists...
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal/InRelease Could not connect to archive.ubuntu.com:80 (91.189.91.39), connection timed out Could not connect to archive.ubuntu.com:80 (185.125.190.36), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.91.38), connection timed out Could not connect to archive.ubuntu.com:80 (185.125.190.39), connection timed out
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease Unable to connect to archive.ubuntu.com:http:
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease Unable to connect to archive.ubuntu.com:http:
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/focal-security/InRelease Could not connect to security.ubuntu.com:80 (185.125.190.39), connection timed out Could not connect to security.ubuntu.com:80 (91.189.91.38), connection timed out Could not connect to security.ubuntu.com:80 (185.125.190.36), connection timed out Could not connect to security.ubuntu.com:80 (91.189.91.39), connection timed out
W: Some index files failed to download. They have been ignored, or old ones used instead.
$ apt-get upgrade -y
Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
$ apt-get install -y zip unzip curl fastjar
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package zip
E: Unable to locate package unzip
E: Unable to locate package curl
E: Unable to locate package fastjar
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1
如何解决这些问题并使curl命令在gitlab环境中运行而不会出现问题?
您使用的是gitlab.com还是自托管?管道运行程序似乎在代理后面,无法安装您的软件包:
http://security.ubuntu.com/ubuntu无法连接到安全。ubuntu.com:80
-
如果是自托管的,您可能需要请求托管跑步者的机器的许可才能下载您的软件包。
-
如果您使用的是gitlab.com运行程序,可能是
archive.ubuntu.com
服务器出现了一些停机时间?我创建了你的gitlab-ci.yml的副本,效果很好。
与您的问题无关,但请查看有关如何改进管道设置的答案:https://stackoverflow.com/a/66012365/9697259
TL;DR:最好创建一个已经安装了依赖项的自定义docker映像,以节省管道执行期间的时间。
如果您使用的是限制访问互联网的自托管GitLab服务器,则另一个解决方案是:使用现有/官方/维护的Docker镜像和所需的工具(前提是您的运行者允许使用Docker Hub中的Docker映像(。
这样一来,您就不会麻烦构建/维护自制的Docker映像了。另一个好处是,您不会一次又一次地apt-get update
、apt-get upgrade
、apt-get install
(这将节省时间(。最后但同样重要的是:经过大小优化的映像(如Alpine(将比Ubuntu20等基本Linux映像小得多,这也缩短了管道执行时间。
就我个人而言,对于curl + jq
的东西,我喜欢使用dwdraju/高山卷曲jq(<5MB(
您的.gitlab-ci.yml
将变成:
test:
image:
name: "dwdraju/alpine-curl-jq"
entrypoint: [""]
stage: test
script:
- echo 'test'
- ./deploy.sh
- echo "content"
- exit 0