Jenkins Workflow Example
About 612 wordsAbout 2 min
JenkinsPipline
2025-03-04
This file contains a Jenkins pipeline script written in Groovy. It defines a Jenkins pipeline that automates the process of building, testing, and deploying a project. Here's a breakdown of how it works:
// Function to send notifications to Telegram
def notifyTelegram() {
// Define Telegram group ID and bot token
def GROUP_ID = "-4781302032"
def TOKEN_ID = "5565174183:AAHX3xouHwDCs1Z4UEIyUyNdGWIuJLAWvTc"
// Get environment variables
def JOB_NAME = env.JOB_NAME
def BUILD_ID = env.BUILD_ID
def GIT_BRANCH = env.GIT_BRANCH
// Get build change description
def CHANGES = sh(script: """
curl --user admin:114b257b7077e01f2f8080580235d921a5 --silent http://127.0.0.1:8080/job/${JOB_NAME}/lastBuild/changes | grep detail | grep '<li>' | sed 's/<li>/\\n/g' | cut -d'(' -f1 | tail -n +2 | awk '{print NR ". " \$0}'
""", returnStdout: true).trim()
// Construct message content
def MSG = "<b>项目:${JOB_NAME}</b>\n" +
"<pre>构建ID: ${BUILD_ID}\n" +
"分支: ${GIT_BRANCH}\n" +
"更新描述:\n" +
"${CHANGES}</pre>"
// Send Telegram message
sh """
curl -X POST --data chat_id="${GROUP_ID}" --data-urlencode "text=${MSG}" "https://api.telegram.org/bot${TOKEN_ID}/sendMessage?parse_mode=HTML"
"""
}
//A pipeline in Jenkins is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. It allows you to define the entire workflow of your build, test, and deploy process as code. This code is typically written in Groovy and is stored in a Jenkinsfile.
pipeline {
//the agent any directive specifies that the pipeline can run on any available Jenkins agent (also known as a node or executor). This means that Jenkins will select any available agent to execute the pipeline stages.
agent any
//the environment block is used to define environment variables that will be available to all stages within the pipeline. These variables can be used to store configuration values, paths, credentials, and other information that is needed during the execution of the pipeline.
environment {
// Define environment variables
GO_PATH = "/usr/local/go/bin/go" // Path to the Go binary
DOCKER_REGISTRY = "registry.3333d.vip" //Docker registry URL
APP_DIR = "/data/bxbw-dev/${JOB_NAME}" //Directory where the application is stored.
GIT_REPO = "http://43.199.1.126:9099/Brazilian/brazilianbaowang-h5.git" //Git repository URL, where the source code placed
GIT_CREDENTIALS_ID = "fe58594e-66a0-46de-a15e-587ab3cda744" //Credentials ID for accessing the Git repository.????
}
//the stages block is used to define a sequence of stages that the pipeline will execute. Each stage represents a distinct phase in the pipeline, such as building, testing, or deploying the application. Stages help organize the pipeline into logical steps and can contain multiple steps that define the actions to be performed.
stages {
stage('Checkout') {
steps {
script {
// Checkout code from Git repository
checkout([
$class: 'GitSCM',
branches: [[name: '*/new-dev']],
userRemoteConfigs: [[
url: GIT_REPO,
credentialsId: GIT_CREDENTIALS_ID
]]
])
}
}
}
stage('Prepare') {
steps {
// You can run sh/bat/PowerShell command in the script, based on which environment the Jenkin deploied
script {
// Install npm dependencies
sh "npm install"
}
}
}
stage('Build') {
steps {
script {
// Build the project
sh """
npm run build:green
"""
}
}
}
stage('Set Version') {
steps {
script {
// Set the version based on tag or current date and time
env.VERSION = env.tag ?: sh(script: "date +%Y%m%d%H%M%S", returnStdout: true).trim()
echo "Version set to: ${env.VERSION}"
}
}
}
stage('Build Docker Image') {
steps {
script {
// Build Docker image
sh """
docker build -t ${DOCKER_REGISTRY}/${JOB_NAME}:${VERSION} .
"""
}
}
}
stage('Push Docker Image') {
steps {
script {
// Push Docker image to registry
sh """
docker push ${DOCKER_REGISTRY}/${JOB_NAME}:${VERSION}
"""
}
}
}
stage('Deploy') {
steps {
script {
// Deploy the application using Kubernetes
sh """
cd ${APP_DIR}
sed -i "s|image:.*|image: ${DOCKER_REGISTRY}/${JOB_NAME}:${VERSION}|g" deployment.yaml
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
"""
}
}
}
}
post {
success {
script {
// Notify on successful build
notifyTelegram()
}
}
failure {
// Log failure message
echo "Pipeline failed. Please check the logs."
}
}
}