【问题标题】:How to run curl commands from gitlab-ci CI/CD on gitlab/github?如何从 gitlab/github 上的 gitlab-ci CI/CD 运行 curl 命令?
【发布时间】:2022-08-14 22:06:15
【问题描述】:

我有这个 .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 环境中运行而不会出现问题?

    标签: bash ubuntu curl gitlab cicd


    【解决方案1】:

    您使用的是 gitlab.com 还是自托管?似乎管道运行器位于代理后面,并且无权安装您的软件包:

    http://security.ubuntu.com/ubuntu 无法连接到 security.ubuntu.com:80

    1. 如果是自托管的,您可能必须请求托管运行器的机器的许可才能下载您的包。

    2. 如果您使用的是 gitlab.com 跑步者,那可能是 archive.ubuntu.com 服务器出现了一些停机时间?我创建了您的 gitlab-ci.yml 的副本,它运行良好。

      与您的问题无关,但请查看有关如何改进管道设置的答案:https://stackoverflow.com/a/66012365/9697259

      TL;DR:最好创建一个已安装依赖项的自定义 docker 映像,以节省管道执行期间的时间。

    【讨论】:

      【解决方案2】:

      如果您使用的是限制访问 Internet 的自托管 GitLab 服务器,则另一种解决方案是:使用现有/官方/维护的 Docker 映像和所需工具(前提是您的运行者允许使用来自 Docker Hub 的 Docker 映像)。

      这样您就不必费心构建/维护自制的 Docker 映像了。 另一个好处是你不会一遍又一遍地apt-get updateapt-get upgradeapt-get install(它将要省时间)。最后但同样重要的是:尺寸优化的映像(例如 Alpine)将比基础 Linux 映像(例如 Ubuntu 20)小得多,这也会缩短您的管道执行时间。

      我个人喜欢使用dwdraju/alpine-curl-jq(<5MB)的curl + jq 的东西

      您的 .gitlab-ci.yml 将变为:

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

      【讨论】:

        猜你喜欢
        • 2020-06-28
        • 2022-08-06
        • 2019-03-03
        • 2019-05-13
        • 2021-11-28
        • 2020-10-27
        • 2020-09-09
        • 1970-01-01
        • 2021-03-21
        相关资源
        最近更新 更多