0

I have my Angular application running fine with Jenkins with all the stages defined. It has almost 600 unit test cases running with headless chrome as a Jenkins stage before build stage. Everything works as expected.

Now the test stage taking 6-10 mins depends on the server load even there are no changes in the spec files. Right now the test is mandatory in the pipeline syntax. I want to make it conditional.

I just want to add the condition in the test stage, not in the build stage. The condition would be, if there are no changes in the .spec file, Jenkins should skip the stage and execute Build stage.

stage('Test') {
            steps {
                sh 'npm run test:headless'
            }
        }

Then the build stage:

stage('Build') {
            steps {
                sh 'npm run build:dev'
            }
        }

Thanks in advance for your help.

Sudip Pal
  • 2,041
  • 1
  • 13
  • 16
  • Does this answer your question? [Determine If Any File Changed In Directory For Latest Git Commit](https://stackoverflow.com/questions/45067130/determine-if-any-file-changed-in-directory-for-latest-git-commit) – Daniel Habenicht Mar 13 '20 at 15:15
  • @DanielHabenicht Looks promising, but the solution provided for dir name, here spec files are in every directory, one directory may contain TS and SPEC both file changes or one, is there anything possible to find the diff using file extension under the SRC folder, in my case .spec files only – Sudip Pal Mar 13 '20 at 15:22
  • Additionally, just wanted to get the result for .spec file changes only not the .ts or .scss or .html file changes. – Sudip Pal Mar 13 '20 at 15:26
  • There might be multiple commits, just last two commit comparison will not work in my case. "Build now" is manual and we trigger the action when required. – Sudip Pal Mar 13 '20 at 15:33

1 Answers1

1

You can go through all Commits made since the last Merge Commit (which should be where you branched off) and search for files changed with spec.ts in their path:

lastMerge() {
    git rev-list origin..HEAD --max-parents=1 --max-count=1
}

changes() {
  git diff --name-only --diff-filter=AMR --cached $(lastMerge)..HEAD | grep spec.ts
}

if [ changes ] 
    then
        echo "start expensive operation"
fi

Credit to: Determine If Any File Changed In Directory For Latest Git Commit and How to find last git commit before a merge

Daniel Habenicht
  • 2,133
  • 1
  • 15
  • 36