Search code examples
gitlabgitlab-ci

Gitlab CI running two different test stages?


So I am sure this is probably simple but im not super familiar with Gitlab CI and have the need to run the same command (Well slightly different for each "environment") two different times.

Right now I run automation tests using Playwright, and typically in playwright you have different projects which have different base url's/etc...

Currently my current gitlab-ci.yml file looks like this:

  - automation_tests 

tests:
  stage: automation_tests
  image:
    name: mcr.microsoft.com/playwright:v1.33.0-focal #official playwright image from microsoft
    entrypoint:
      ["/bin/bash", "-c", "ln -snf /bin/bash /bin/sh && /bin/bash -c $0"]
  script:
    - npm ci
    - npx playwright install chromium
    - $SOME_HIDDEN_VARIABLE="SOME_HIDDEN_VARIABLE" npx playwright test --project=qa --project=stage
  artifacts:
    paths: #assuming default playwright artifacts paths
      - ./playwright-report/
    when: always
    expire_in: 5 days #artifacts will purged after 5 days of test run

Pretty simple, it runs both projects one after the other in one "test" job.

I essentially need to be able to split it into two jobs with each having it's own --project="whatever" and also so I can split up the environment variables (Since I will have to do that for other projects).

Is it as simple as just adding another "stage" block named different or what?

Thanks


Solution

  • If both jobs are very identical, you can use a hidden job as base for your jobs with the keyword extends and override the keywords you want (scripts, variables, rules, etc).

    GitLab CI/CD will not process the hidden job but it can be reusable as a template.

    
    stages:
      - automation_tests 
    
    .tests:
      stage: automation_tests
      image:
        name: mcr.microsoft.com/playwright:v1.33.0-focal #official playwright image from microsoft
        entrypoint:
          ["/bin/bash", "-c", "ln -snf /bin/bash /bin/sh && /bin/bash -c $0"]
      script:
        - npm ci
        - npx playwright install chromium
        - $SOME_HIDDEN_VARIABLE="SOME_HIDDEN_VARIABLE" npx playwright test --project=$PROJECT_NAME
      artifacts:
        paths: #assuming default playwright artifacts paths
          - ./playwright-report/
        when: always
        expire_in: 5 days #artifacts will purged after 5 days of test run
      variables:
        PROJECT_NAME: ""
    
    test_qa:
      extends: .tests
      variables:
        PROJECT_NAME: "qa"
    
    test_stage:
      extends: .tests
      variables:
        PROJECT_NAME: "stage"
    
    

    In this example, both jobs will run at the same time. We can use the keyword needs (or dependencies if we want to fetch an artifact) if we want an order between the two jobs.