在 CentOS 下使用 Jenkins 创建任务(Job) 一般分为 安装 Jenkins → 登录 Web 界面 → 新建任务 几个步骤。下面按常见场景说明。
如果还没装,可先确认:
systemctl status jenkins
若未安装,常见安装方式(示例):
sudo yum install -y java-11-openjdk
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
sudo yum install -y jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
访问:
http://<服务器IP>:8080
http://IP:8080cat /var/lib/jenkins/secrets/initialAdminPassword
步骤:
my-centos-build
https://github.com/xxx/xxx.git
常见:
示例(每天凌晨 2 点):
H 2 * * *
在 CentOS 上执行 shell 命令:
选择:
Execute shell
示例:
#!/bin/bash
echo "当前系统:"
cat /etc/centos-release
echo "开始构建..."
mkdir -p /opt/build
cd /opt/build
make || exit 1
echo "构建完成"
Jenkins 默认用户是 jenkins:
sudo usermod -aG wheel jenkins
或修改目录权限:
sudo chown -R jenkins:jenkins /opt/build
在 Jenkins 的 shell 里:
which make
建议:
sudo yum install -y make git
在 shell 中:
source /etc/profile
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'make'
}
}
}
}
如果你愿意,可以告诉我:
我可以直接给你一份 可用的 Jenkins 任务配置示例。