Jananath Banuka
Jananath Banuka

Reputation: 3963

Gitlab CI job missing artifacts from previous job

I have a simple .gitlab-ci.yaml as below:

.
.
.
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

I have here removed other jobs for easy-reading. So in deploy stage, the gitlab-ci says, the artifact is being uploaded as below:

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

Ans in the ansible-playbooks job, where it says cat hosts, it gives this error:

cat: hosts: No such file or directory

This host file is in / of the build path, which is /builds/iamdempa/k8s-gcp-cicd

because in the ls command of deploy stage it the hosts file is there...

Can someone please help me?

Problem: Is it not possible to send the artifact to other jobs as well? Can we only send the artifact to the consecutive jobs only?

Upvotes: 2

Views: 4143

Answers (1)

jeremysprofile
jeremysprofile

Reputation: 11484

needs: is used to run jobs out of order, only requiring the listed jobs to have completed. Here's a snippet from GitLab:

The needs: keyword enables executing jobs out-of-order, allowing you to implement a directed acyclic graph in your .gitlab-ci.yml.
This lets you run some jobs without waiting for other ones, disregarding stage ordering so you can have multiple stages running concurrently.

dependencies: by default will pass on all artifacts from previous jobs.

However, for your ansible-playbooks job, only test-ansible is guaranteed to be a previous job. You should specify all jobs that your job requires to have run first under your needs, i.e.,

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

Upvotes: 2

Related Questions