Gitlab CI作业缺少先前作业的工件

时间:2020-07-26 11:20:16

标签: gitlab-ci

我有一个简单的.gitlab-ci.yaml,如下所示:

.
.
.
stages:
  - create-ssh-key-pair
  - prerequisites
  - install-terraform
  - deploy
  - restart-sshd
  - test-ansible
  - ansible-playbooks
  - destroy


deploy:
  stage: deploy
  script:
    - |
      # !/bin/bash
      KNOWNHOSTS=~/.ssh/known_hosts
      if [ -f "$KNOWNHOSTS" ]; then
        echo "$KNOWNHOSTS exists."  
        echo "" > ~/.ssh/known_hosts      
      else 
        mkdir -p ~/.ssh && touch ~/.ssh/known_hosts          
      fi
    - |
      #!/bin/bash
      ANSIBLEHOSTS=/etc/ansible/hosts
      if [ -f "$ANSIBLEHOSTS" ]; then
          echo "$ANSIBLEHOSTS exists."
      else 
          mkdir -p /etc/ansible/ && touch /etc/ansible/hosts
      fi
    - cat /etc/ansible/hosts
    - cat /etc/ansible/hosts
    - cp /etc/ansible/hosts ${CI_PROJECT_DIR}
    - terraform init
    - terraform plan
    - terraform apply --auto-approve
    - pwd
    - ls
  artifacts:
    paths:
      - ./hosts
  tags:
    - gitlab-org
.
.
.
ansible-playbooks:
  stage: ansible-playbooks
  retry:
    max: 2
    when:
      - always
  script:
    - echo "ansible"
    - ls
    - pwd
    - cat hosts
    - ansible-playbook -i hosts ./ansible-playbooks/master_and_minions.yaml
    - ansible-playbook -i hosts ./ansible-playbooks/master.yaml
    - ansible-playbook -i hosts ./ansible-playbooks/join_master_and_hosts.yaml
  when: delayed
  start_in: 10 seconds
  needs:
    - job: test-ansible
  tags:
    - gitlab-org

我在这里删除了其他作业以便于阅读。因此,在deploy阶段,gitlab-ci说,工件被上传如下:

Uploading artifacts...
./hosts: found 1 matching files and directories    
Uploading artifacts as "archive" to coordinator... ok  id=656453560 responseStatus=201 Created token=mJXyVJbo

ansible-playbooks作业中回答“ cat hosts”时,出现此错误:

cat: hosts: No such file or directory

此主机文件位于构建路径/的{​​{1}}

因为在/builds/iamdempa/k8s-gcp-cicd的{​​{1}}命令中暂存了ls文件...

有人可以帮我吗?

问题:是否也可以将工件发送到其他作业?我们可以仅将工件发送给连续的作业吗?

1 个答案:

答案 0 :(得分:0)

needs:用于运行乱序的作业,仅要求列出的作业已完成。这是GitLab的片段:

needs:关键字允许无序执行作业,使您可以在.gitlab-ci.yml中实现有向无环图。
这样一来,您无需等待阶段即可运行某些作业,而无需等待其他任务,因此您可以让多个阶段同时运行。

dependencies:默认情况下会传递先前作业中的所有工件。

但是,对于您的ansible-playbooks工作,只有test-ansible被保证是以前的工作。您应指定您的工作首先需要在needs下运行的所有工作,即

ansible-playbooks:
  # ...
  needs:
    - job: test-ansible
    - job: deploy