Jenkins Integrations: Git Hosting, Slack and Email Notifications, REST API, CLI and Jira
Integration Map
text
Git host (GitHub/GitLab/Bitbucket) ββwebhookβββΊ Jenkins ββstatus checkβββΊ pull request
β
ββββΊ Slack / email / Teams (notify)
ββββΊ Jira (link build to issue)
ββββΊ SonarQube, Nexus (quality, artifacts)
ββββ REST API / CLI (scripts and bots call Jenkins)
GitHub
| Piece | Purpose |
|---|---|
| GitHub Branch Source plugin | Multibranch and Organization Folder jobs: discovers branches and pull requests |
| GitHub App credential | Better than a personal token: scoped, higher API limits, belongs to the organization |
Webhook to https://jenkins.example.com/github-webhook/ | Starts builds instantly on push and pull request events |
| Commit status / Checks API | Shows pass or fail on the pull request |
- Create a GitHub App with permissions: Contents (read), Metadata (read), Pull requests (read), Commit statuses (read and write), Checks (read and write)
- Install it on your organization or repositories
- In Jenkins add the GitHub App credential (App ID and private key)
- Create an Organization Folder or a Multibranch job using that credential
- Add the webhook (the plugin can manage hooks) with a secret
groovy
pipeline {
agent any
stages {
stage('Test') {
steps { sh './mvnw -B verify' }
}
}
post {
success { githubNotify context: 'ci/jenkins', status: 'SUCCESS', description: 'Build passed' }
failure { githubNotify context: 'ci/jenkins', status: 'FAILURE', description: 'Build failed' }
}
}
Require the check: In GitHub branch protection, require the Jenkins status check before merging. Then broken code cannot reach
main.
GitLab and Bitbucket
| Host | Plugin | Notes |
|---|---|---|
| GitLab | GitLab plugin or GitLab Branch Source | Webhook URL https://jenkins.example.com/project/JOBNAME, secret token, reports status to merge requests |
| Bitbucket Cloud / Server | Bitbucket Branch Source, Bitbucket Push and Pull Request | Webhook per repository, app passwords or access tokens |
| Gitea / Azure Repos | Their branch source plugins | Same pattern: webhook in, status out |
groovy
options { gitLabConnection('gitlab-main') }
triggers { gitlab(triggerOnPush: true, triggerOnMergeRequest: true, branchFilterType: 'All') }
post {
success { updateGitlabCommitStatus name: 'jenkins', state: 'success' }
failure { updateGitlabCommitStatus name: 'jenkins', state: 'failed' }
}
Slack Notifications
groovy
def notify(String status) {
def color = [SUCCESS: 'good', UNSTABLE: 'warning', FAILURE: 'danger', ABORTED: '#999999'][status] ?: '#999999'
def changes = currentBuild.changeSets.collectMany { it.items }.collect { "${it.author.fullName}: ${it.msg.take(60)}" }.take(5).join('\n')
slackSend(channel: '#builds', color: color,
message: "*${status}* ${env.JOB_NAME} #${env.BUILD_NUMBER} (${currentBuild.durationString.replace(' and counting', '')})\n<${env.BUILD_URL}|Open build>\n${changes}")
}
pipeline {
agent any
stages { stage('Build') { steps { sh 'make' } } }
post {
failure { script { notify('FAILURE') } }
unstable { script { notify('UNSTABLE') } }
fixed { script { notify('FIXED') } } // first success after failures
}
}
- Send messages on failure, unstable and fixed only. Messages on every success become noise
- Include: job, build number, who changed what, link to the log, duration
- Use a separate channel for production deploy events
- Store the Slack token in credentials (Slack plugin settings)
Email Notifications
groovy
post {
failure {
emailext(
subject: "FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
body: '''${SCRIPT, template="groovy-html.template"}''',
recipientProviders: [culprits(), requestor(), developers()],
attachLog: true, compressLog: true)
}
}
- Install Email Extension (
emailext) and set SMTP in Manage Jenkins β System recipientProviderssends to people who changed the code (culprits), the starter (requestor) and committers (developers)- Use
mailfor a very simple message
Jenkins REST API
Almost every Jenkins page has an API: add /api/json to the URL. Use API tokens for authentication.
bash
J=https://jenkins.example.com
AUTH="-u ci-bot:$JENKINS_TOKEN"
# Last build result of a job
curl -sS $AUTH "$J/job/team-a/job/shop/lastBuild/api/json?tree=number,result,duration,timestamp" | jq
# Build with parameters
curl -sS -X POST $AUTH "$J/job/team-a/job/shop/buildWithParameters?BRANCH=main&DEPLOY=false" -D - -o /dev/null | grep -i location
# Queue and executors
curl -sS $AUTH "$J/queue/api/json?tree=items[task[name],why]" | jq
curl -sS $AUTH "$J/computer/api/json?tree=computer[displayName,offline,numExecutors]" | jq
# Console log
curl -sS $AUTH "$J/job/team-a/job/shop/42/consoleText" | tail -50
# Stop a build
curl -sS -X POST $AUTH "$J/job/team-a/job/shop/42/stop"
| Tip | Detail |
|---|---|
Use ?tree= to ask only for fields you need | Smaller, faster responses |
| POST requests need CSRF crumb only with password auth | API tokens do not need a crumb |
/job/FOLDER/job/NAME/ | Each folder level needs /job/ |
A queued build returns a Location header | Follow it to learn the build number |
Jenkins CLI
bash
# Download the client from your own Jenkins
curl -sSO https://jenkins.example.com/jnlpJars/jenkins-cli.jar
alias jcli='java -jar jenkins-cli.jar -s https://jenkins.example.com/ -auth ci-bot:$JENKINS_TOKEN -webSocket'
jcli who-am-i
jcli list-jobs team-a
jcli build team-a/shop -p BRANCH=main -s -v # wait and show log
jcli console team-a/shop 42
jcli install-plugin git -deploy
jcli safe-restart
Admin power: CLI users with admin rights can change a lot. Create a limited service account for CLI scripts.
Trigger Builds Remotely
bash
# Job option "Trigger builds remotely (e.g., from scripts)" with an authentication token
curl -sS "https://jenkins.example.com/job/deploy/build?token=$JOB_TOKEN"
# Generic Webhook Trigger plugin: map JSON fields into variables
curl -sS -X POST -H "Content-Type: application/json" -d '{"ref":"refs/heads/main","service":"shop"}' \
"https://jenkins.example.com/generic-webhook-trigger/invoke?token=$HOOK_TOKEN"
Jira
- Install the Jira plugin and connect it with a service account
- Mention issue keys in commit messages (
SHOP-123: fix cart total). Jenkins links builds to the issue - Use a pipeline step to comment or move an issue, for example
jiraAddComment, or call the Jira REST API withcurland a credential - Use the issue key in the branch name (
feature/SHOP-123-cart) so builds and pull requests link together
Practice Tasks
- Create a Multibranch job for a GitHub repository using a GitHub App credential
- Add a webhook, open a pull request and see the status check on it
- Send a Slack message on failure with the list of changes
- Read the last build result and the queue with
curlandjq - Trigger a parameterized build from a script with an API token
Conclusion
Jenkins connects to Git hosts through webhooks in and status checks out, to chat and email through notifications limited to useful events, and to scripts through the REST API and CLI with API tokens. Prefer GitHub App credentials, require the Jenkins check on protected branches, and keep tokens in credentials.
Quick Summary: GitHub App + webhook + status; GitLab/Bitbucket plugins; Slack/email on failure+fixed; REST API tree=; CLI; remote trigger/Generic Webhook; Jira keys.

