-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
77 lines (76 loc) · 3.78 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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."
}
}
}
}
}
stage('Terraform Format Check') {
steps {
script {
// Check if there are any Terraform files to validate
def terraformFiles = sh(script: "find . -name '*.tf'", returnStdout: true).trim()
if (terraformFiles) {
// Check the formatting of Terraform files
def fmtOutput = sh(script: "terraform fmt -check -recursive", returnStdout: true, returnStatus: false).trim()
if (fmtOutput) {
echo "The following Terraform files are not properly formatted:"
echo fmtOutput
error "Please format your Terraform files with 'terraform fmt'."
} else {
echo "All Terraform files are properly formatted."
}
} else {
echo "No Terraform files found to format."
}
}
}
}
stage('Terraform Validate') {
steps {
script {
// Check if there are any Terraform files to validate
def terraformFiles = sh(script: "find . -name '*.tf'", returnStdout: true).trim()
if (terraformFiles) {
// Initialize Terraform (required before validation)
sh "terraform init -backend=false"
// Validate the Terraform files
sh "terraform validate"
} else {
echo "No Terraform files found to validate."
}
}
}
}
}
post {
success {
echo 'All checks passed successfully.'
}
failure {
echo 'Validation failed. Please check the logs for details.'
}
}
}