Transmode / gradle-docker

A Gradle plugin to build Docker images from the build script.
Apache License 2.0
647 stars 142 forks source link

How to update build jars to a gradle task & external Dockerfile dynamically #155

Closed devopsadmin-james closed 4 years ago

devopsadmin-james commented 4 years ago

I have used this "https://jgitver.github.io" plugin to build jar files based on git tags. With its usage gradle build jars are built based on git tags and git commits history(after new tag is created something like say git tag is v2.1.0 then build jar would be test-2.1.0-0.jar on master branch & test-2.1.0-0-dev.jar on a dev branch and after few commits say two more commits that built jar would be test-2.1.0-2.jar ).

So i have a below gradle task which will build docker image using that jar file with the help of your "Transmode/gradle-docker" plugin.

task buildDocker(type: Docker, dependsOn: build) { push = false tag = 'test/java' tagVersion = getDate() dockerfile = file('src/main/docker/Dockerfile') doFirst { copy {

// from 'build/libs/"${jar}"' from 'build/libs/Test-2.1.0-0.jar' into 'build/docker' } } }

So i have to update this jar file into this gradle task based on the current built jars dynamically and also into one of external Dockerfile (which is below) dynamically

FROM openjdk:8-jre

VOLUME /tmp

ADD Test-2.1.0-0.jar java.jar

RUN bash -c 'touch /java.jar'

EXPOSE 8080

How should we proceed to achieve this scenario.

devopsadmin-james commented 4 years ago

Created a shell-script task of gradle, somthing like this

task Test() { doLast { exec { executable "./test.sh" } } }

Contents inside test.sh like this

!/bin/bash

jar=$(find ./build/libs/ -type f -exec grep -l 'Web' {} \; | cut -d/ -f4) echo " " echo "$jar" sed -i "s|ADD\ .*|ADD\ $jar\ java.jar|g" ./src/main/docker/Dockerfile

sed -i "s|build/libs/.*|build/libs/$jar\'|g" ./build.gradle (tried this option got success but had to run job twice.)

So just copied everything from 'build/libs' into 'buidl/docker' at gradle task run time to get desired result.

task buildDocker(type: Docker, dependsOn: build) { push = false tag = 'test/java' tagVersion = getDate() dockerfile = file('src/main/docker/Dockerfile') doFirst { copy {

// from 'build/libs/"${jar}"' //from 'build/libs/Test-2.1.0-0.jar' from 'build/libs/'' into 'build/docker' } } }