azure-devopsazure-pipelinescommitlint

I am implementing commitlint in an Azure CI pipeline but cannot get the reference to another branch with the --from tag to work


I am working with a yaml file in my .net core project in Azure DevOps which looks like this:

trigger:
- main

pool:
  vmImage: windows-latest

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

stages:
- stage: LintCommitMessages
  jobs:
  - job: Validate
    displayName: Validate
    steps:
    - pwsh: |      
        npm install @commitlint/config-conventional @commitlint/cli
        npx commitlint --from refs/remotes/origin/main
      displayName: Validate commit message

I keep getting the following error message:

Starting: Validate commit message
==============================================================================
Task         : PowerShell
Description  : Run a PowerShell script on Linux, macOS, or Windows
Version      : 2.212.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
"C:\Program Files\PowerShell\7\pwsh.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\112b3b4e-829d-4fde-a720-44510788ca2d.ps1'"

added 200 packages, and audited 201 packages in 16s

27 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
D:\a\1\s\node_modules\@commitlint\cli\lib\cli.js:123
        throw err;
        ^

Error: fatal: ambiguous argument 'refs/remotes/origin/main..HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

    at Transform._transform (D:\a\1\s\node_modules\git-raw-commits\index.js:84:30)
    at Transform._read (D:\a\1\s\node_modules\readable-stream\lib\_stream_transform.js:177:10)
    at Transform._write (D:\a\1\s\node_modules\readable-stream\lib\_stream_transform.js:164:83)
    at doWrite (D:\a\1\s\node_modules\readable-stream\lib\_stream_writable.js:409:139)
    at writeOrBuffer (D:\a\1\s\node_modules\readable-stream\lib\_stream_writable.js:398:5)
    at Transform.Writable.write (D:\a\1\s\node_modules\readable-stream\lib\_stream_writable.js:307:11)
    at Socket.ondata (node:internal/streams/readable:754:22)
    at Socket.emit (node:events:525:35)
    at Socket.emit (node:domain:489:12)
    at addChunk (node:internal/streams/readable:315:12)
##[error]PowerShell exited with code '1'.
Finishing: Validate commit message

Anyone who has had the same issue?

I have tried most variations of the path that I can come up with so I must be misunderstanding how to enter this one completely.

I have also tried this variant and then the pipeline runs but it does not capture any of the errors that I try entering in the commit messages so everything passes through:

trigger:
- main

pool:
  vmImage: windows-latest

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  main_branch: ''

stages:
- stage: LintCommitMessages
  jobs:
  - job: lint
    pool:
      vmImage: ubuntu-latest
    displayName: Lint commit message
    steps:
    - script: npm install @commitlint/{config-conventional,cli}
      displayName: Install commitlint
    - script: |
        main_branch=$(git branch -a | grep origin/main$ || git branch -a | grep origin/master$)
        npx commitlint --from ${main_branch} --verbose
      displayName: Run commitlint

This is the result:

Starting: Run commitlint
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.212.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/de0c00c9-35ff-41e6-9297-062f15e54e74.sh
⧗   input: Merge pull request 13 from feat/change-pipeline into main
✔   found 0 problems, 0 warnings
Finishing: Run commitlint


Solution

  • I found out the solution now and it was a setting in Azure DevOps for the pipeline. By navigating to the pipeline and then select edit you can then select triggers in the top right corner:

    Trigger

    You will then see a menu item named YAML and after clicking that you can select Get sources. At the bottom of the options that comes up to the right there is a choice called "Shallow fetch", you need to uncheck this one and then everything will work :-)