Jenkins是开源CI&CD软件领导者,提供超过1000个插件来支持构建、部署、自动化,满足任何项目的需要。
官网: https://www.jenkins.io/zh/
Debian12安装Jenkins
官方指导: https://pkg.jenkins.io/debian-stable/
主要步骤:
# 添加密钥
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
# 添加apt库列表
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
# 更新本地包索引
sudo apt-get update
# 安装jre
sudo apt-get install fontconfig openjdk-17-jre
# 安装jenkins
sudo apt-get install jenkins
# 查看jenkins状态
sudo systemctl status jenkins
# 访问jenkins页面 http://localhost:8080
# 获取解锁密钥
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
# 根据页面提示安装插件,配置管理员账号
使用Jenkins创建流水线
在Github上创建一个python flask项目,并在tests目录下创建单元测试。当推送代码到仓库时,通过GitHub webhook触发器通知Jenkins执行流水线任务。
1.安装插件
在路径Jenkins Dashboard -> Manage Jenkins -> Manage Plugins -> Available
搜索
- GitHub Integration
- Git
- Pipeline
其中Git
,Pipeline
默认已安装,可在Installed plugins
中查看
2.创建 Jenkins Pipeline 项目
创建 Pipeline 项目
- 在 Jenkins 主界面,点击"New Item"
- 输入项目名称,例如"Python_Project"
- 选择"Pipeline"
- 点击"OK"
配置 Pipeline 项目
在"Pipeline"部分,选择"Pipeline script"
pipeline {
agent any
environment {
FLASK_APP = 'src/app.py'
VENV_DIR = 'venv'
}
stages {
stage('Clone Repository') {
steps {
git branch: 'main', url: 'https://github.com/<your_name>/<your_project>.git'
}
}
stage('Setup Virtual Environment') {
steps {
sh 'python3 -m venv ${VENV_DIR}'
sh '. ${VENV_DIR}/bin/activate'
}
}
stage('Install Dependencies') {
steps {
sh '. ${VENV_DIR}/bin/activate && pip install --default-timeout=100 -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt'
}
}
stage('Run Unit Tests') {
steps {
sh '. ${VENV_DIR}/bin/activate && pytest tests/'
}
}
}
post {
always {
echo 'Pipeline completed.'
}
success {
echo 'Pipeline succeeded.'
}
failure {
echo 'Pipeline failed.'
}
}
}
3.启用 GitHub webhook 触发器
进入 Jenkins 项目配置,在"Build Triggers"部分,勾选"GitHub hook trigger for GITScm polling"
4.配置 GitHub Webhook
- 进入 GitHub 仓库
- 点击"Settings"
- 选择"Webhooks"
- 点击"Add webhook"
- 在"Payload URL"中输入 Jenkins 服务器地址和 GitHub Webhook 的 URL
http://your-jenkins-server/github-webhook/
- 设置"Content type"为"application/json"
- 在"Which events would you like to trigger this webhook?"部分,选择"Just the push event"
- 点击"Add webhook"
配置完成后,查看"Recent Deliveries"是否成功。
5.测试流水线
执行任务,在Console Output查看日志
标签:Pipeline,GitHub,使用,webhook,jenkins,Jenkins,sudo,安装 From: https://www.cnblogs.com/rustling/p/18315009