GitHub Action Gradle buildBootImage - spring-boot

Spring boot 2.3 onwards comes with Gradle tasks buildBootImage which creates a container and by default pushes it to docker.io/library. How can this task be customized, in the context of Gradle running in GitHub action to push the container to GHCR, ECR, or GCR; ie, when GitHub action executes ./gradlew buildBootImage
Gradle's buildBootImage task comes with the below config parameters block, what's the best way to set these values within the build script and how do we pass these values from GitHub actions?
tasks.getByName<BootBuildImage>("bootBuildImage") {
imageName = "bestway to set image name"
isPublish = true
docker {
publishRegistry {
url = "best way to set container registry URL"
username = "best way to set user name for CR"
password = "best way to set password for CR"
}
}
}

Related

Jenkins Pipeline emailext: How to access build object in pre-send script

I'm using Jenkins ver. 2.150.1 and have some freestyle jobs and some pipeline jobs.
In both job types I am using the emailext plugin, with template and pre-send scripts.
It seems that the build variable, which is available in the freestyle projects, is null in the pipeline projects.
The pre-send script is the following (just an example, my script is more complex):
msg.setSubject(msg.getSubject() + " [" + build.getUrl() + "]")
There is no problem with the msg variable.
In the freestyle job, this script adds the build url to the mail subject.
In the pipeline job, the following is given in the job console:
java.lang.NullPointerException: Cannot invoke method getUrl() on null object
The invocation of emailext in the pipeline job is:
emailext body: '${SCRIPT, template="groovy-html.custom.pipeline.sandbox.template"}',
presendScript: '${SCRIPT, template="presend.sandbox.groovy"}',
subject: '$DEFAULT_SUBJECT',
to: 'user#domain.com'
I would rather find a general solution to this problem (i.e. Access the build variable in a pipeline pre-send script), but would also appreciate any workarounds to my current needs:
Access job name, job number, and workspace folder in a pipeline pre-send script.
I have finally found the answer -
Apparently for presend script in pipeline jobs, the build object does not exist, and instead the run object does. At the time I posted this question this was still undocumented!
Found the answer in this thread
Which got the author to update the description in the wiki:
run - the build this message belongs to (may be used with FreeStyle or Pipeline jobs)
build - the build this message belongs to (only use with FreeStyle jobs)
You can access the build in a script like this:
// findUrl.groovy
def call(script) {
println script.currentBuild.rawBuild.url
// or if you just need the build url
println script.env.BUILD_URL
}
and would call the script like this from the pipeline:
stage('Get build URL') {
steps {
findUrl this
}
}
The currentBuild gives you a RunWrapper object and the rawBuild a Run. Hope this helps.

Set host name as an environment variable in Heroku review app

I'm using the Review Apps feature integrated with Github on Heroku. In one of my apps, I set an environment variable called HOST_NAME . For example, if the site is http://www.purplebinder.com, then HOST_NAME would be set to www.purplebinder.com. It's used in a couple of places where we work with cookies and in our transactional emails.
When I open up a new pull request and spin up a review app, HOST_NAME should be something like purplebinder-pr-27.herokuapp.com.
Is there a way to set this value automatically? The Heroku documentation on review apps says an env var can inherit a value from the parent app or be hardcoded in app.json. Neither of those approaches work here, because the value needs to be different each time, and also different from the parent app.
Heroku also says an env var can be set "through a generator", but doesn't go into detail about what that is.
This question might be a duplicate of Setting ROOT_URL for Review Apps, but nobody answered that one. It's also similar to How to get Heroku app name from inside the app, but the answers there involved running a script after the app was created - here I'd like to set this value as part of the initial build.
From https://devcenter.heroku.com/articles/github-integration-review-apps#heroku_app_name-and-heroku_parent_app_name:
To help with scripting, two special config vars are available to
review apps. If you specify HEROKU_APP_NAME or HEROKU_PARENT_APP_NAME
as required or optional config vars in your app.json file, Heroku will
set those config vars to the new application name and the parent
application name respectively. They will then be available for use in
the postdeploy script so that you can do more advanced bootstrapping
and configuration.
Here is an example app.json file that uses
HEROKU_APP_NAME and HEROKU_PARENT_APP_NAME:
{
"name":"Advanced App",
"scripts": {
"postdeploy": "rake db:setup && bin/bootstrap"
},
"env": {
"HEROKU_APP_NAME": {
"required": true
},
"HEROKU_PARENT_APP_NAME": {
"required": true
}
}
}
If you add the heroku-buildpack-cli to your parent app, then it enables you to set environment variables from your post-deploy script. The command should look something like the following:
heroku config:set HOST_NAME=${HEROKU_APP_NAME}.herokuapp.com --app ${HEROKU_APP_NAME}
Here's an approach ignoring app.json for Rails installations:
in the relative config/<environment>.rb. I personally use production.rb and staging just references it.
if ENV.fetch("HEROKU_APP_NAME", "").include?("staging-pr-")
ENV["APPLICATION_HOST"] = ENV["HEROKU_APP_NAME"] + ".herokuapp.com"
ENV["ASSET_HOST"] = "https://" + ENV["APPLICATION_HOST"]
config.action_mailer.default_url_options = { host: ENV.fetch("APPLICATION_HOST") }
end
...
It's a bit misleading as the heroku environment variables will still have the old variables, but it works.
You can also create review environment for you application copying staging.rb or production.rb from config/environments. This would be useful.
After adding HEROKU_APP_NAME and HEROKU_PARENT_APP_NAME to your app.json, you can easily set;
config.action_mailer.default_url_options = { host: "#{ENV['HEROKU_APP_NAME']}.herokuapp.com" }
config.action_mailer.asset_host = "http://#{ENV['HEROKU_APP_NAME']}.herokuapp.com"
config.action_controller.asset_host = "#{ENV['HEROKU_APP_NAME']}.herokuapp.com"
config.action_cable.url = "wss://#{ENV['HEROKU_APP_NAME']}.herokuapp.com/cable"

Connect Travis CI to SonarQube through a HAProxy with Basic Auth using Gradle

I'm trying to setup SonarQube for a Gradle project. I get it to play locally, but when using Travis CI I, run into problems. Travis cannot reach the SonarQube server through the HAProxy.
SonarQube server is behind a HAProxy with Basic Access Authentication. I have tried to use the name and password both as sonar.login/sonar.password and as auth credentials in the URL, but with no success.
sonarqubeSonarQube server [https://sonar.<domain>.com] can not be reached
or
sonarqubeSonarQube server [https://<name>:<password>#sonar.<domain>.com] can not be reached
Basically my build.gradle file contains this:
plugins {
id "org.sonarqube" version "2.0.1"
}
sonarqube {
properties {
properties["sonar.host.url"] = getProperty('SONAR_URL')
properties["sonar.login"] = getProperty('SONAR_USERNAME')
properties["sonar.password"] = getProperty('SONAR_PASSWORD')
}
}
I had to remove the 'systemProp.' prefix to the properties in order to get it to work locally.
When running locally, I have a init.gradle file that contains this section, and it works nice.
allprojects {
ext.set('SONAR_URL', 'http://localhost:9000')
ext.set('SONAR_USERNAME', 'admin')
ext.set('SONAR_PASSWORD', 'admin')
...
}
On Travis CI, I try to add environment variables for the job:
ORG_GRADLE_PROJECT_SONAR_PASSWORD ••••••••••••••••
ORG_GRADLE_PROJECT_SONAR_USERNAME ••••••••••••••••
ORG_GRADLE_PROJECT_SONAR_URL ••••••••••••••••
How can I get Travis CI to reach the sonar server that we have?

Jenkins: How to get node name from label to use as a parameter

I need to give a server name to a maven build. During the maven build this server name will be used to make a call that server do some tests on that server.
Our servers have jenkins slaves on them and is grouped using labels
Example
Slaves/Node | Label
Server1 | BackEndServers
Server2 | BackEndServers
Server3 | FrontEndServers
Server4 | FrontEndServers
With Elastic Axis plugin i can say run my Jenkins job on this Node Label (for example on BackEndServers) and the same project will be executed on both of the servers (Server1 & Server2).
In my case I cannot do this as maven is not installed on the BackEndServers where my code is running. But the maven build needs to know about the server names though.
So is there a way how I can get the server names from a label and then run the same job multiple times passsing each servername to the maven build?
Example
Giving that I have the label 'BackEndServers'
obtain a list of node names 'Server1,Server2'
and run my job for each node name and pass a parameter to it
aka
Having Job (with parameter Server1)
Having Job (with parameter Server2)
Use Jenkins environment variables like NODE_NAME in the Maven command of the build job as value for a system property. For example:
mvn clean install -Djenkins.node.name=${NODE_NAME}
In your Maven project (pom.xml) configure the plugin, which requires the node name, with the help of following property: ${jenkins.node.name}
Here are some links - how to trigger Jenkins builds remotely:
How to trigger Jenkins builds remotely and to pass paramter
Triggering builds remotely in Jenkins
Launching a build with parameters
I don't, if it is possible in the way you want it. But the provided information should help you to find a solution.
Try Jenkins.getInstance().getComputer(env.NODE_NAME).getNode() See more on official Doc
In the end I created a 2 jobs.
To interogate the Jenkens nodes for me and build up a string of servers to use
Then use Dynamic Axis lable with the list I have in Job 1 to execute my maven build
In Job 1 - I used The EnjEnv plugin and it has a 'Evaludated Groovy Script' section that basically you can do anything... but it should return a property map. I don't know how to return a value from a Groovy script so this worked kewl for me as I can reference property (or Environment variables) from almost anyware
import hudson.model.*
String labelIWantServersOf = TheLabelUsedOnTheElasticAxisPlugin; // This is the label assosiated with nodes for which i want the server names of
String serverList = '';
for (aSlave in hudson.model.Hudson.instance.slaves) {
out.println('Evaluating Server(' + aSlave.name + ') with label = ' + aSlave.getLabelString());
if (aSlave.getLabelString().indexOf(labelIWantServersOf ) > -1) {
serverList += aSlave.name + ' ';
out.println('Valid server found: ' + aSlave.name);
}
}
out.println('Final server list where SOAP projects will run on = ' + serverList + ' which will be used in the environment envInject map');
Map<String, String> myMap = new HashMap<>(2);
myMap.put("serverNamesToExecuteSoapProjectOn", serverList );
return myMap;
Then I had some issue to pass the Environment var onto my next job. So I simply wrote the values that I wanted to a property file using a windows batc script in the Build process
echo serverNamesToExecuteSoapProjectOn=%serverNamesToExecuteSoapProjectOn%> baseEnvMap.properties
Then as a post build action I had a "Trigger parameterized build on other projects' calling my 2nd job and I passed the baseEnvMap.properties to it.
Then on my Job 2 which is a Multiconfig job I added a Dynamic Axis using the environment var that was passed via the property file to job 2.
This will duplicate Job 2 and execute it each time with the value that the groovy script build up which I can reference in my mvn arguments.
To list out all nodes of label name LABELNAME:
http://ServerIP:8080/label/LABELNAME/api/json?pretty=true

How to send an email from Jenkins only in a release?

I was trying to resolve this issue, and searching forums etc. and trying for myself, without success.
We have a jenkins job and there we use the Release Plugin (with a standard configuration)
In the job then we have the "Perform Maven Release" in the left side to generate a version (tag, change poms, etc.) This work perfect.
We want to send an email to the team when the release has been done.
I tried the enviroment variable that the release plugin sets (IS_M2RELEASEBUILD by default) and combine with the email-ext plugin plugin where I can attach a groovy script (advanced=>trigger=>script trigger)
And I tried a lot of scripts to active the email, and none works, my last chance was:
def env = System.getenv()
env['IS_M2RELEASEBUILD'] == 'true'
but when I perform the release we have not the email sent (so this script evaluate the conditional to false or whatever)
Anyone has this setup in his Jenkins?
Thanks a lot!
You need to use "Editable Email Notification" as "Post-build Action" and paste
def env = build.getEnvironment();
String isRelease = env['IS_M2RELEASEBUILD'];
logger.println "IS_M2RELEASEBUILD="+isRelease;
if ( isRelease == null || isRelease.equals('false')) {
logger.println "cancel=true;";
cancel=true;
}
as Pre-send Script, fill in your E-Mail(s) in "Project Recipient List" and add an "Success"-Trigger.
(precondition is you have not changed the default "Release envrionment variable" in "Maven release build")
https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin
This plugin allows you to configure every aspect of email notifications. You can customize when an email is sent, who should receive it, and what the email says.
This is not an answer, just a suggestion (I can't add comments). Have you tried echoing that environment variable in a post-build and pre-build step?
Have you tried having another build run when the release build completes successfully and have that job send the email, perhaps by running a shell script.

Resources