Gradle 发布 docker

buildscript {
    repositories {
        gradlePluginPortal()
        jcenter()
    }
    dependencies {
        classpath 'com.bmuschko:gradle-docker-plugin:6.0.0'
    }
}
repositories {
    jcenter()
}

ext {
    // 设置 DEBUG 容器对外端口
    DEBUG_PORT = "10081"
    DEBUG_NAME = "debug"
}

def releaseTime() {
    return new Date().format("HHmmss", TimeZone.getTimeZone("GMT+08:00"))
}

def buildImage() {
    return "bs-devops:5000/${project.group}.${project.name}:${project.version}.build-${releaseTime()}"
}

def image() {
    return "bysun:5000/${project.group}.${project.name}:${project.version}"
}

def debugImage() {
    return "bs-devops:5000/${project.group}.${project.name}:${DEBUG_NAME}"
}

def debugContainer() {
    return "${DEBUG_NAME}.${project.group}.${project.name}"
}

// use fully qualified class name
apply plugin: com.bmuschko.gradle.docker.DockerRemoteApiPlugin
 import task classes
import com.bmuschko.gradle.docker.tasks.image.*
import com.bmuschko.gradle.docker.tasks.container.*

docker {
    url = 'tcp://bs-devops:2375'
}

task _createDockerfile(type: Dockerfile) {
    group 'docker'
    description 'Generate docker file for the application'
    //destFile = project.file('Dockerfile')

    from('bs-devops:5000/alpine-jre8:3.8-201')
    addFile("libs/${project.name}-${project.version}.jar", "/app.jar")
    exposePort(80)
    defaultCommand('java', '-jar', "/app.jar")
}

task _createImage(type: DockerBuildImage) {
    group 'docker'
    description 'Executes build, generates a docker file and builds image from it'
    dependsOn(build, _createDockerfile)

    dockerFile = project.file("${project.buildDir}/docker/Dockerfile")
    inputDir = project.buildDir
    images.add(buildImage())
    images.add(image())
}

task pushDevImage(type: DockerPushImage) {
    group 'docker'
    dependsOn _createImage
    images = [buildImage().toString()]
}

task pushPrdImage(type: DockerPushImage) {
    group 'docker'
    images = [image().toString()]
}

task print() {
    group 'docker'
    doLast {
        println buildImage()
        println image()
    }
}


task printDebug() {
    group 'docker-debug'
    doLast {
        println debugImage()
    }
}

task _createDebugDockerfile(type: Dockerfile) {
    group 'docker-debug'
    description 'Generate docker file for the application'
    //destFile = project.file('Dockerfile')

    from('bs-devops:5000/alpine-jre8:3.8-201')
    addFile("libs/${project.name}-${project.version}.jar", "/app.jar")
    exposePort(80)
    defaultCommand('java', '-jar', '-Dspring.profiles.active=debug', "/app.jar")
}

task _createDebugImage(type: DockerBuildImage) {
    group 'docker-debug'
    description 'Executes build, generates a docker file and builds image from it'
    dependsOn(build, _createDebugDockerfile)
    dockerFile = project.file("${project.buildDir}/docker/Dockerfile")
    inputDir = project.buildDir
    images.add(debugImage())
}

task pushDebugImage(type: DockerPushImage) {
    group 'docker-debug'
    dependsOn _createDebugImage

    doLast {
        images = ["bs-devops:5000/${project.group}.${project.name}:${DEBUG_NAME}".toString()]
    }
}

task _createDebugContainer(type: DockerCreateContainer) {
    group 'docker-debug'
    dependsOn pushDebugImage

    containerName = debugContainer()
    targetImageId debugImage()

    hostConfig.portBindings = ["${DEBUG_PORT}:80"]
    // 因dockerJava代码重构,类结构调整导致无法正常运行,需要等 bmuschko/gradle-docker-plugin 升级修复此问题
//    hostConfig.publishAll = true
    hostConfig.autoRemove = true
    exposePorts('tcp', [80])    // << NECESSARY, BUT NOT DOCUMENTED!
}

task startDebugContainer(type: DockerStartContainer) {
    group 'docker-debug'
    dependsOn(_createDebugContainer)
    targetContainerId _createDebugContainer.getContainerId()
}

task stopDebugContainer(type: DockerStopContainer) {
    group 'docker-debug'
    targetContainerId debugContainer()
}

dependencies {
    testCompile 'com.bmuschko:gradle-docker-plugin:5.2.0'
}

在这里插入图片描述

  • docker._createDockerfile 生产dockerfile文件
  • docker._createImage 生成镜像,并打标签(tag)
  • docker.print 输出编译版本镜像和生成环境镜像
  • docker.pushDevImage 推送开发环境镜像
  • docker.pushPrdImage 推送生产环境镜像

调试环境

  • docker-debug._createDebugContainer 创建调试环境容器
  • docker-debug._createDockerfile 生产dockerfile文件
  • docker-debug._createDebugImage 生成debug镜像,并打标签(tag)
  • docker-debug.printDebug 输出调试版本镜像
  • docker-debug.pushDebugImage 推送调试环境镜像
  • docker-debug.startDebugContainer 启动调试环境容器
  • docker-debug.stopDebugContainer 停止调试环境容器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值