繁体   English   中英

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

[英]How to run curl commands from gitlab-ci CI/CD on gitlab/github?

我有 this.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无法连接到security.ubuntu.Z4D23AD18A2D10BEC5FEZDA6:

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

  2. 如果您使用的是 gitlab.com 跑步者,也许是存档中的一些停机时间archive.ubuntu.com服务器我创建了您的 gitlab-ci.yml 的副本,它运行良好。

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

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

Another solution if you're using a self hosted GitLab server with restricted access to the internet: use an existing/official/maintained Docker image with required tools (provided your runners allow using Docker images from Docker Hub).

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM