操作系统信息
虚拟机,Centos7.6,4C/16G
Kubernetes版本信息
Client Version: version.Info{Major:“1”, Minor:“22”, GitVersion:“v1.22.1”, GitCommit:“632ed300f2c34f6d6d15ca4cef3d3c7073412212”, GitTreeState:“clean”, BuildDate:“2021-08-19T15:45:37Z”, GoVersion:“go1.16.7”, Compiler:“gc”, Platform:“linux/amd64”}
Server Version: version.Info{Major:“1”, Minor:“22”, GitVersion:“v1.22.12”, GitCommit:“b058e1760c79f46a834ba59bd7a3486ecf28237d”, GitTreeState:“clean”, BuildDate:“2022-07-13T14:53:39Z”, GoVersion:“go1.16.15”, Compiler:“gc”, Platform:“linux/amd64”}
容器运行时
KubeSphere版本信息
kubesphere v3.4.1,使用 kk 安装。
问题是什么
尝试使用 gitops 功能,将 kustomization.yaml 文件与工程源码分开存储在不同的 git 仓库中。在 CI 阶段, git clone 工程源码,编译,打包,制作 docker 镜像,上传镜像,然后修改 gitops 仓库中 docker 镜像的版本号;最后衔接上 argocd 的持续部署。在 git 拉取 gitops 仓库的这一步骤中出现了配置问题,请求帮助!
jenkins 配置文件如下:
pipeline {
agent {
node {
label 'mavenjdk11'
}
}
stages {
stage('Clone repository') {
agent none
steps {
git(url: 'http://gitlab.xxxx.io/zhang/my-app.git', credentialsId: 'gitlab', branch: 'master', changelog: true, poll: false)
}
}
stage('Run compile') {
agent none
steps {
container('maven') {
sh 'mvn compile -X'
}
}
}
stage('Run build') {
steps {
container('maven') {
sh 'mvn package'
}
}
}
stage('docker login') {
steps {
container('maven') {
sh '''docker login -u 'robot$test' -p 'bQfSgiBo92AlQ2xqrMKgboHz3I19fMrc' $REGISTRY'''
}
}
}
stage('Generate dockerfile') {
steps {
script {
def dockerfilePath = 'Dockerfile'
def dockerfileContent = """
# Use an official Java runtime as a parent image
FROM openjdk:11-jdk-slim
# Set the working directory in the container
WORKDIR /app
ENV JAVA_OPTS = ""
# Copy the current codebase into the container at /app
COPY target/*.jar app.jar
# Run the JAR file on container startup
ENTRYPOINT ["sh", "-c", "java \${JAVA_OPTS:-} -jar /app/app.jar"]
"""
// Write the Dockerfile content to disk
writeFile file: dockerfilePath, text: dockerfileContent
}
}
}
stage('build & push') {
agent none
steps {
container('maven') {
sh 'pwd'
script {
// 获取当前时间戳
env.timestamp = sh(script: 'date +%Y%m%d%H%M%S', returnStdout: true).trim()
// 构建 Docker 镜像,并使用时间戳作为镜像标签
def imageName = "${REGISTRY}/${HARBOR_NAMESPACE}/${APP_NAME}:${timestamp}"
sh "docker build -t ${imageName} ."
// 推送 Docker 镜像
sh "docker push ${imageName}"
}
}
}
}
stage('update docker tag') {
steps {
sh "mkdir -p argocd-demo && cd argocd-demo"
git(url: 'http://gitlab.xxxx.io/zhang/argocd-demo.git', credentialsId: 'gitlab', branch: 'master', changelog: true, poll: false)
sh """
pwd
sed -i "s/{tag}/${timestamp}/g" apps/my-app/deployment.yaml
git add -A
git commit -m "update tag: ${timestamp}"
git push
"""
}
}
}
environment {
REGISTRY = '172.16.101.96:8082'
HARBOR_NAMESPACE = 'ks-devops-harbor'
APP_NAME = 'my-app'
HARBOR_CREDENTIAL_PSW = credentials('harbor')
}
}
请问 update docker tag 阶段的脚本,应该如何编写?