Reusable Jenkins pipeline functions for common CI/CD operations including Git/branch management, GitHub integration, JIRA integration, Mattermost notifications, and hash-based caching.
- Go to Jenkins → Manage Jenkins → Configure System
- Scroll to Global Pipeline Libraries
- Click Add
- Configure:
- Name:
irontec-jenkins-library - Default version:
main(or your preferred branch) - Retrieval method: Modern SCM
- Source Code Management: Git
- Project Repository:
https://github.com/irontec/jenkins-shared-library.git
- Name:
Add this at the top of your Jenkinsfile:
@Library('irontec-jenkins-library@main') _
pipeline {
// ... your pipeline
}- GitHub Branch Source Plugin: For GitHub integration
- GitHub Plugin: For GitHub notifications
- JIRA Plugin: For JIRA integration
- Mattermost Plugin: For Mattermost notifications
Configure these in your pipeline or globally:
environment {
// GitHub Integration
GITHUB_CREDENTIALS = 'your-github-credentials-id'
GITHUB_CONTEXT_PREFIX = 'my-project-ci' // Optional, default: "ci"
// JIRA Integration
JIRA_TICKET = getJiraTicket() // Auto-extracted from branch
// Mattermost Integration
MATTERMOST_CHANNEL = '#my-project-ci' // Optional
// Hash Caching
HASH_FILE = "${JENKINS_HOME}/caches/${JOB_NAME}/tested_hashes.txt"
MAX_HASHES = 100 // Optional, default: 100
}Extracts JIRA ticket identifier from branch name.
Expected format: TICKET-123-description
Returns: String - JIRA ticket (e.g., "PROJ-123") or empty string
Example:
def ticket = getJiraTicket()
if (ticket) {
echo "Working on ticket: ${ticket}"
}Gets the current branch name.
Returns: String - Current branch name
Example:
def branch = getBranchName()
echo "Building branch: ${branch}"Gets the base branch for the current build.
Returns: String - Base branch name
Example:
def base = getBaseBranch()
echo "Comparing against: ${base}"Gets the Docker tag for the current build.
Returns: String - Docker tag to use
Example:
def tag = getDockerTag()
docker.build("myapp:${tag}", ".")Checks if any commit in the current PR contains a specific tag.
Parameters:
module(String): Tag to search for in commit messages
Returns: boolean - True if tag is found
Example:
if (hasCommitTag("backend:")) {
echo "Backend changes detected"
}Checks if a pull request has a specific label.
Parameters:
label(String): Label name to check
Returns: boolean - True if PR has the label
Example:
if (hasLabel("skip-ci")) {
echo "CI skipped by label"
return
}Notifies GitHub of a successful stage completion.
Parameters:
contextPrefix(String, optional): Prefix for context (default: env.GITHUB_CONTEXT_PREFIX or "ci")description(String, optional): Description (default: "Finished")credentialsId(String, optional): Credentials ID (default: env.GITHUB_CREDENTIALS)
Example:
stage('tests') {
steps {
sh 'make test'
}
post {
success { notifySuccessGithub() }
}
}Notifies GitHub of a failed stage.
Parameters: Same as notifySuccessGithub
Example:
post {
failure { notifyFailureGithub() }
}Notifies GitHub of an unstable/cancelled stage.
Parameters: Same as notifySuccessGithub
Example:
post {
unstable { notifyUnstableGithub() }
}Marks a GitHub pull request as approved.
Parameters:
botUsername(String, optional): Bot username (default: "ironArt3mis")
Example:
post {
success { githubMarkApproved() }
}Marks a GitHub pull request as requiring changes.
Parameters:
botUsername(String, optional): Bot username (default: "ironArt3mis")message(String, optional): Review message
Example:
post {
unstable {
githubMarkChangesRequested("jenkins-bot",
"Tests failed. Please fix before merging.")
}
}Updates GitHub pull request title to include JIRA ticket.
Parameters:
jiraTicket(String, optional): JIRA ticket identifier
Example:
def ticket = getJiraTicket()
if (ticket) {
env.JIRA_TICKET = ticket
githubUpdatePullRequestTitle()
}Updates JIRA issue custom fields with PR and branch information.
Parameters:
site(String, optional): JIRA site URL (default: 'irontec.atlassian.net')jiraTicket(String, optional): JIRA ticket identifier
Custom fields updated:
customfield_10165: Pull Requestcustomfield_10166: Branch name
Example:
def ticket = getJiraTicket()
if (ticket) {
env.JIRA_TICKET = ticket
jiraUpdateCustomFields('mycompany.atlassian.net')
}Sends a failure notification to Mattermost.
Parameters:
channel(String, optional): Mattermost channel (default: env.MATTERMOST_CHANNEL)branch(String, optional): Branch to monitor (default: "main")color(String, optional): Message color (default: "#FF0000")icon(String, optional): Emoji icon (default: ":red_circle:")
Example:
environment {
MATTERMOST_CHANNEL = '#my-project-ci'
}
post {
failure { notifyFailureMattermost() }
}Sends a fixed/recovered notification to Mattermost.
Parameters: Same as notifyFailureMattermost
Example:
post {
fixed { notifyFixedMattermost() }
}Calculates a hash of all files in a directory.
Parameters:
dir(String): Directory path to hash
Returns: String - SHA256 hash of directory contents
Example:
def backendHash = getCurrentHash("backend")
def frontendHash = getCurrentHash("frontend")Checks if a hash has already been tested.
Parameters:
hash(String): Hash to checkhashFile(String, optional): Path to cache file
Returns: boolean - True if hash has been tested before
Example:
def hash = getCurrentHash("app")
if (isHashTested(hash)) {
echo "Already tested, skipping"
return
}Saves a tested hash to the cache file.
Parameters:
hash(String): Hash to savehashFile(String, optional): Path to cache filemaxHashes(Integer, optional): Maximum number of hashes to store
Example:
def hash = getCurrentHash("app")
// Run tests...
saveTestedHash(hash)@Library('irontec-jenkins-library@main') _
pipeline {
agent any
environment {
// Configure for your project
GITHUB_CREDENTIALS = 'github-credentials-id'
GITHUB_CONTEXT_PREFIX = 'my-project'
MATTERMOST_CHANNEL = '#my-project-ci'
// Auto-generated values
DOCKER_TAG = getDockerTag()
BRANCH_NAME = getBranchName()
BASE_BRANCH = getBaseBranch()
JIRA_TICKET = getJiraTicket()
HASH_BACK = getCurrentHash("backend")
HASH_FILE = "${JENKINS_HOME}/caches/${JOB_NAME}/hashes.txt"
MAX_HASHES = 100
}
stages {
stage('Update PR Info') {
when {
expression { env.JIRA_TICKET }
}
steps {
script {
jiraUpdateCustomFields()
githubUpdatePullRequestTitle()
}
}
}
stage('Check Cache') {
steps {
script {
env.CACHED = isHashTested(env.HASH_BACK)
echo "Hash tested before? ${env.CACHED}"
}
}
}
stage('Tests') {
when {
expression { env.CACHED != "true" }
}
steps {
sh 'make test'
}
post {
success { notifySuccessGithub() }
failure { notifyFailureGithub() }
}
}
}
post {
failure {
notifyFailureMattermost()
}
success {
githubMarkApproved()
saveTestedHash(env.HASH_BACK)
}
fixed {
notifyFixedMattermost()
}
}
}stage('Backend') {
when {
anyOf {
expression { hasLabel("force-backend-tests") }
expression { hasCommitTag("backend:") }
branch "main"
}
}
steps {
sh 'make test-backend'
}
}environment {
HASH_APP = getCurrentHash("app")
HASH_WEB = getCurrentHash("web")
}
stages {
stage('Check Caches') {
steps {
script {
env.APP_CACHED = isHashTested(env.HASH_APP)
env.WEB_CACHED = isHashTested(env.HASH_WEB)
}
}
}
stage('Test App') {
when {
expression { env.APP_CACHED != "true" }
}
steps {
sh 'cd app && make test'
}
}
}
post {
success {
saveTestedHash(env.HASH_APP)
saveTestedHash(env.HASH_WEB)
}
}When adding new functions to this library:
- Create a new
.groovyfile in thevars/directory - Use the
call()method for the main function - Add comprehensive documentation with examples
- Make functions configurable via parameters and environment variables
- Avoid hardcoding project-specific values
- Update this README with the new function
Internal use only - Irontec