Open yhan219 opened 1 year ago
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
stage('持续部署') {
agent {
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'''
}
}
}
}
不过,我已经测试过了,很有问题啊,我的仓库是在 gitee 上的,直接报错!!!
最后一个如果用mave会报没有ks命令的错误。
最后一个如果用mave会报没有ks命令的错误。
就是因为默认的 Maven 镜像中没有安装 ks 命令行工具,所以会报错,你可以基于官方的镜像,将 ks 命令行工具安装进去,然后再在 Jenkins 中将官方的 maven 镜像替换,就可以的!!
不过,就算你做到这一步,依然会报错;别问,我试过很多方法,甚至手动 run 一个 Pod 去运行,但是发现 ks 工具好像有问题,就是不能拉取 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
- name: "maven-base"
label: "maven-base"
inheritFrom: "maven"
containers:
- name: "maven"
image: "kubesphere/builder-maven:v3.2.1-jdk11"
- name: "base"
image: "kubesphere/builder-base:v3.2.2"
command: "cat"
args: ""
ttyEnabled: true
privileged: false
resourceRequestCpu: "100m"
resourceLimitCpu: "4000m"
resourceRequestMemory: "100Mi"
resourceLimitMemory: "8192Mi"
使用label maven-base后,最后一步cd:
stage('cd') {
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'''
}
}
}
}
还是报错
+ ks app update --app-name restaurant-cd --app-namespace dong42xjfqc --name core.harbor.domain/rest/restaurant:latest --newName core.harbor.domain/rest/restaurant:SNAPSHOT--34 --git-password **** --git-username=yhan219@sina.com --git-target-branch master
Error: Missing kustomization file 'kustomization.yaml'.
Usage:
kustomize edit set image [flags]
Examples:
The command
set image postgres=eu.gcr.io/my-project/postgres:latest my-app=my-registry/my-app@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
will add
images:
- name: postgres
newName: eu.gcr.io/my-project/postgres
newTag: latest
- digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
name: my-app
newName: my-registry/my-app
to the kustomization file if it doesn't exist,
and overwrite the previous ones if the image name exists.
The command
set image node:8.15.0 mysql=mariadb alpine@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
will add
images:
- name: node
newTag: 8.15.0
- name: mysql
newName: mariadb
- digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
name: alpine
to the kustomization file if it doesn't exist,
and overwrite the previous ones if the image name exists.
The image tag can only contain alphanumeric, '.', '_' and '-'. Passing * (asterisk) either as the new name,
the new tag, or the digest will preserve the appropriate values from the kustomization file.
Flags:
-h, --help help for image
Global Flags:
--stack-trace print a stack-trace on error
Usage:
ks app update [flags]
Aliases:
update, up
Flags:
--app-name string The name of the application
--app-namespace string The namespace of the application
-d, --digest string Digest is the value used to replace the original image tag. If digest is present NewTag value is ignored
--git-password string The password of the git provider
--git-provider string The flag --mode=pr need the git provider, the mode will fallback to commit if the git provider is empty
--git-target-branch string The target branch name that you want to push
--git-username string The username of the git provider
-h, --help help for update
-n, --name string Name is a tag-less image name
--newName string NewName is the value used to replace the original name
-t, --newTag string NewTag is the value used to replace the original tag
--secret-name string The username of the git provider
--secret-namespace string The username of the git provider
Global Flags:
--context string Sets a context entry in kubeconfig
failed to push changes, error is: failed to push branch, error is: unable to checkout git branch: 397527233, error: a branch named "refs/heads/master" already exists
script returned exit code 1
没有文档,感觉是个实验功能
没有文档,感觉是个实验功能
额,感觉你还是没有理解,在 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