I want to get list of commit Id's shown in Git Changelog Plugin output as post-build action and iterate through it using java. Which script/method should I use?
With a Pipeline you can have the plugin return its context. And then loop through it. This works with plugin version 2.0 and later. In this example I list all commit id:s between develop and master. But you can specify type: 'COMMIT' and specific commit if that is what you want.
node {
sh """
git clone git#github.com:jenkinsci/git-changelog-plugin.git .
"""
def changelogContext = gitChangelog returnType: 'CONTEXT',
from: [type: 'REF', value: 'master'],
to: [type: 'REF', value: 'develop']
changelogContext.commits.each { commit ->
println "Commit id: ${commit.hashFull}"
}
}
If you want to do this in pure Java, not a Pipeline. You can just use the lib.
Related
Consider a folder with two jenkins files, JenkinsfileA and JenkinsfileB.
# JenkinsfileA
node {
environment {
APP_NAME = 'APPLICATION_A'
# Load the other jenkins file
load `JenkinsfileB'
}
#JenkinsfileB
node{
echo "Got parameter $APP_NAME"
stage ('do something'){
echo 'doing something'
}
}
In B, the $APP_NAME param is coming up null. What is the canonical way to pass APP_NAME parameter from A into B? Should I be using load here or something else? Are env vars. preferred or can we make use of parameters somehow?
Update
I think the reason env isn't working for me is because jenkins is triggering second job in a completely separate workspace. I'd like to do something like this:
load 'JenkinsfileB', parameters: [[$class: 'StringParameterValue', name: 'MYPARAM', value: "some-value" ]]
But keep getting error
java.lang.IllegalArgumentException: Expected named arguments but got [{parameters=[{$class=StringParameterValue, name=MYPARAM, value=some-value}]}, JenkinsfileB]
And when I try to use build job syntax instead of load the build can't even find JenkinsfileB, so I am stumped.
Unreleated: is node still the correct directive to use to ensure builds run on any agent, or should I be using pipeline{ agent: any}? Got node from this article
My project root directory is:
D:/Project/Node_Project
I am using a gradle plugin to install nodejs temporarily in my project root directory so that some nodejs command can run in the project while the thoject builds. The plugin is as below:
plugins {
id "com.github.node-gradle.node" version "2.2.4"
}
node {
download = true
version = "10.10.0"
distBaseUrl = 'https://nodejs.org/dist'
workDir = file("${project.buildDir}/nodejs")
}
So, nodejs is getting installed inside the project in the location:
D:/Project/Node_Project/build/nodejs/node-v10.10.0-win-x64
Now, I am using a .execute(String[] "path to set at environment variable", String path of file to be executed which is in the project root directory) method to run a windows command with node dependency. Code below:
cmd = "node connect.js"
def process = cmd.execute(["PATH=${project.projectDir}/build/nodejs/node-v10.10.0-win-x64"],null)
In the above .execute method, is there a way to auto-populate the "build/nodejs/node-v10.10.0-win-x64" part of the string instead of hardcoding it into the method?
Something like:
def process = cmd.execute(["PATH=${project.projectDir}/.*"],null)
Syntax of .execute method:
https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/String.html#execute(java.lang.String[],%20java.io.File)
All the codes are inside "build.gradle" file. Please help!
I asked why you don't just write a task of type NodeTask, but I understand that you like to run a it in the background, which you can't do with that.
You could list the content of a directory and use that as part of the command. But you could also just grab it from the extension provided by the plugin.
This is not documented and it might break in future releases of the plugin, but you can do something like this (Groovy DSL):
task connectJS {
dependsOn nodeSetup
doFirst {
def connectProcess = "$node.variant.nodeExec $projectDir/src/js/connect.js".execute()
// Blocking readers (if async, pipe to a log file instead)
connectProcess.in.eachLine { logger.info(it) }
connectProcess.err.eachLine { logger.err(it) }
}
}
I have a pipeline to build a Docker image in every push to specific branches in my git repository. The problem is that semantic versioning is using '+' character to specifying the metadata section in the version name. I need to replace this char for another supported by Docker in the image name. There is any way to replace that character or use a custom version name?
I am using nebula.release to inferring the project version.
id 'nebula.release' version '10.1.1'
jib {
to {
image = "registry.gitlab.com.uy:5005/project/app:$version"
auth {
username = System.getenv('CI_REGISTRY_USER')
password = System.getenv('CI_REGISTRY_PASSWORD')
}
tags = ['latest']
}
container {
ports = ['8080']
environment = [
SPRING_OUTPUT_ANSI_ENABLED: 'ALWAYS',
]
useCurrentTimestamp = true
}
allowInsecureRegistries = true
}
jibDockerBuild.dependsOn bootJar
This is the error:
What went wrong: Execution failed for task ':jib'.
com.google.cloud.tools.jib.image.InvalidImageReferenceException: Invalid image reference:
registry.gitlab.com.uy:5005/project/app:1.0.0-rc.1.dev.0+108db18
I created this task in my build.gradle file, Is there any way to reuse it?
task cleanVersion {
ext.sanitizeVersion = { version ->
return version.toString().replace('+', '_')
}
doLast {
println sanitizeVersion("$version")
}
}
I could use some help. Thanks in advance for your time.
Since build scripts are code and it looks like jib is an extension, project.version is a retrieval property (compared to a task output or something generated), you can use the same code you have in your current cleanVersion task to configure the extension.
image = "registry.gitlab.com.uy:5005/project/app:${version.toString().replace('+', '_')}"
I am trying to get data from SQL Server database table and show it as part of choice parameter as part of a Jenkins Job Build Parameters that I am trying to setup.
I am trying to figure out how to use Extensible Choice for this.
The Choice provider I used is "System Groovy Choice Parameter"
import groovy.sql.Sql
import com.microsoft.sqlserver.jdbc.SQLServerDriver
def output = []
def configuration = [
'dbInstance' : 'servername',
'dbPort' : 0000,
'dbName' : 'dbName',
'dbUser' : 'dbUser',
'dbPass' : 'dbPass'
]
def sql = Sql.newInstance("jdbc:sqlserver://${configuration.dbInstance}:${configuration.dbPort};"
+ "databaseName=" + configuration.dbName,
configuration.dbUser, configuration.dbPass,
'com.microsoft.sqlserver.jdbc.SQLServerDriver')
String sqlString = "SELECT * FROM dbTable"
sql.eachRow(sqlString){ row -> output.push(row[0])
}
return output.sort()
Below is the error I see. Which I understand I see because the jdbc driver is not present. I downloaded the driver from the link below:
https://www.microsoft.com/en-us/download/details.aspx?id=11774
I followed the instructions as to where it should be unzipped to as mentioned in the instructions.
I saw that the CLASSPATH variable is missing, so i went ahead and created the Environment variable with path: "C:\Program Files\sqljdbc_6.0\enu\sqljdbc.jar"
Error: unable to resolve class com.microsoft.sqlserver.jdbc.SQLServerDriver
How do i make sure that the script runs successfully and returns all the data to Extensible Choice. If there is anyother way to do this, I am open to suggestions.
Thank you very much
To resolve the issue I had to copy the "sqljdbc4.jar" file to the following location "C:\Java\jdk1.8.0_92\jre\lib\ext" since that is the path where the JAVA searches for the external jars. Use 4th version for the file which will have 4 in the file name as above as that is version Jenkins supports.
How can I access the current build commit's sha1 of my project from Java code?
This is preferred but not obligatory:
Getting the shortened version of sha1 (say, 6 first chars)
A solution without any side libraries or frameworks
I'm using OS.X v. 10.10, Android Studio v. 1.2.2.
You can get this information in gradle. For example you can use this function
def gitHash() {
try {
def process = 'git log -1 --format=%H'.execute()
def longHash = process.getText().trim()
println 'gitHash is ' + longHash
longHash
}
catch (ignore) {
println 'unable to get git hash'
'ERROR'
}
}
and you can assign the value to (for example) your client version. Then you can retrieve the client version from your app. You need git on your PATH, and you can change the format (--format) to get the desired one.