Using only with a dependency

Hi

First time posting here.

I’m just wondering how I should use a stage that runs only for merge_requests and for it to use a dependency. Very simply, I have an initial build stage and a test:pr stage that requires the build artifact. But I find my test:pr stage runs as detached. I did have a look in the docs, but haven’t figured out how to do it, so wondered if someone could help correct my ci file. nb We are self hosting Gitlab free version

stages:
  - build
  - dev
# omitted some other stages for brevity

build:ranorex:
  stage: build
  script:
      - nuget restore $CI_PROJECT_DIR\MySolution.sln
      - msbuild $CI_PROJECT_DIR\MySolution.sln
  artifacts:
    expire_in: 1 week
    paths:
      - $CI_PROJECT_DIR\User_Interfaces___Regression
  tags:
    - ranorex

test:pr:
  stage: dev
  script:
    - User_Interfaces___Regression/bin/Debug/User_Interfaces___Regression.exe /a:["gitlab"] /testsuite:User_Interfaces___Regression.rxtst /ju
  artifacts:
    expire_in: 1 week
    reports:
      junit: $CI_PROJECT_DIR\*.junit.xml
  only:
    refs:
      - merge_requests
    variables:
      - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "qa"
  dependencies:
    - build:ranorex
  tags:
    - ranorex

// omitted the other stages for brevity

Thanks :slight_smile:

Hi @jonny7

I think you probably want to use `workflow:rules here to ensure that your are always using merge request pipelines.

So, you would have something like:

stages:
  - build
  - dev

workflow:
  rules:
    - if: $CI_MERGE_REQUEST_IID  # Run in merge requests
    - if: $CI_COMMIT_TAG  # Run when a tag is pushed
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH  # Run if a commit is pushed to the default branch

build:ranorex:
  stage: build
  script:
      - nuget restore $CI_PROJECT_DIR\MySolution.sln
      - msbuild $CI_PROJECT_DIR\MySolution.sln
  artifacts:
    expire_in: 1 week
    paths:
      - $CI_PROJECT_DIR\User_Interfaces___Regression
  tags:
    - ranorex

test:pr:
  stage: dev
  script:
    - User_Interfaces___Regression/bin/Debug/User_Interfaces___Regression.exe /a:["gitlab"] /testsuite:User_Interfaces___Regression.rxtst /ju
  artifacts:
    expire_in: 1 week
    reports:
      junit: $CI_PROJECT_DIR\*.junit.xml
    rules: # Only on merge requests
        - if $CI_MERGE_REQUEST_IID
          when: on_success
       - when: never
    variables:
      - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "qa"
  dependencies:
    - build:ranorex
  tags:
    - ranorex

The test stage here also uses rules to limit the scope of its job.

HTH,

Sarah

1 Like

Thanks! I used this as a template and all seems good!

1 Like