Passing arguments to build.gradle - docker

I am building a springboot application using gradle.
The build eventually outputs a docker container and I want my build.gradle to support multiple environments.
I am using gradlew build docker to build and the docker section of my build.gradle is :
docker {
dependsOn build
name "app-production"
files bootJar.archivePath, 'application.yml'
buildArgs(['JAR_FILE': "${bootJar.archiveName}"])
}
I successfully passed arguments to the dockerfile but now I also want to pass arguments to the build.gradle file. Is it possible using gradlew build docker?
I need something like this : gradlew build docker --env=qa and then :
docker {
dependsOn build
name "app-${env}"
files bootJar.archivePath, 'application.yml'
buildArgs(['JAR_FILE': "${bootJar.archiveName}"])
}
NOTE: ${bootJar.archiveName} is a local variable in my build.gradle, not an environment variable
Any ideas?

You can use Project Properties to pass values to your build script from command line, as follows:
gradlew build docker -Penv=qa
Then in your build script you can access this property :
docker {
name "app-${env}"
// ....
}

Related

Module not found error in Jenkins while trying to run wdio from docker image

I am using webdriverio . My package.json is having all my dependency. I have created a docker image for my project throught Jenkins using a docker file. Now I am trying run the scripts from Jenkins. It is failing saying module not found. For example in config file I have used var json=require('cjson') . Same has been installed in docker image . But when I run through Jenkins it fails saying module cjson not found
Build container on your local and try to run it from there and check:
**1. Is your dependency installation script running?
See if there are any error for specific module that's failing**
Best

How to generate a docker image

I try to create a docker image based on my java gradle web application.
I add plugin and build in my build.gradle as shown below.
apply plugin: 'application'
apply plugin: 'docker'
buildscript {
repositories { mavenCentral() }
dependencies {
classpath 'se.transmode.gradle:gradle-docker:1.1'
}
}
I didn't specify the java version as in gradle's default sourceCompatibility is 1.7
After execute ./gradlew distDocker
build folder is generated
docker/application-b35a946bf5b.tgz
docker/Dockerfile
classes/
distributions/
libs/
resources/
scripts/
tmp/
Can i create docker images based on docker/application-b35a946bf5b.tgz and docker/Dockerfile
I am new to docker.
Any suggestion will be helpful.

How build a Dockerfile in a Subdirectory using a Jenkinsfile

I have a github repository with a declarative pipeline Jenkinsfile.
The workingdirectory on my node contains subdirectories.
The project is a simple empty linkx docker project created with visual studio 2017 and .net core 2.1. It executes on my windows 7 machine normally and has a hello world web page.
I am unable to build the Dockerfile on jenkins.
I can start the Dockerfile build using dir(...){}.
The failing step is always
COPY ["MyProject/MyProject.csproj", "MyProject/"]
This step requires the relative path to be in MySolution.
The file Workspace/MySolution/MyProject/MyProject.csproj exists
The error Message is that Workspace/MyProject/MyProject.csproj does not exist.
I searched exhaustively using google and stackoverflow. Among the things I tries are combinations of sh commands, dir syntax, options on docker build like -f. Some of them were straigth up failures and the best results I had ran into the COPY step issue.
One example of a failing step in the Jenkinsfile would be:
dir("MySolution/MyProject")
{
script
{
docker.build("MyProject", ".")
}
}
This fails with the COPY issue from above.
I have seen questions on so that seem to not quite apply here and which solutions did not transfer to this issue.
It turns out I ended up really close to the solution of my issue.
This fixed my sub folder problem:
dir("MySolution")
{
script
{
docker.build("MyProject", "-f ./MyProject/Dockerfile .")
}
}

Gradle: What is the proper way to specify the final spring-boot jar file?

My spring-boot application was originally built with maven, but I'm trying to gradle-ify it. Part of my build process was making a docker image with the dockerfile-maven plugin. The most accepted gradle equivalent seems to be the com.palantir.docker, so that's what I'm using (at least according to this tutorial from pivotal.)
My setup is pretty simple. My dockerfile takes a build argument, which is the final jar, and adds it to the image.
FROM openjdk:8-jdk-alpine
MAINTAINER github.com/paul-nelson-baker
EXPOSE 5005 8080 35729
ENTRYPOINT ["java", "-Xdebug", "-Xrunjdwp:server=y,transport=dt_socket,address=5005,suspend=n", "-jar", "/var/paulbaker/pauls-blog.jar"]
ARG JAR_FILE
ADD ${JAR_FILE} "/var/paulbaker/pauls-blog.jar"
The part that's getting me hungup is the gradle setup. The build arg that I pass appears correct:
docker {
name = "paulbaker.io/my-blog"
tags 'latest'
dockerfile file('src/main/docker/Dockerfile')
files "${project.name}-${version}.jar"
buildArgs([JAR_FILE: "${project.name}-${version}.jar"])
}
However whenever I attempt to build the image, I get the following error even though the actual java build succeeds:
$ gradle build docker
> Task :docker FAILED
ADD failed: stat /var/lib/docker/tmp/docker-builder952857277/pauls-blog-0.0.1-SNAPSHOT.jar: no such file or directory
I've tried a myriad of combinations to try to specify the path (relative to my project) of ./build/libs/pauls-blog-0.0.1-SNAPSHOT.jar, which I can see exists while the printed directory above does not.
There is clearly something I'm not understanding about gradle builds, can someone clear up how to get the final jar file?
The following error:
> Task :docker FAILED
ADD failed: stat /var/lib/docker/tmp/docker-builder952857277/pauls-blog-0.0.1-SNAPSHOT.jar: no such file or directory
happens when the Palantir Docker plugin cannot find the file specified using the docker.files attribute with respect to your current context directory.
In your question, the plugin was searching for the jar file in the root directory of the project (likely where your build.gradle is) and fails because the jar file is not generated there. When you specify jar.archivePath, you are specifying the full location of the path to the jar file which will now obviously be found.
I've solved the problem, but I don't know why this is different, so I'm leaving the question opened so someone has a chance to give a detailed explanation as to why this works.
The correct values should be referencing jar.archivePath.
So the final result looks like this:
docker {
name = "paulbaker.io/my-blog"
tags 'latest'
dockerfile file('src/main/docker/Dockerfile')
files jar.archivePath
buildArgs([JAR_FILE: "${jar.archiveName}"])
}
The arguments to files are added to a gradle CopySpec of the docker task.
So files jar.archivePath adds that path where your jar build artifact was created to this CopySpec.
When your Dockerfile is processed, the jar.archiveName is inserted into the ADD template and because your jar.archivePath is in the CopySpec of the docker build context, it finds the jar.archiveName in the CopySpec and copies the file to the destination of ADD.
This is my take on what I'm seeing in my own testing.

How to pass parameter in the Cucumber Runner file to run files from specific folder

I am using Serenity Cucumber framework using Gradle. I have integrated this with Jenkins. My requirement is that as a part of one Jenkins job I would like to run the feature files from one folder and as a part of next Jenkins job feature files located from other folder should get executed. Can you please suggest how I can pass the parameters to Cucumber Runner file during run time. For e.g.:
Below is my feature file
#RunWith(CucumberWithSerenity.class)
#CucumberOptions(features = "src/test/resources/Sanity/")
public class TestRunnerSerenity {
}
As a part of Sanity Build all my test cases from src/test/resources/Sanity/ folder should get executed. So how I can pass this folder path value to the Cucumber Runner class run time. So that I can maintain my builds without having multiple runner files and manual intervention.
Thanks in advance for your help.
There is some configuration need to do in Code,Gradle and Jenkins and Here is step by step
In Grade file, I will create systemProperty function.
task prodTest(type: Test)
prodTest{
systemProperty 'test.folder', testFolder
}
task "runProdTest" {
dependsOn "clean", "cleanTest", "prodTest"
}
In Constants Class,
Create static final String
public static final String TEST_FOLDER = system.getProperty("test.folder","FolderName")`
When you run locally, It will not failed because of external parameter is not available.
Go to runner file,
#RunWith(CucumberWithSerenity.class)
#CucumberOptions(features = Constants.TEST_FOLDER)
public class TestRunnerSerenity {
}
Now Jenkins Configuration
Go to Jenkins - Configuration
There should be Invoke Gradle Script Script. And You can add parameter under "switches". You will tell jenkis to u
-Ptest.folder="src/test/resources/Sanity/"
Tasks = runProdTest
You all set !!

Resources