自学内容网 自学内容网

CI/CD(持续集成与持续交付)流水线

集成 Jenkins、GitLab Webhook、Nexus 和 RabbitMQ 可以形成一个全面的 CI/CD(持续集成与持续交付)流水线,结合消息队列可以创建事件驱动的工作流。以下是配置这四个工具以实现一个基本的 CI/CD 流程的详细步骤。

前置条件
Jenkins、GitLab、Nexus 和 RabbitMQ 实例已经安装并运行,各实例间网络通信正常。
Jenkins 有访问 GitLab 仓库、Nexus 仓库和 RabbitMQ 的权限。
一、配置 GitLab Webhook
Webhook 是 GitLab 推送代码变更后通知 Jenkins 的一种方式。

在 GitLab 中配置 Webhook
访问项目设置:

打开你的 GitLab 项目,点击左侧栏中的 Settings,然后选择 Webhooks。
添加 Webhook:

在 URL 字段中填写 Jenkins 的 Webhook URL,通常形式如下:

http://<jenkins-url>/project/<your-jenkins-job-name>

选择触发事件,例如 Push events、Merge Request events 等。
点击 Add webhook。

二、在 Jenkins 中配置 GitLab 和 Nexus
安装插件
打开 Jenkins 管理界面,选择 Manage Jenkins > Manage Plugins。
在 Available 标签页,搜索并安装以下插件:
GitLab Plugin
Git Plugin
Nexus Artifact Uploader Plugin
RabbitMQ Build Trigger Plugin
配置 GitLab 连接
打开 Jenkins 管理界面,选择 Manage Jenkins > Configure System。
在 GitLab 部分,添加 GitLab 服务器信息:
填写 GitLab 服务器 URL。
添加 GitLab API token,在 GitLab User Settings 中生成一个新的访问令牌(需要具备 api 权限)。
配置 Nexus 连接
打开 Jenkins 管理界面,选择 Manage Jenkins > Configure System。
滚动到 Nexus Artifact Uploader 部分,添加一个新的 Nexus 服务器配置,填写 URL、认证方式、用户名和密码。
配置 RabbitMQ 连接
打开 Jenkins 管理界面,选择 Manage Jenkins > Configure System。
在 RabbitMQ Build Triggers 部分,添加 RabbitMQ 服务器信息,包括主机名、端口、用户名和密码。可以选择一个交换机和队列名称。
三、创建 Jenkins Pipeline
通过 Jenkins Pipeline 配置整个 CI/CD 流程,包括从 GitLab 获取代码、构建、上传到 Nexus 和通过 RabbitMQ 发送消息。

Copy

pipeline {
agent any

environment {
    GIT_REPO_URL = 'https://gitlab.com/your-username/your-repo.git'
    NEXUS_URL = 'http://nexus.example.com'
    NEXUS_REPO = 'your-repo'
    NEXUS_GROUP = 'com.example'
    ARTIFACT_ID = 'your-artifact'
    VERSION = '1.0.0'
    RABBITMQ_HOST = 'rabbitmq.example.com'
    RABBITMQ_PORT = '5672'
    RABBITMQ_QUEUE = 'your-queue'
    RABBITMQ_USER = 'your-user'
    RABBITMQ_PASS = 'your-password'
}

stages {
    stage('Checkout') {
        steps {
            git credentialsId: 'your-credentials-id', url: "${GIT_REPO_URL}"
        }
    }

    stage('Build') {
        steps {
            sh 'mvn clean package'
        }
    }

    stage('Test') {
        steps {
            sh 'mvn test'
        }
    }

    stage('Upload to Nexus') {
        steps {
            nexusArtifactUploader(
                nexusVersion: 'nexus3',
                protocol: 'http',
                nexusUrl: "${NEXUS_URL}",
                groupId: "${NEXUS_GROUP}",
                version: "${VERSION}",
                repository: "${NEXUS_REPO}",
                credentialsId: 'your-nexus-credentials-id',
                artifacts: [
                    [artifactId: "${ARTIFACT_ID}",
                     classifier: '',
                     file: 'target/your-artifact.jar',
                     type: 'jar']
                ]
            )
        }
    }

    stage('Send Message to RabbitMQ') {
        steps {
            script {
                def messageJson = """
                {
                    "project": "${env.JOB_NAME}",
                    "build_number": "${env.BUILD_NUMBER}",
                    "status": "SUCCESS",
                    "artifactUrl": "${NEXUS_URL}/repository/${NEXUS_REPO}/${NEXUS_GROUP.replace('.', '/')}/${ARTIFACT_ID}/${VERSION}/${ARTIFACT_ID}-${VERSION}.jar"
                }
                """
                sh """
                curl -X POST -d '${messageJson}' \
                     -u ${RABBITMQ_USER}:${RABBITMQ_PASS} \
                     http://${RABBITMQ_HOST}:${RABBITMQ_PORT}/api/exchanges/%2f/amq.default/publish \
                     -H "content-type:application/json" \
                     -d '{
                           "properties": {},
                           "routing_key": "${RABBITMQ_QUEUE}",
                           "payload": "${messageJson}",
                           "payload_encoding": "string"
                      }'
                """
            }
        }
    }
}

post {
    success {
        echo 'Build, upload, and message sending completed successfully'
    }
    failure {
        echo 'Build, upload, or message sending failed'
    }
}

}
四、在 RabbitMQ 中监控和处理消息
为了处理从 Jenkins 发送到 RabbitMQ 的消息,需要在 RabbitMQ 的队列中创建一个消费者。

Python 示例消费者代码
你可以使用任何编程语言来编写 RabbitMQ 消息消费者,这里介绍一个用 Python 编写的示例消费者代码:

Copy

import pika
import json

def callback(ch, method, properties, body):
message = json.loads(body)
print(f"Received message: {message}")

connection = pika.BlockingConnection(pika.ConnectionParameters(‘rabbitmq.example.com’))
channel = connection.channel()

channel.queue_declare(queue=‘your-queue’)

channel.basic_consume(queue=‘your-queue’, on_message_callback=callback, auto_ack=True)

print(‘Waiting for messages. To exit press CTRL+C’)
channel.start_consuming()
总结
通过这些步骤,你可以配置一个集成了 Jenkins、GitLab Webhook、Nexus 和 RabbitMQ 的完整 CI/CD 流水线。这将使你的开发和部署流程更加自动化和高效。具体实现可能根据你的项目需求有所调整,你可以根据自己的需求定制和扩展这些配置。


原文地址:https://blog.csdn.net/weixin_42692164/article/details/143001889

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!