目 录CONTENT

文章目录

Jenkins模拟GitOps系列04-Jenkins与harbor集成

cplinux98
2022-04-30 / 0 评论 / 0 点赞 / 58 阅读 / 261 字 / 正在检测是否收录...

00:文章简介

介绍基于Jenkins的GITOPS中的 Jenkins与harbor集成。

在harbor中创建一个制品库和账户

账户:ciadmin
仓库:myapp

01:添加Jenkins凭据

image-20220405013537451

02:增加推送镜像

pipeline {
    environment {
        appName = "myapp/spring-boot-helloworld"
        appVersion = "v0.9.2"
        registry = "https://harbor.linux98.com/"
        registryCredential = "harbor-myapp-ci-admin"
        dockerImage = ""
    }
    agent {
        kubernetes {
            inheritFrom 'maven-and-docker'
        }
    }
    stages {
        stage('Source') {
            steps {
                git branch: 'develop', url: 'http://gitlab.linux98.com:8080/root/test.git'
            }
        }
        stage('Build') {
            steps {
                container('maven'){
                    sh 'mvn clean test package'
                }
            }
        }
        stage('Build app image'){
            steps {
                container('docker'){
                    script {
                        dockerImage = docker.build appName + ":" + appVersion
                    }
                }
            }
        }
        stage('Push app image') {
            steps {
                container('docker'){
                    script{
                        docker.withRegistry( registry, registryCredential ){
                            dockerImage.push()
                        }
                    }
                }
            }
        }
    }
}

结果

image-20220405124818669

03:将Jenkinsfile归档到gitlab仓库

image-20220405132922996

将pipeline保存到develop分支中的Jenkinsfile文件中,并提交到gitlab

vim Jenkinsfile
git add .
git commit -m "Add Jenkinsfile"
git push origin develop

测试结果和上面一样,只是将Pipeline与Jenkins分离了,这样更方便开发人员在提交程序时,直接修改Jenkinsfile。

0

评论区