如何快速搭建jenkins CI/CD集成平台


avatar
admin 2025-06-11 89
# Jenkins 运行依赖
sudo apt install -y openjdk-17-jdk

# 添加 Jenkins 仓库密钥
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null

# 添加 Jenkins 仓库
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

# 安装 Jenkins
sudo apt update
sudo apt install -y jenkins

sudo systemctl enable jenkins    # 设置开机启动
sudo systemctl start jenkins     # 启动 Jenkins
sudo systemctl status jenkins    # 查看状态

访问地址

http://你的服务器IP地址:8080

管理员密码

cat /var/lib/jenkins/secrets/initialAdminPassword

pipeline方式使用SSH拉取代码脚本(✅推荐)

pipeline {
    agent any

    environment {
        DEPLOY_PATH = '/www/wwwroot/xxx'
    }

    stages {
        stage('拉取最新代码') {
            steps {
                sshagent(['xxxxxxx-xxxxx-xxxxx-xxxxxxxx-xxxxxx']) {
                    dir("${DEPLOY_PATH}") {
                        sh '''
                            git pull [email protected]:xxx/xxxx.git main
                        '''
                    }
                }
            }
        }
    }
}

pipeline方式使用HTTP拉取代码脚本

pipeline {
    agent any

    environment {
        DEPLOY_PATH = '/www/wwwroot/xxx'
    }

    stages {
        stage('拉取最新代码') {
            steps {
                dir("${DEPLOY_PATH}") {
                    withCredentials([usernamePassword(credentialsId: 'xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
                        sh '''
                            git pull http://${GIT_USER}:${GIT_PASS}@gitlab.xxx.cn/xxx/xxx.git main
                        '''
                    }
                }
            }
        }
    }
}

注意:

使用ssh方式需要安装插件SSH Agent Plugin
xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx为设置好的唯一标识,路径:Manage Jenkins->Credentials

暂无评论

发表评论

相关阅读