Cypress pipeline from different repo in Azure DevOps

Viewed 1150

I'm trying to add Cypress tests to an asp.net app. It's a .net 4.5 website and I'm not sure how to add it to the existing repo so I decided to make a separate one for it inside of the same Azure DevOps project. So my Azure DevOps project has two repos: one for the web app and another for Cypress.

I need to create a pipeline that runs whenever there are changes to the development branch of the web app repo. It needs to build and run the unit tests from the web app repo and then also run the Cypress tests from the Cypress repo.

I'm having a hard time putting this together. Can anyone help create a general pipeline that does these tasks? Something that can help me figure out what direction I need to go.

1 Answers

Since you want to run Cypress tests from another separate repository within your Web App pipeline, you must do a checkout for the Cypress repository in the yaml file from the Web App.

Assuming your pipeline will have no stages and consist of multiple jobs, e.g. one to execute anything from your Web App and one for the Cypress tests, this would be one possible approach:

trigger:
  - develop

resources:
  repositories:
    - repository: your-cypress-repo
      type: git
      name: your-azure-devops-project/your-cypress-repo

variables:
  - group: cypress-variables

jobs:
  - job:
      # execute the unit tests or build of your web app here

  - job: cypress_tests
    displayName: 'Cypress Tests'
    pool:
      vmImage: 'ubuntu-latest'
    steps:
      - checkout: your-cypress-repo

      - task: NodeTool@0
        inputs:
          versionSpec: '14.x'
          workingDirectory: $(Build.SourcesDirectory)
        displayName: 'Install Node.js'

      - task: Npm@1
        inputs:
          command: 'ci'
          workingDirectory: $(Build.SourcesDirectory)
        displayName: 'Execute npm clean-install'

      - script: |
          npm run {your package.json script for cypress test execution}
        workingDirectory: $(Build.SourcesDirectory)
        displayName: Cypress Tests
        env:
          CYPRESS_BASE_URL: '$(baseUrl)'
          CYPRESS_someOtherEnvVariable: '$(someOtherEnvVariable)'

Brief explanation of the steps:

  1. The trigger will be the develop branch.
  2. Then you define the necessary Cypress repository as resource.
  3. The variables are optional, but it makes sense to maintain necessary secrets or credentials for Cypress tests, e.g. passwords for test user, in a variable group configurable outside the pipeline definition.
  4. I didn't add details for the unit tests job, since you may already have that. The job for the Cypress tests first does a checkout of the Cypress repository (uses default branch), installs Node and all required packages and finally executes your Cypress tests.

As a tip, I also added how you can dynamically inject Environment variables to the Cypress Test execution. This can be done, by using the CYPRESS_ namespace as also described here.

Related