使用Jenkins MultiBranch实现Docker build镜像并push到DockerHub

使用Docker部署Jenkins,并通过MultiBranch功能自动对项目进行打包并docker build和push。

1. Docker部署Jenkins

拉取镜像

docker pull jenkinsci/jenkins:2.150.1

启动

docker run --name jenkins -d -p 8080:8080 -p 50000:50000 -v ~/workspace/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v /usr/local/bin/docker:/bin/docker -t jenkinsci/jenkins:2.150.1

其中:-v /var/run/docker.sock:/var/run/docker.sock -v /usr/local/bin/docker:/bin/docker作用是使用主机的docker命令。否则可能Jenkins创建docker镜像时会报docker not found错误。

修改容器中docker权限

# 进入Jenkins容器内
docker exec -it -u root 5c8 bash
# 修改docker权限
chmod 777 /var/run/docker.sock

如果不修改容器中docker权限,则Jenkins进行到docker build时会报dial unix /var/run/docker.sock: connect: permission denied错误。

Jenkins插件安装和创建multibranch job请自行完成。

2. 编写Dockerfile
FROM openjdk:8-jdk-alpine3.7
VOLUME /tmp

ARG JAR_FILE
ADD target/${JAR_FILE} target/app.jar

EXPOSE 8080

RUN touch target/app.jar
ENTRYPOINT ["java","-jar","target/app.jar"]
3. 编写Jenkinsfile
node {
    def app
    def mvnHome = tool 'maven'
    env.PATH = "${mvnHome}/bin:${env.PATH}"
    def IMAGE_NAME = "star936/rest-api-mybatis"

    stage('Package') {
        /* Let's make sure we have the repository cloned to our workspace */

        checkout scm
        sh 'mvn -v'
        sh 'mvn clean package -Dmaven.test.skip=true -Ddockerfile.skip=true -P dev'
    }

    stage('Build and Push image') {
        /* This builds the actual image; synonymous to
         * docker build on the command line */
        /* Finally, we'll push the image with two tags:
         * First, the incremental build number from Jenkins
         * Second, the 'latest' tag.
         * Pushing multiple tags is cheap, as all the layers are reused. */
        echo env.BRANCH_NAME
        echo env.WORKSPACE
        sh 'docker version'
        def JAR_FILE = IMAGE_NAME + '.jar'
        docker.withRegistry('https://registry.hub.docker.com', 'dockerhub') {
            app = docker.build("${env.IMAGE_NAME}:${BRANCH_NAME}", "--build-arg JAR_FILE=${JAR_FILE} .")
            app.push()
        }
    }
}

tool 'maven'中的maven是在Jenkins->系统管理->全局工具配置->Maven->Add Maven处填写的名字.

说明:DockerfileJenkinsfile中出现的JAR_FILE参数,与pom.xmlJAR_FILE相互应。

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>${dockerfile-maven-version}</version>
    <configuration>
        <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
        <repository>${docker.image.prefix}/${docker.repository.name}</repository>
        <tag>${project.version}</tag>
        <buildArgs>
            <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
    </configuration>
</plugin>
4. 配置GitHub Webhooks

想要实现git push后Jenkins自动构建变化的分支,则还需要配置GitHub Webhooks: 在项目的settings->Webhooks->Add webhook->Payload URL处填写http://{jenkins服务器IP}/git/notifyCommit?url={MultiBranch添加repository源时填写的URL}&delay=0sec
对于在本地搭建Jenkins的,推荐使用ngrok.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值