GitLab CI 启用 SCP

GitLab CI enable SCP

我目前正在 GitLab.com 上使用其中一个共享跑步者。是否可以设置 .gitlab-ci.yaml 文件,以便构建可以将 SCP 文件从远程服务器发送到运行器上?我的目标是 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

根据你的问题 ,我认为通过 http 获取你的依赖项是不可能的,所以这里是你需要做的才能使用 scp:

  • 生成密钥对
  • private 密钥复制到 gitlab CI 变量(我们称之为 SSH_PRIVATE_KEY
  • public 密钥复制到 gitlab 将连接的服务器并将其添加到您的 ~/.ssh/authorized_keys 文件
  • 告诉您的 CI 管道使用存储在 Gitlab CI 变量中的私钥

为了执行最后一步,只需将以下内容添加到脚本中的 .gitlab-ci.yml 或感兴趣的工作的 before_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'

您可能还想指定 CodeA 依赖于 B 和 C。为了使其正常工作,build_CodeB 和 build_CodeC 需要处于与 [=50= 不同的阶段].

除此之外,您需要一种方法将构建的文件从 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

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