-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
40 lines (39 loc) · 2.05 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
pipeline {
agent any
environment {
TARGET_BRANCH = 'main'
}
stages {
stage('Lint Commit Messages') {
steps {
script {
// Using withCredentials to securely provide GitHub credentials
withCredentials([usernamePassword(credentialsId: 'github-pat', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
// Correctly setting up Git to use credentials
sh 'git config credential.helper "!f() { echo username=\\$GIT_USERNAME; echo password=\\$GIT_PASSWORD; }; f"'
// Fetching the target branch to compare differences
sh "git fetch --no-tags origin +refs/heads/${env.TARGET_BRANCH}:refs/remotes/origin/${env.TARGET_BRANCH}"
// Checkout to the latest commit of the PR branch
sh "git checkout -B ${env.BRANCH_NAME} origin/${env.BRANCH_NAME}"
// Extracting only the last commit message from the PR branch
def lastCommitMsg = sh(script: "git log -1 --pretty=format:'%s'", returnStdout: true).trim()
// Check the last commit message against the Conventional Commits format
if (!lastCommitMsg.matches("^(feat|fix|docs|style|refactor|perf|test|chore|revert|ci|build)(!)?(\\(\\S+\\))?\\: .+")) {
echo "The last commit message does not follow Conventional Commits format:"
echo " - ${lastCommitMsg}"
error "The last commit message is not in the Conventional Commits format. PR cannot be merged."
}
}
}
}
}
}
post {
success {
echo 'The last commit message follows the Conventional Commits format.'
}
failure {
echo 'Commit message validation failed. Please ensure the last commit follows the Conventional Commits format.'
}
}
}