Deploy 阶段改为 docker compose --env-file /etc/juiceworkstation/juice.env,敏感变量不进仓库、也不被 cleanWs 清空;README 补充该 env 文件的创建与权限设置;新增根目录 .gitignore 忽略 .env。
70 lines
1.9 KiB
Groovy
70 lines
1.9 KiB
Groovy
pipeline {
|
|
agent any
|
|
|
|
options {
|
|
timestamps()
|
|
disableConcurrentBuilds()
|
|
|
|
buildDiscarder(
|
|
logRotator(
|
|
numToKeepStr: '10',
|
|
artifactNumToKeepStr: '5'
|
|
)
|
|
)
|
|
}
|
|
|
|
stages {
|
|
|
|
stage('Checkout') {
|
|
steps {
|
|
cleanWs()
|
|
checkout scm
|
|
}
|
|
}
|
|
|
|
stage('Build Images') {
|
|
steps {
|
|
script {
|
|
env.BUILD_TIME = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
|
env.COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
|
|
}
|
|
sh 'docker compose build backend frontend'
|
|
}
|
|
}
|
|
|
|
stage('Deploy') {
|
|
steps {
|
|
script {
|
|
// 读取宿主机 docker.sock 的组 GID,供后端容器以附加组方式访问 socket。
|
|
env.DOCKER_GID = sh(
|
|
script: "stat -c '%g' /var/run/docker.sock 2>/dev/null || echo 999",
|
|
returnStdout: true
|
|
).trim()
|
|
}
|
|
// 管理员密码等敏感变量放在工作区外的 .env 文件里:
|
|
// 既不会被 cleanWs 清空,也不会进仓库。需在宿主机预先创建该文件
|
|
// (路径 /etc/juiceworkstation/juice.env,属主 root:jenkins、权限 640)。
|
|
sh '''
|
|
docker compose --env-file /etc/juiceworkstation/juice.env up -d
|
|
docker compose --env-file /etc/juiceworkstation/juice.env ps
|
|
'''
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
post {
|
|
success {
|
|
echo 'Build and deploy succeeded'
|
|
}
|
|
|
|
failure {
|
|
echo 'Build or deploy failed'
|
|
}
|
|
|
|
always {
|
|
echo 'Pipeline finished'
|
|
}
|
|
}
|
|
}
|