1
votes

I have a gitlab ci pipeline in my application repo, A, which calls an end to end testing Repo T to run its tests. The repo A pipeline succesfully triggers the tests from Repo T but if the test job fails in T, the job calling the test job in T from A still passes. How do I get repo A to track the result of Repo T's test job, and pass/fail its pipeline based off of the test jobs in T?

.gitlab-ci.yml for testing Repo T:

stages:
  - test

test:
  stage: test
  image: markhobson/maven-chrome:jdk-11
  artifacts:
    paths:
      - target/surefire-reports
  script:
    - mvn test
  only:
    - triggers

.gitlab-ci.yml from application repo A:

job1:
    stage: unit-tests ...
job2:
    stage: build ...
...

trigger-e2e-repo:
  stage: e2e-testing
  image: markhobson/maven-chrome
  script:
    - "curl -X POST -F token=repo-T-token -F ref=repo-T-branch https://repo-A/api/v4/projects/repo-T-id/trigger/pipeline"
  only:
    - repo-A-branch
2

2 Answers

1
votes

Since GitLab 11.8 you can trigger a pipeline via bridge job.

In GitLab 11.8, GitLab provides a new CI/CD configuration syntax to make this task easier, and avoid needing GitLab Runner for triggering cross-project pipelines.

With bridge jobs it is possible to mirror the status of the trigger pipeline to the calling pipeline.

You can mirror the pipeline status from the triggered pipeline to the source bridge job by using strategy: depend.

Example in your case:

trigger-e2e-repo:
  stage: e2e-testing
  trigger: repo-T
  strategy: depend

If the triggered pipeline with the test jobs fails, the calling pipeline also fails.

If you only want to execute a particular job in your repository "Repo T" when executed by a bridge job, then you should use only: pipeline (only) or rules: -if '$CI_PIPELINE_SOURCE == "pipeline"' (rules:if) instead of only: triggers.

1
votes

I wasn't able to use the bridge job property of mirroring a downstream job result as the version of my gitlab is before 11.8. I did manage to get it to work by creating a trigger for repo A, and making a call from repo T to repo A with this new second trigger. The remaining jobs in repo A will only be activated by triggers and setting of variables (JOB in this case ) as laid out below:

.gitlab-ci.yml for repo T:

test:
  stage: test
  script:
    - mvn test
    - "curl -X POST -F token=repo-A-token -F ref=$BRANCH -F variables[JOB]=build https://project.com/api/v4/projects/project_id/trigger/pipeline"

.gitlab-ci.yml in A

job1:
  ...
  except:
    - triggers
job2:
  ...
  except:
    - triggers
...
trigger-e2e-repo:
  stage: e2e-testing
  script:
    - "curl -X POST -F token=repo-B-token -F ref=repo-B-branch -F variables[BRANCH]=$CI_COMMIT_REF_NAME -F https://project-B/api/v4/projects/project-B-id/trigger/pipeline"
  except:
    - triggers

build_application_for_prod:
  stage: build_prod
  script:
    - "curl -X POST -F token=repo-A-token -F ref=$CI_COMMIT_REF_NAME -F variables[JOB]=deploy -F variables[SEND]=true https://foo/api/v4/projects/proj_A_id/trigger/pipeline"
  only:
    variables:
      -  $JOB == "build"

deploy_production_environment:
  stage: deploy_prod
  script:
    - ...
  only:
    variables:
      - $JOB == "deploy"

Note I also had to add the except statements for the jobs before the end to end tests in repo A so that they won't rerun and loop when repo A's API trigger is called later on.