Scan Multibranch Pipeline Triggers in Jenkinsfile - jenkins-pipeline

Is it possible to have this section from the Jenkins UI into a Jenkinsfile instead?

You can use pollSCM trigger.
Eg: triggers { pollSCM('H */4 * * 1-5') }
Doc: https://jenkins.io/doc/book/pipeline/syntax/

Related

How to wrap an existing Gradle task with parameters into a custom task?

I am trying to create a custom Gradle task which invokes an existing Gradle task with parameters which are specific to my project. Here is how I invoke the task from the command line:
./gradlew downloadJson \
-Pendpoint=http://example.com/foo \
-Pdestination=src/main/com/example/foo.json
I would like to create a downloadFoo task which I can invoke without specifying the parameters explicitely.
tasks.register("downloadFoo" /* type needed? */) {
// What goes here?
}
Related
Create Gradle Tasks that executes other gradle task with parameters
How to pass Custom gradle Task as parameter to another Custom gradle Task
There's no real concept of tasks wrapping other tasks in Gradle...
What you can do in this situation is to just create a new task of type ApolloDownloadSchemaTask, and then set the properties:
import com.apollographql.apollo.gradle.internal.ApolloDownloadSchemaTask
tasks.register("downloadFoo", ApolloDownloadSchemaTask) { task ->
description("Downloads foo.")
group("Apollo")
task.schemaFilePath.set("src/main/com/example/foo.json")
task.endpointUrl.set("http://example.com/foo")
}
Try this form:
tasks.register("downloadBackendSchema", com.apollographql.apollo.gradle.internal.ApolloDownloadSchemaTask) { task ->
description("Downloads Apollo Schema")
group("Apollo")
task.schemaRelativeToProject.set("src/main/java/com/project/backend/grqphql/schema.json")
task.endpoint.set("http://your-project.com/graphql")
}

How to execute a task that is created in execution phase?

I am migration from Gradle 4.x to 6.x.
Currently I create in the execution phase many dynamic tasks with data from previous steps of the build. The tasks can't be define in the definition phase. If I would create all possible task in the definition phase this would need hours and the execution would run in seconds.
In Gradle 4.x I have execute this tasks with foo.execute() immediately. This is not possible with Gradle 6.x anymore. What is the best replacement for it?
Is ist possible to add dependencies in the execution phase? Also in future versions? I thing on a structure like:
task taskFactory {
DynamicTask dt = new DynamicTask()
mainTask.dependsOn dt
...
}
task mainTask(dependsOn taskFactory) {
...
}
Or is there a phase between definition phase and execution phase?

Sonar scanner report file with taskId and analysisId

What I have for JAVA
I am using Jenkins as my CI/CD server and I created a Jenkinsfile for my JAVA project and for the scanning and quality I am using the maven sonar plugin. The mvn sonar:sonar command generate a file at target/sonar/report-task.txt. The file contains information related with the scanning process and using that information I am able to call the SonarQube REST API with the taskId generated and then I am able to call the REST API using analysisId and decide if the pipeline is broken based on the quality conditions.
What I want for Javascript (any other type of project)
I am trying to do something similar for a Javascript project but this time and using the sonar-scanner from command line but I realized that there is not file generated as report-task.txt ( I believe this file is only generated by maven sonar-plugin). So I will like to know if there is a way to generate that kind of information.
I really need the taskId value in order to do dynamically calls to SonarQube REST API once the scanner process has started.
Since you're using a Jenkinsfile there's no need to do this manually. From the docs
node {
stage('SCM') {
git 'https://github.com/foo/bar.git'
}
stage('SonarQube analysis') {
withSonarQubeEnv('My SonarQube Server') {
sh 'mvn clean package sonar:sonar'
} // SonarQube taskId is automatically attached to the pipeline context
}
}
// No need to occupy a node
stage("Quality Gate"){
timeout(time: 1, unit: 'HOURS') { // Just in case something goes wrong, pipeline will be killed after a timeout
def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
If you're not using Maven to build and analyze, then just sub-in the correct commands as appropriate.
The file contains information related with the scanning process is:
.scannerwork/report-task.txt

How can I conditionally run code if a Gradle plugin is applied?

I have a script plugin that I would like to:
Check if the ivy-publish applied (via apply plugin: ivy-publish):
If it is applied, declare publishing { repositories { ivy { } } }
If it's not applied, run some other code
However, I'm unsure of how to actually run code if the ivy-publish plugin is applied, and I couldn't find anything about that in the documentation. Is there any way to do this?
You can use the PluginManager.withPlugin(String id, Action<? super AppliedPlugin> action) method. From the Javadoc:
If a plugin with the specified ID has already been applied, the supplied action will be executed immediately. Otherwise, the action will executed immediately after a plugin with the specified ID is applied.
In your build script you could do something like:
pluginManager.withPlugin('ivy-publish') {
// Do configuration
}
You can always use findPlugin:
println project.plugins.findPlugin('ivy-publish')
Or use:
if (project.getPluginManager().hasPlugin("ivy-publish")) {
..
}

Is there a Maven plugin to create Jenkins job

I have about a hundred maven projects for which I would like to create Jenkins jobs. Instead of creating the jobs by hand, with the likely attendant mistakes, is there a way to create from a maven plugin?
There is the job-dsl plugin which allows you to build jobs in a groovy DSL
This works by providing a build step in a separate job
You can loop through your jobs as explained here
def giturl = 'https://github.com/quidryan/aws-sdk-test.git'
for(i in 0..10) {
job("DSL-Tutorial-1-Test-${i}") {
scm {
git(giturl)
}
steps {
maven("test -Dtest.suite=${i}")
}
}
}

Resources