如何从gitlab/github上的gitlab-ci CI/CD运行curl命令?

2ic8powd  于 2022-11-13  发布在  Git
关注(0)|答案(2)|浏览(260)

我有这个.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

以及deploy.sh包含curl命令的www.example.com脚本:

#!/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环境中运行而不出现问题呢?

ua4mk5z4

ua4mk5z41#

您使用的是gitlab.com还是自托管?管道运行程序似乎位于代理服务器后面,无法安装您的软件包:
http://security.ubuntu.com/ubuntu无法连接到security.ubuntu.com:80
1.如果是自托管的,您可能需要请求托管runner的计算机的权限,以便能够下载您的包。
1.如果您使用的是gitlab.com runners,可能是因为archive.ubuntu.com服务器出现了一些故障?我创建了一个gitlab-ci.yml的副本,它运行正常。
与您的问题无关,但请检查此答案,了解如何改进您的管道设置:https://stackoverflow.com/a/66012365/9697259
TL;DR:最好创建一个自定义的Docker映像,并在其中安装依赖项,以保存管道执行期间的时间。

h7wcgrx3

h7wcgrx32#

另一个解决方案,如果你使用的是一个自托管的GitLab服务器,限制访问互联网:使用现有/官方/维护的Docker镜像和所需的工具(如果你的跑步者允许使用Docker Hub中的Docker镜像)。
这样你就不用费心构建/维护一个自制的Docker映像了。另一个好处是你不用一遍又一遍地apt-get updateapt-get upgradeapt-get install(这保存时间)。最后但同样重要的是:经过大小优化的映像(如Alpine)将比基本Linux映像(如Ubuntu 20)小很多,这也缩短了您的流水线执行。
就我个人而言,我喜欢使用curl + jq(〈5 MB)
您的.gitlab-ci.yml将变成:

test:
  image:
    name: "dwdraju/alpine-curl-jq"
    entrypoint: [""]
    stage: test
    script:
        - echo 'test'
        - ./deploy.sh
        - echo "content"
        - exit 0

相关问题