Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitlab-ci.yml, before_script and artifact

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.

like image 535
Henry Yang Avatar asked Oct 10 '18 00:10

Henry Yang


People also ask

What is artifacts in GitLab CI Yml?

Artifacts are files created as part of a build process that often contain metadata about that build's jobs like test results, security scans, etc. These can be used for reports that are displayed directly in GitLab or can be published to GitLab Pages or in some other way for users to review.

What is Before_script in GitLab CI?

These are scripts that you choose to be run before the job is executed or after the job is executed. These can also be defined at the top level of the YAML file (where jobs are defined) and they'll apply to all jobs in the . gitlab-ci. yml file.

What is Before_script?

before_script is used to define the command that should be run before all builds, including deploy builds. This can be an array or a multi-line string.

Where does GitLab CI store artifacts?

The artifacts are stored by default in /home/git/gitlab/shared/artifacts . Save the file and restart GitLab for the changes to take effect.


1 Answers

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.

like image 84
rnstlr Avatar answered Oct 16 '22 10:10

rnstlr