无法安装和更新 gitlab-ci-yml 中的包

问题描述 投票:0回答:1

我有一个 gitlab-ci-yml 脚本要部署在我的远程服务器中,但我在安装 ssh 时遇到问题,我已经强制分配了 DNS,但仍然是同样的问题,并在 docker 中尝试了不同的图像,但没有任何效果。

gitlab 跑步者:

  [[runners]]
  name = "deployment_runner"
  url = "http://mygitlab-url/"
  id = 9
  token = "aFcyz1PhKjE1CcPvg8E3"
  token_obtained_at = 2024-12-09T02:53:50Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    MaxUploadedArchiveSize = 0
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "ubuntu:20.04"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
    network_mtu = 0
    extra_hosts = ["host"]
    dns = ["8.8.8.8", "8.8.4.4"]

gitlab-ci-yml:

stages:
  - deploy

deploy:
  image: ubuntu:20.04
  stage: deploy
  tags:
    - deployment_runner
    
  only: 
    - development
  before_script:
    - echo "nameserver 8.8.8.8" > /etc/resolv.conf
    - echo "nameserver 8.8.4.4" >> /etc/resolv.conf
    - cat /etc/resolv.conf
    - apt-get clean
    - apt-get update
    - apt-get install -y dnsutils
    - nslookup archive.ubuntu.com
    - apt-get install -y openssh-client
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_PRIVATE_KEY" | base64 -d > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - ssh-keyscan -H $SSH_HOST > ~/.ssh/known_hosts

  script:
    - ssh $SSH_USER@$SSH_HOST "cd $WORK_DIR && git pull origin development && sudo docker compose restart &&  exit"
  after_script:
    - rm -rf ~/.ssh

错误:

20 $ echo "nameserver 8.8.8.8" > /etc/resolv.conf
21 $ echo "nameserver 8.8.4.4" >> /etc/resolv.conf
22 $ cat /etc/resolv.conf
23 nameserver 8.8.8.8
24 nameserver 8.8.4.4
25 $ apt-get clean
26 $ apt-get update
27 Err:1 http://archive.ubuntu.com/ubuntu focal InRelease
28 Temporary failure resolving 'archive.ubuntu.com'
29 Err:2 http://security.ubuntu.com/ubunty focal-security InRelease
30 Temporary failure resolving 'security.ubuntu.com'
31 Err: 3 http://archive.ubuntu.com/ubuntu focal-updates InRelease
32 Temporary failure resolving 'archive.ubuntu.com'
53 Err:4 http:archive.ubuntu.com/ubuntu focal-backports InRelease
34 Temporary failure resolving 'archive.ubuntu.com'
55 Reading package lists...
36 W: Failed to fetch http:/archive.ubuntu.com/ubuntu/dists/focal/InRelease Temporary failure resolving 'archive.ubuntu.com'
37 W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease Temporary failure resolving 'archive.ubuntu.com'
38 W: Failed to fetch http:/archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease Temporary failure resolving 'archive.ubuntu.com'
39 W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/focal-security/InRelease Temporary failure resolving 'security.ubuntu.com'
40 W: Some index files failed to download. They have been ignored, or old ones used instead.
41 $ apt-get install -y dnsutils
42 Reading package lists...
43 Building dependency tree...
44 Reading state information...
45 E: Unable to locate package dnsutils
47 Running after_script
48 Running after_script...
49 $ rm -rf ~/.ssh
51 ERROR: Job failed: exit code 1
python docker gitlab dockerfile gitlab-ci-runner
1个回答
0
投票

您的 Docker 运行程序似乎没有网络连接。我无法查明确切的问题,但我注意到您的

GitLab Runner
设置中有一些有问题的配置:

extra_hosts = ["host"]

此格式不正确。根据文档,它应该遵循格式:

hostname:<ip address>
。例如:

extra_hosts = ["other-host:127.0.0.1"]
network_mtu = 0

这个值看起来很可疑。我只能在 documentation 中找到此参数的提及,我建议删除它或设置一个更典型的值,例如 1492 — 除非您有特定原因以这种方式配置它。

出于测试目的,我建议在安装了 GitLab Runner 的主机上运行任何 Docker 容器来验证网络连接。这将帮助您确定问题是否出在主机本身或运行程序配置上。

最后,一个一般性建议:通常最好构建一个自定义 Docker 镜像,并预安装所有必需的软件包(如

dnsutils
)和更新。这样可以避免每次触发作业时都安装它们,从而可以节省时间并减少管道执行期间的潜在问题。

© www.soinside.com 2019 - 2024. All rights reserved.