GitLab CI启用SCP

11
我目前正在使用GitLab.com上的共享Runner。是否可以设置.gitlab-ci.yaml文件,以使构建可以从远程服务器将文件SCP到Runner?我的目标是SCP对我构建的必要依赖项,但它们未在任何Git存储库中跟踪。
我已经标记了我想要执行传输的行,但我不知道如何正确表达它。
注意:CodeA具有CodeB和CodeC的依赖关系,必须先构建它们才能编译CodeA,因此我需要访问CodeB和CodeC,并在ubuntu镜像上首先构建它们。
image: ubuntu:12.04

before_script:

build_CodeC:
  stage: build
  allow_failure: true
  script:
-->- scp user@remoteServer:/home/user/file.tar . <---
   - sh ./continuous_integration/build_CodeC_dependency.sh

build_CodeB:
  stage: build
  script:
    - sh ./continuous_integration/build_CodeB_dependency.sh

build_CodeA:
  stage: build
  script:
    - sh ./continuous_integration/build_CodeA.sh
1个回答

18

根据您在这里提出的问题,我认为无法通过http获取依赖项,因此以下是使用scp所需做的:

  • 生成密钥对
  • 私钥复制到Gitlab CI变量中(我们称之为SSH_PRIVATE_KEY
  • 公钥复制到Gitlab将连接到的服务器上,并将其添加到~/.ssh/authorized_keys文件中
  • 告诉CI流水线使用存储在Gitlab CI变量中的私钥

为了执行最后一步,只需在感兴趣的作业的script或before_script部分中将以下内容添加到.gitlab-ci.yml中:

- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
# Run ssh-agent (inside the build environment)
- eval $(ssh-agent -s)
# Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

您可能还希望指定CodeA依赖于B和C。为了使其正常工作,build_CodeB和build_CodeC需要处于不同的阶段而不是build_CodeA。

除此之外,您需要一种方法将build_CodeB和build_CodeC生成的文件传递到build_CodeA作业中。一种方法是使用artifacts

最终,您的.gitlab-ci.yml文件应该类似于这样:

image: ubuntu:12.04

stages:
  - deps
  - build

build_CodeC:
  stage: deps
  allow_failure: true
  script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    # Run ssh-agent (inside the build environment)
    - eval $(ssh-agent -s)
    # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - scp user@remoteServer:/home/user/file.tar .
    - sh ./continuous_integration/build_CodeC_dependency.sh
  artifacts:
    paths:
      - path_to_built_codeC

build_CodeB:
  stage: deps
  script:
    - sh ./continuous_integration/build_CodeB_dependency.sh
  artifacts:
    paths:
      - path_to_built_codeB

build_CodeA:
  stage: build
  dependencies:
    - build_CodeB
    - build_CodeC
  script:
    - sh ./continuous_integration/build_CodeA.sh

我只在build_CodeC中放置了SSH密钥设置部分,因为那是您使用scp的地方。您需要将此复制到任何需要使用scp的作业中。我认为您可能需要在build_codeB中执行此操作,因为您的tar文件不会传递到build_CodeB作业。


再次感谢您,Jawad。您是正确的,对于这个特定的构建,需要使用scp。必要的构建顺序是:CodeC、CodeB、CodeA。那么我应该为每个步骤设置一个阶段吗?阶段:deps1、deps2、build? - wandadars
1
如果CodeB依赖于CodeC,那么是的。 - Jawad
1
我正在阅读 GitLab 关于 SSH 密钥 的笔记,发现他们提到了你在使用 Docker 执行器时建议的内容。我如何知道我是在本地 GitLab runner 上运行 Docker 执行器还是 shell 执行器?这是否在 gitlab-ci.yaml 文件中有指定? - wandadars
1
好的,既然你在yaml文件中以image: ubuntu:12.04开头,我就假设你是在Docker Runner上运行。当你通过gitlab-ci-multi-runner注册Runner时,你可以自己决定。 - Jawad
好的,那很有道理。阶段名称很重要吗?当我提交了新的yaml控制文件后,在GitLab中管道状态显示由于某些原因跳过了deps阶段。 - wandadars
1
名称不应该有任何区别。 - Jawad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接