4

I have two simple stages. (build and test). And I want jobs in the pipeline to run sequentially.

Actually, I want when I run the test job, it doesn't run until the build job was passed completely.

My gitlab file:

stages:
  - build
  - test

build:
  stage: build
  script:
    - mvn clean package
    only:
    - merge_requests

test:
  stage: test
  services:
  script:
    - mvn verify
    - mvn jacoco:report
  artifacts:
    reports:
      junit:
        - access/target/surefire-reports/TEST-*.xml
    paths:
      - access/target/site/jacoco
    expire_in: 1 week
  only:
    - merge_requests

Can I add

needs:
    - build

in the test stage?

rezza72
  • 127
  • 1
  • 11

1 Answers1

1

Based on the simplicity of your build file, i do not think that you actively need the needs. Based on the documentation, all stages are executed sequentially.

The pitfall you are in right now, is the only reference. The build stage will run for any branch, and for that ignore merge requests. if you add a only directive to your build job, you might get the result you are looking for like:


build:
  stage: build
  script:
    - mvn clean package
  only:
    - merge_requests
    - master # might be main, develop, etc. what ever your longliving branches are

This way it will not be triggered for each branch but only for merge requests and the long living branches. see the only documentation. Now the execution is not assigned to the branch but to the merge request, and you will have your expected outcome (at least what i assume)

Simon Schrottner
  • 4,146
  • 1
  • 24
  • 36
  • Thanks for the answer. Well, I want the build stage done and compiled. Do I need to add any more scripts to do this? – rezza72 Feb 21 '21 at 08:59
  • oh, I forgot to add the `only` in the build stage. actually, there is `only` in the real gitlab-ci file. but I forgot to add that in the question. (I edited the main question). – rezza72 Feb 21 '21 at 09:03
  • stages are executed sequentially anyways. as long as you have them in separate stages, this will happen. And the order is defined by the order you define them. So they will run sequentially based on your code. The only thing that can happen is, that the pipeline is triggered for a different 'context' like you have a MR but it is triggered for a the branch and not the MR - you can see this, by taking a look at the pipeline. if the reference is the branch name, or the merge request ID – Simon Schrottner Feb 21 '21 at 09:03
  • important to note is, if you want to have generated data available in the follow up job, you need to either cache it, or store it as an artifact. Each stage is encapsulated and normally has now information about prior jobs – Simon Schrottner Feb 21 '21 at 09:05
  • Sorry if I don't get it exactly. Please look at the [image](https://i.imgur.com/mwvoaCW.png) . Look, if someone clicks on the test, I want the test stage not to run, and I want the person who clicked, to be noticed that the build stage must be done first before the test can run. Now I do not know if my wanted is irrational or not. In fact, my intention is to run the build stage before each test. thanks in advance. – rezza72 Feb 21 '21 at 09:32
  • what you are looking for is `when: manual` - either you put it only on the build job (then tests will be run automatically) or you add it to both of them, than the user has to click twice :) https://docs.gitlab.com/ee/ci/yaml/#when – Simon Schrottner Feb 21 '21 at 09:37
  • generally i would run the build and test stage always, just to verify it is working. Additionally i would put a `interubtible: true` to all the stages when executed from a merge requests. This way a new commit will cancel the current pipeline and start a new one. - the manual part is always tricky - because either you do the whole build in manual, and auto trigger tests, but normally doing both manual is more tideous than just one click :) – Simon Schrottner Feb 21 '21 at 09:39
  • when I put the `when: manual` in the build, the pipeline build stage was skipped and the test stage was passed. And when I add `when: manual` in the test so, IntelliJ shows [this](https://i.imgur.com/QHPA7oH.png) tip. – rezza72 Feb 21 '21 at 12:52
  • what can I do for solving the problem? – rezza72 Feb 26 '21 at 08:47