Search code examples
gitlabgitlab-cigitlab-pages

Gitlab CI: build for CI and Merge request but publish only CI to pages


I have a .gitlab-ci.yml file which I want to use to run a script for merge request validation. The same script should be used in CI, but only there the result should be published to gitlab pages. Also, only for the CI, the result should be cached.

This is a simplified version of the current .gitlab-ci.yml:

pages:
  stage: deploy
  script:
  - mkdir public/
  - touch public/file.txt
  artifacts:
    paths:
    - public
  only:
  - master
  cache:
    paths:
    - fdroid

(The real-world code is in the fdroid-firefox gitlab repo.)

There are 2 ways how the pipeline is being triggered. Depending on this, I do or do not want to publish to pages:

  1. by merge request validation. In this case, I want to execute the script part, but I don't want to publish or cache the result (otherwise, anyone with permissions to create a merge request could overwrite the gitlab pages content).
  2. by CI (which is triggered both after check-in to master branch and following a schedule). In this case, I want the result to be cached and the gitlab pages to be updated.

I already tried splitting up the stages:

stages:
  - build
  - deploy

build_repo:
  stage: build
  script:
  - mkdir public/
  - touch public/file.txt

pages:
  stage: deploy
  script: echo "publish to Gitlab pages"
  artifacts:
    paths:
    - public
  only:
  - master
  cache:
    paths:
    - fdroid

(Original .gitlab-ci.yml file) But by doing this, the pages:deploy stage faled because it does not have access to the result of the build stage. The pages:deploy stage shows an error symbol and on the tooltip it says missing pages artifacts. (real world log). The log says:

Uploading artifacts for successful job
00:01
Uploading artifacts...
WARNING: public: no matching files                 
ERROR: No files to upload  

What am I doing wrong that I don't have access to the result of the build stage?

How can I run the script section in both cases but still deploy to pages only from master branch?


Solution

  • You don't save your public path artifacts in your build job. And that's why they are missing at next deploy stage pages job.

    You have this:

    build_repo:
      stage: build
      script:
        - your script
    

    Try to save artifacts in your build job like this:

    build_repo:
      stage: build
      script:
        - your script
      artifacts:
        when: always
        paths:
          - public
    

    So they will be passed to the next stage deploy and pages job could see them.