1.流水线创建
2.创建秘钥
2.1通过http账号密码方式
2.2通过ssh方式
3.根据脚本构建应用
def label = "jenkins-slave"
podTemplate(label: label, cloud: 'kubernetes',
)
{
node(label) {
stage('pull code') {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '6dabf361-4478-47c3-aba9-dc6e3d15795f', url: 'https://codeup.aliyun.com/61233c140fc7bf0dbe1e642d/docker-demo.git']]])
}
stage('build') {
echo "开始进行代码编译并推送到镜像仓库"
sh '''
echo ${IMAGE_VERSION}-$BUILD_NUMBER-`git log -n1 --format=format:%h` > image_version.txt
cat image_version.txt
echo "测试 Kubernetes 动态生成 jenkins slave"
docker login -u admin -p harbor 192.168.56.11:8084
mvn clean package -Dmaven.test.skip=true
cp /home/jenkins/agent/workspace/pipelineDemo/target/demo.jar ./
docker build -t 192.168.56.11:8084/demo/${IMAGE_NAME}:`cat image_version.txt` .
docker push 192.168.56.11:8084/demo/${IMAGE_NAME}:`cat image_version.txt`
'''
echo "镜像编译结束"
}
stage('packageChart') {
echo "准备开始打chart包,并上传"
sh '''
helm repo add --ca-file /etc/docker/certs.d/ca.crt --username admin --password harbor myrepo https://192.168.56.11/chartrepo/demo
cp image_version.txt k8s/helm/
cd k8s/helm
sed -i "s/{{IMAGE_NAME}}/${IMAGE_NAME}/g" values.yaml
sed -i "s/{{IMAGE_VERSION}}/`cat image_version.txt`/g" values.yaml
cat values.yaml
sed -i "s/{{JENKINS_NUM}}/$BUILD_NUMBER/g" Chart.yaml
sed -i "s/{{CHART_NAME}}/${CHART_NAME}/g" Chart.yaml
sed -i "s/{{CHART_VERSION}}/${CHART_VERSION}/g" Chart.yaml
cat Chart.yaml
helm package .
echo ${CHART_NAME}-${CHART_VERSION}.$BUILD_NUMBER.tgz > tgz.txt
cat tgz.txt
curl --request POST --form chart=@`cat tgz.txt` --user admin:harbor https://192.168.56.11/api/chartrepo/demo/charts --cacert /etc/docker/certs.d/ca.crt
'''
echo "打包结束"
}
stage('deploy') {
echo "准备开始部署"
sh '''
helm repo add --ca-file /etc/docker/certs.d/ca.crt --username admin --password harbor myrepo https://192.168.56.11/chartrepo/demo
helm repo update
cd k8s/helmValue
helm upgrade myapp myrepo/${CHART_NAME} -n default --version ${CHART_VERSION}.$BUILD_NUMBER -f dev.yaml
'''
}
}
}
3.1代码拉取介绍
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '6dabf361-4478-47c3-aba9-dc6e3d15795f', url: 'https://codeup.aliyun.com/61233c140fc7bf0dbe1e642d/docker-demo.git']]])
3.2自定义参数使用
使用方法为 ${CHART_NAME}