-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to use cd in ci #940
Comments
The initial suspicion is that the previous steps all use container('maven'), and the last step cd uses container('base'), but if I replace the cd with maven, the build prompts that the ks command does not exist. |
那是因为你默认使用的是 maven ,你可以在最后一个步骤使用 base
不过,我已经测试过了,很有问题啊,我的仓库是在 gitee 上的,直接报错!!! |
最后一个如果用mave会报没有ks命令的错误。 |
就是因为默认的 Maven 镜像中没有安装 ks 命令行工具,所以会报错,你可以基于官方的镜像,将 ks 命令行工具安装进去,然后再在 Jenkins 中将官方的 maven 镜像替换,就可以的!! 不过,就算你做到这一步,依然会报错;别问,我试过很多方法,甚至手动 run 一个 Pod 去运行,但是发现 ks 工具好像有问题,就是不能拉取 gitee 上的私有仓库,太奇怪了!!,只能选择我上面提供的方法。 |
stage('持续部署') {
agent none
steps {
withEnv(["BUILD_STAGE_NAME=阶段的名称"]) {
script {
BUILD_STAGE_NAME = "持续部署"
}
}
container('maven') {
// 清空当前目录
sh '''
pwd && ls -lah
rm -rf *
pwd && ls -lah
'''
// 下载维护 kustomize 配置仓库的源码,因为我的项目源码和配置清单是分开在两个仓库中的
git(credentialsId: 'gitee-id', url: 'xxx', branch: 'master', changelog: true, poll: false)
// 更新配置
withCredentials([usernamePassword(credentialsId: "gitee-id",
usernameVariable: "GIT_USERNAME",
passwordVariable: "GIT_PASSWORD")]) {
sh 'git config --local credential.helper "!p() { echo username=\\$GIT_USERNAME; echo password=\\$GIT_PASSWORD; }; p"'
sh 'git config --global user.name "${GIT_USERNAME}"'
sh 'git config --global user.email "${GIT_USERNAME}"'
sh '''
pwd && ls -lah && cd xxx/xxxxxxxx && ls -lah
sed -i "s#registry-vpc.*#$REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:v$BUILD_NUMBER#g" deployment.yaml
git add -A && git commit -m "update tag: v$BUILD_NUMBER" && git push -u origin master
'''
}
}
}
} |
有点懵,我现在加了个jenkins agent
使用label maven-base后,最后一步cd:
还是报错
|
没有文档,感觉是个实验功能 |
额,感觉你还是没有理解,在 pipeline 中可以在每个 stage 中使用各自的 agent 的,无需重新配置: pipeline {
agent { // 这边配置的是全局,每个步骤 stages 都可以使用
node {
label 'mavenjdk11'
}
}
options { // 可选项
timeout(time: 5, unit: 'HOURS')
}
environment { // 环境变量
DOCKER_CREDENTIAL_ID = 'aliyun-docker-id'
REGISTRY = 'XXX'
DOCKERHUB_NAMESPACE = 'XXX'
APP_NAME = 'XXXX'
}
stages {
stage('拉取代码') {
agent none
steps {
withEnv(["BUILD_STAGE_NAME=阶段的名称"]) {
script {
BUILD_STAGE_NAME = "拉取代码"
}
}
git(credentialsId: 'gitee-id', url: 'XXXX', branch: 'master', changelog: true, poll: false)
}
}
stage('项目编译') {
agent none
steps {
withEnv(["BUILD_STAGE_NAME=阶段的名称"]) {
script {
BUILD_STAGE_NAME = "项目编译"
}
}
container('maven') {
sh 'mvn clean package -Dmaven.test.skip=true'
}
}
}
stage('构建镜像') {
agent none
steps {
withEnv(["BUILD_STAGE_NAME=阶段的名称"]) {
script {
BUILD_STAGE_NAME = "构建镜像"
}
}
container('maven') {
sh 'docker build --no-cache --force-rm -t $APP_NAME:latest -f Dockerfile .'
}
}
}
stage('推送镜像') {
agent none
steps {
withEnv(["BUILD_STAGE_NAME=阶段的名称"]) {
script {
BUILD_STAGE_NAME = "推送镜像"
}
}
container('maven') {
withCredentials([usernamePassword(credentialsId: 'aliyun-docker-id', usernameVariable: 'DOCKER_USER_VAR', passwordVariable: 'DOCKER_PASSWORD_VAR')]) {
sh 'echo "$DOCKER_PASSWORD_VAR" | docker login $REGISTRY -u "$DOCKER_USER_VAR" --password-stdin'
sh 'docker tag $APP_NAME:latest $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:v$BUILD_NUMBER'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:v$BUILD_NUMBER'
}
}
}
}
stage('持续部署') {
agent { // 这边配置的局部,只能 持续部署 这个 stage 使用
node {
label 'base'
}
}
steps {
container('base') {
withCredentials([usernamePassword(credentialsId : 'coding-id' ,passwordVariable : 'PASS' ,usernameVariable : 'USER')]) {
sh '''ks app update --app-name restaurant-cd \\
--app-namespace dong42xjfqc \\
--name core.harbor.domain/rest/restaurant:latest \\
--newName core.harbor.domain/rest/restaurant:latest \\
--git-password $PASS --git-username=$USER \\
--git-target-branch master'''
}
}
}
stage('清空镜像') {
agent none
steps {
withEnv(["BUILD_STAGE_NAME=阶段的名称"]) {
script {
BUILD_STAGE_NAME = "清空镜像"
}
}
container('maven') {
sh 'docker system prune -af'
}
}
}
}
post { // 后置动作
always {
echo '总是执行'
sh 'pwd && ls -lah'
}
success {
echo '后置执行 ---> 成功后执行...'
mail(to: '', cc: '', subject: "${APP_NAME}的构建报告", body: '流水线构建成功^_^')
}
failure {
echo '后置执行 ---> 失败后执行...'
mail(to: '', cc: '', subject: "${APP_NAME}的构建报告", body: "《${BUILD_STAGE_NAME}阶段》构建失败o(╥﹏╥)o")
}
aborted {
echo '后置执行 ---> 取消后执行...'
mail(to: '', cc: '', subject: "${APP_NAME}的构建报告", body: "${APP_NAME}流水线被手动取消了o(╥﹏╥)o")
}
}
} |
What is version of KubeSphere DevOps has the issue?
v3.3.2
How did you install the Kubernetes? Or what is the Kubernetes distribution?
k8s v1.22.12
What happened?
Now cd can be used in ci, but when I use it, it is not successful, what do I need to do
Relevant log output
Additional information
The text was updated successfully, but these errors were encountered: