31

I've been trying to make a GitLab CI/CD pipeline for deploying my MEAN application. I have three stages: 1. test 2. build 3. deploy

The build stage has a build_angular job which generates an artifact. Now I want to use this artifacts in the next stage i.e deploy. The deploy job tells me that it has downloaded the artifact(image has been attached), but now I want to extract this artifact, but I don't know where the artifact is being downloaded.

The path where the artifact is being downloaded is not mentioned anywhere in the docs. if

Goutam B Seervi
  • 1,096
  • 1
  • 18
  • 31

4 Answers4

41

GitLab is cleaning the working directory between two subsequent jobs. That's why you have to use artifacts and dependencies to pass files between jobs.

When the "deploy" job says that the build artifact have been downloaded, it simply means that they have been recreated as they were before. The location of the downloaded artifacts matches the location of the artifact paths (as declared in the .yml file).

Example

build_job:
  stage: build
  script:
  - echo "create bin/ directory"
  - make
  artifacts:
    paths:
      - bin/

deploy_job:
  stage: deploy
  script:
  - ls bin/
  dependencies:
  - build_job

Directory bin/ is passed to deploy_job from build_job.

piarston
  • 1,685
  • 1
  • 13
  • 25
  • It seems to be also important that the jobs which build the artifacts are on prior stages (which is already the case here). – ph_0 Sep 22 '20 at 13:14
  • What if you had steps: build, test, and deploy? For deploy I want to get the artifacts from the build step, not the test step. Otherwise I'd be deploying stuff like test-coverage.xml. – Exegesis Apr 27 '21 at 18:04
2

If the artifact is downloaded, it will be situated at the very same path it was in the task it was registered. Thus, if you cannot find an artifact then it is likely not being downloaded.

To make sure you get an artifact from a specific task, you have two options:

  1. Using needs (as of GitLab 12.2)
  2. Using dependencies

Using dependencies is well explained by @piarston's answer, so I won't repeat this here.

With the newer needs keyword you can even explicitly specify if you want the artifacts or not. This is done by means of the needs:artifacts keyword:

build_job:
  stage: build
  script:
  - echo "create bin/ directory"
  - make
  artifacts:
    paths:
      - bin/

deploy_job:
  stage: deploy
  script:
  - ls bin/
  needs:
  - job: build_job
    artifacts: true

other_job:
  stage: deploy
  needs:
  - job: build_job
    artifacts: false

In this example, the artifacts are downloaded for the deploy_job but not for the other_job.

With needs you can write explicitly and in a clear manner where you need the artifacts, and where you just want to wait for the previous job to finish.

j-i-l
  • 10,281
  • 3
  • 53
  • 70
0

Note that gitlab-org/gitlab-runner issue 2656 mentions:

But the documentation talks about this limitation in fact : "in the latest pipeline that succeeded" : no way now to get artifacts from the currently running pipeline.

Thank you !
I think the documentation should really make it more obvious that you need the whole pipeline to complete before the artifact is accessible and that you can't use this within the pipeline.

It seemed to me that the obvious usecase of this feature would be deploying on the server, and that you'd want server deployment to be part of the pipeline.

That comes from Pipelines / Jobs Artifacts / Downloading the latest artifacts

To download a single file from the artifacts use the following URL:

https://example.com/<namespace>/<project>/-/jobs/artifacts/<ref>/raw/<path_to_file>?job=<job_name>

See allpix-squared/allpix-squared as an example.

Its .gitlab-ci.yml deploy stage calls a script with the right path:

sudo -u cvclicdp -i $RUNNER_LOCATION/.gitlab-ci.d/gitlab_deploy.sh $RUNNER_LOCATION $BUILD_PATH
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-4

Each job runs on the separate runner.

Upload artifact action implementation

Github Action "actions/upload-artifact@v3" uploads the files from provided path to storage container location.

In next job when you run action "actions/download-artifact@v3" , it downloads the artifact from 'storage container location' where previous job uploaded the artifacts to provided path.

Implementation for download artifact and displaying download path

For more information refer below links,

Github action for upload artifacts

Github action for download artifacts

chetan kadam
  • 15
  • 1
  • 6