How can I run ng test {project} command in gitlab CI/CD for each project with file changes in a merge request

  angular, gitlab, gitlab-ci, yaml

So I am setting up a large monorepo made up of multiple projects.

I currently have my root gitlab-ci.yml set up like this

include :
  - '/apps/open-api/open-api-gitlab-ci.yml'
  - '/apps/app-platform-pc/app-platform-pc-gitlab-ci.yml'
  - '/apps/app-platform-bb/app-platform-bb-gitlab-ci.yml'
  - '/apps/app-platform-ifa/app-platform-ifa-gitlab-ci.yml'
  - '/apps/app-platform-user/app-platform-user-gitlab-ci.yml'
  - '/apps/app-platform-hybrid/app-platform-hybrid-gitlab-ci.yml'
  - '/apps/internal/authorable-content-manager/authorable-content-manager-gitlab-ci.yml'


image: $Image_URL
variables:
  DOCKER_DRIVER: overlay2

cache:
  paths:
    - node_modules/

before_script:
  - echo "Start of CI/CD Pipeline"

stages:
  - test
  - build
  - deploy
  - cloudflare
  - cloudflare-production

after_script:
  - echo "End of CI/CD Pipeline"

And then as an example, my app-platform-pc-gitlab-ci.yml file looks like this

unit tests:
  stage: test
  script:
    - npm i
    - sh io-resources/husky-functions/lint-test-build.sh
  only:
    - merge_requests
  tags:
    - angular
    - dev
  except:
    variables:
      - $RUN_JOBS_DASHBOARD == "false"
      - $RUN_JOBS_IO == "true"
      - $RUN_JOBS_OPEN_API == "true"
      - $RUN_JOBS_BusinessBanking == "true"
      - $RUN_JOBS_Hybrid == "true"

build-dashboard:
  <redacted>

deploy-dashboard:
  <redacted>

Then in that io-resources/husky-functions/lint-test-build.sh I have

#!/bin/env bash

function get_staged {
    git diff HEAD~ --name-only
}
STAGED_FILE=$(get_staged)

echo $STAGED_FILE

#Check for changes in project 1
if echo "$STAGED_FILE" | grep 'apps/private-client-apps/project1'; then
    ng test --project=project1
    ng build --project app-platform-pc --prod --base-href /io/ --subresource-integrity
else
    echo "Staged changes-"$STAGED_FILE
    exit 0
fi

#Check for changes in project 1
if echo "$STAGED_FILE" | grep 'apps/private-client-apps/project2'; then
    ng test --project=project2
    ng build --project app-platform-pc --prod --base-href /io/ --subresource-integrity
else
    echo "Staged changes-"$STAGED_FILE
    exit 0
fi

etc, etc , etc.

This works, but it really doesn’t seem like it should be the best way of doing things. Please note that within each app, there are multiple sub apps. So I dont want to just run tests for the entire app-platform-pc thing, I want to run tests for the neccesary sub-modules in that app. For example, private-client-apps/project1

Is there a way of doing this with pure yaml files? I was thinking of using the

only:
   changes:

syntax, but then it seems like I’d to create a test step for every single project in app-platform-pc-gitlab-ci, which will bloat that file a lot. I suppose its not the end of the world, but I was wondering if there’s a better way. Like somehow passing a variable to the script section so I can run ng-test {variable} depending on the folders detected by the changes section.

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.