gitlab-ci.yml, before_script and artifact

Henry Yang picture Henry Yang · Oct 10, 2018 · Viewed 8.4k times · Source

In gitlab-ci.yml documentation, it says that

before_script is used to define the command that should be run before all jobs, including deploy jobs, but after the restoration of artifacts.

This tells me that artifact is produced before a job start running

But the artifact documentation says that

Artifacts is a list of files and directories which are attached to a job after it completes successfully

This tells me that artifact is produced after a job finish running.

This is a contradiction. Can someone please explain how this is not a contradiction?

I imagine they are talking about the artifact in previous job? But I don't know how artifact and job work and can be wrong.

Answer

rnstlr picture rnstlr · Oct 25, 2018

Artifacts can be produced by build jobs from one stage and consumed by build jobs from the next stage. So before_script is run after the artifacts produced by the previous stage are restored for the current stage.

So the follwing .gitlab-ci.yml

stages:
  - build
  - test

before_script:
  - echo "before_script"
  - ls

build_artifacts:
  stage: build
  tags:
    - docker
  script:
    - echo "build_artifacts"
    - touch build_output
  artifacts:
    paths:
      - build_output

test_artifacts:
  stage: test
  tags:
    - docker
  script:
    - echo "test_artifacts"

Will give the following outputs:

# build_artifacts job
$ echo "before_script"
before_script
$ ls
README.md
$ echo "build_artifacts"
build_artifacts
$ touch build_output
Uploading artifacts...
build_output: found 1 matching files               
Uploading artifacts to coordinator... ok            id=56026 responseStatus=201 Created token=xxxxzzzz
Job succeeded



# test_artifacts job
Downloading artifacts for build_artifacts (56026)...
Downloading artifacts from coordinator... ok        id=56026 responseStatus=200 OK token=xxxxzzzz
$ echo "before_script"
before_script
$ ls
README.md
build_output
$ echo "test_artifacts"
test_artifacts
Job succeeded

As you can see the test_artifacts job downloads the artifacts before the before_script runs.