Skip to content

Commit

Permalink
Do not tag traces with shared libraries Git metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog committed Dec 20, 2024
1 parent 9bd7908 commit 2e29383
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
31 changes: 31 additions & 0 deletions docker/controller-node/70-configure-shared-library.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// A script that configures sample Jenkins shared library (https://www.jenkins.io/doc/book/pipeline/shared-libraries/)

import jenkins.plugins.git.GitSCMSource
import org.jenkinsci.plugins.workflow.libs.GlobalLibraries
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration
import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever

// Define the shared library configuration
def libraryName = "test-shared-lib"
def defaultVersion = "main"
def gitUrl = "[email protected]:DataDog/ci-visibility-test-jenkins-shared-library.git"
def credentialsId = "github-ssh"

// Create a Git SCM source
def gitSCMSource = new GitSCMSource(null, gitUrl, credentialsId, "*", "", false)

// Create a retriever using the SCM source
def retriever = new SCMSourceRetriever(gitSCMSource)

// Create the library configuration
def libraryConfig = new LibraryConfiguration(libraryName, retriever)
libraryConfig.defaultVersion = defaultVersion
libraryConfig.implicit = false
libraryConfig.allowVersionOverride = true

// Add the library configuration to the global libraries
def globalLibraries = GlobalLibraries.get()
def existingLibraries = globalLibraries.libraries.findAll { it.name != libraryName }
globalLibraries.libraries = existingLibraries + libraryConfig

println "Global Trusted Pipeline Library '${libraryName}' has been configured."
1 change: 1 addition & 0 deletions docker/controller-node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ COPY 40-create-datadog-key_credentials.groovy /usr/share/jenkins/ref/init.groovy
COPY 50-create-jobs.groovy /usr/share/jenkins/ref/init.groovy.d/50-create-jobs.groovy
COPY 51-create-multi-branch-pipeline.groovy /usr/share/jenkins/ref/init.groovy.d/51-create-multi-branch-pipeline.groovy
COPY 60-add-script-signature-approvals.groovy /usr/share/jenkins/ref/init.groovy.d/60-add-script-signature-approvals.groovy
COPY 70-configure-shared-library.groovy /usr/share/jenkins/ref/init.groovy.d/70-configure-shared-library.groovy

RUN /var/jenkins_home/add-github-to-known-hosts.sh

Expand Down
12 changes: 12 additions & 0 deletions docker/controller-node/jobs/test-pipeline-shared-library.cps
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@Library("test-shared-lib@main") _

pipeline {
agent any
stages {
stage('Hello') {
steps {
sayhello "Alice"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ of this software and associated documentation files (the "Software"), to deal
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Action;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.SCMListener;
Expand Down Expand Up @@ -82,12 +83,16 @@ public class DatadogSCMListener extends SCMListener {
* @param listener - Current build listener
* @param changelogFile - Changelog
* @param pollingBaseline - Polling
* @throws Exception if an error is encountered
*/
@Override
public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListener listener,
File changelogFile, SCMRevisionState pollingBaseline) throws Exception {
File changelogFile, SCMRevisionState pollingBaseline) {
try {
if (isSharedLibraryCheckout(build, workspace)) {
// We do not want to tag traces with shared library Git info
return;
}

// Process only if job is NOT in excluded and is in included
if (!DatadogUtilities.isJobTracked(build)) {
return;
Expand Down Expand Up @@ -157,6 +162,25 @@ public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListene
}
}

private boolean isSharedLibraryCheckout(Run<?, ?> build, FilePath workspace) {
FilePath parent = workspace.getParent();
if (parent == null) {
return false;
}
String name = parent.getName();
if (!name.endsWith("@libs")) {
return false;
}
// The workspace name ends with @libs, this is most likely a shared library checkout.
// Double-check if the build has a org.jenkinsci.plugins.workflow.libs.LibrariesAction
for (Action action : build.getAllActions()) {
if (action.getClass().getName().equals("org.jenkinsci.plugins.workflow.libs.LibrariesAction")) {
return true;
}
}
return false;
}

private boolean isGit(SCM scm) {
if (scm == null) {
return false;
Expand Down

0 comments on commit 2e29383

Please sign in to comment.