How To Pass Commands Onto A Windows Container Using Jenkinsfile - docker

I have a jenkinsfile job that is calling an image and spinning a windows container on a windows Jenkins agent. In the next steps of the job, I am sending commands to check SCM and to run scripts, but I notice that they all run on the Agent itself and don't get to the container.
I have similar jobs that run on Linux that successfully pass the commands to the containers, but I failed to duplicate this in windows.
Here is the edited jenkinsfile:
pipeline {
agent {
docker {
label 'Windows'
image "${docker_registry}/${imageName}:${imageTag}"
registryUrl "https://${docker_registry}"
}
}
options {
skipDefaultCheckout()
buildDiscarder(logRotator(numToKeepStr: '90', artifactNumToKeepStr: '90'))
timeout(time: 1, unit: 'HOURS')
ansiColor('xterm')
timestamps()
}
parameters {
string(
name: 'JOB_BRANCH',
defaultValue: '',
)
stages{
stage('Check Out SCM'){
steps{
checkout([$class: 'GitSCM',
branches: [[name: "${JOB_BRANCH}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'job'], [$class: 'CleanCheckout']], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'password', url: 'https://stash']]])
}
}
stage('Run Container'){
steps{
script{
bat label: 'set location' , script: "powershell -Command set-location ${JOB_FOLDER}"
bat label: 'set location' , script: "powersehll -Command msbuild '.\\solution.sln' /property:Configuration=debug"
}
}
}
}
I apologize I could not find a way to make this look any better.
Any advice will be greatly appreciated!!

Related

Why does my jenkinsFile stage take so long to run?

i'm fairly new to jenkins and scripted sequential pipelines.
As for now I have two simple stages that are "git checkout" and then "Build".
properties([parameters([choice(choices: ['master'], description: 'Branch to select', name: 'branch')])])
node{
stage('Git checkout'){
echo "Pulling changes ${params.branch}"
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'some-credentials', url: '<my-git-url>']]])
}
stage('Build'){
dir('project/cms') {
withGradle(){
echo 'Creating build folder'
powershell './gradlew clean build'
}
}
}
}
The first stage runs within 5s but the second one takes too long. When I go to check concole output it sais within 50s "Build Successful" but the stage doesn't stop running.
Is there a way to make the stage stop and continue with the pipeline sctipt?
I don't know what can be done to solve this. It just keeps running for more than 10min

How to get the latest git commit time of another repository from a Jenkins declarative pipeline

I have a declarative pipeline. In this pipeline I want to add a verify step to check the last code commit time which lies in another repository. Based on the commit time, I do have to proceed with the next steps.
I am facing issues in fetching the commit time. I am very new to git commands, so unable to resolve the issue.
Points - My Jenkins file is in one repository, and I need the commit details of another repository. I need specifically the commit time (Epoc)
My code looks like this -
#Library('xx-libs') _
pipeline {
agent any
options {
timeout(time: 2, unit: 'HOURS')
}
parameters {
xxxxx
}
environment {
xxxxxx
}
stages {
stage('Checkout source code') {
steps {
script {
checkout ([$class: 'GitSCM',
branches: [[name: '*/'+branch]],
extensions: [
[$class: 'CloneOption', noTags: true, timeout: 20],
[$class: 'RelativeTargetDirectory', relativeTargetDir: '/tmp/core/'],
[$class: 'SparseCheckoutPaths', sparseCheckoutPaths:[[$class:'SparseCheckoutPath', path:'code/System/Infra/Version/version.cpp']]],
[$class: 'CleanBeforeCheckout']
],
userRemoteConfigs: [[
credentialsId: 'azure-bearer-auto-updated',
url: 'https://xx.xx.com/xxx/xxx/_git/core'
]]
])
git_time = sh script: "git show -s --format=%ct", returnStdout: true
echo "$git_time"
}
}
}
} // stages
}

Jenkins "Git Parameter" plugin with "useRepository" option

I'm using "Git Parameter" plugin to allow users to pick branch\tag for configured repositories.
This plugin has "useRepository" option to allow linking with the configured repos in the "Pipeline script from SCM" option:
This assumes that i need to preconfigure some repos in the Jenkins pipeline (in the Jenkins UI) to be able to use "Git Parameter" plugin in the pipelines.
But i want to dynamically predefine list of repos from the pipeline code itself, without any configuration in the "Pipeline script from SCM" section.
Unfortunately this doesn't work.
I'm tried to add "checkout" block with "GitSCM" class before calling "gitParameter" but with no success.
Code example:
def app_components = [
BACKEND : ["NAME": "backend", "GIT": "ssh://git#xxx.local/_git/backend"],
FRONTEND : ["NAME": "frontend", "GIT": "ssh://git#xxx.local/_git/fronend"]
]
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
dynamicParameters = []
app_components.eachWithIndex { name, components, index ->
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CleanCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'XXX', url: components.GIT]]
])
dynamicParameters << gitParameter(name: 'BRANCH_' + name, defaultValue: 'develop', quickFilterEnabled: true, type: 'PT_BRANCH_TAG', listSize: '10', useRepository: components.GIT)
}
def userInput = input(
id: 'userInput', message: 'Test message:?',
parameters: dynamicParameters
)
println(userInput);
}
}
}
}
Git parameters text fields are always empty:
Could you advice some solution in this scenario?
Play around with adding
gitParameter branchFilter: 'origin/(.*)',

Trigger pipeline after a push on bitbucket server

I'm creating this new Job based on pipeline on jenkins. I want my jenkinsfile to be on bitbucket reposiotry : Let's say my config file is on bitbucket.org/config.git
The job mission is to clean install a project bitbucket.org/myProject.git
How can I configure the pipeline so it will trigger if any push is made in bitbucket.org/myProject.git and following the steps defined in bitbucket.org/config.git?
I do not want to create multi-branch pipeline and I do not want my jenkins file to be on the same repository than my project to compile.
My current config is:
pipeline {
agent any
parameters {
string(defaultValue: '', description: 'URL', name: 'GIT_URL')
string(defaultValue: '', description: 'Credential', name: 'CREDENTIAL_ID')
}
stages {
stage ('Initialize') {
steps {
git branch: 'develop', credentialsId: "${params.CREDENTIAL_ID}", url: "${params.GIT_URL}"
}
}
stage ('Build') {
steps {
sh 'mvn clean install '
echo 'build'
}
}
}
You can use shared Libraries in Jenkins. you would still need a Jenkinsfile in your code, but that would not contain any logic. It would simply refer the shared library and pass any params like git repo path.
For more information on shared libraries please refer this link https://jenkins.io/doc/book/pipeline/shared-libraries/.
For triggering the build, you can define a trigger in your pipeline. Example :
triggers {
pollSCM('H/5 * * * *')
}
or use webhooks if you don't want to poll.
Actually, i managed to make it work.
In my jenkins pipeline, i activated "Build when a change is pushed to BitBucket".
node {
checkout([$class: 'GitSCM',
branches: [[name: 'feature/test-b']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SubmoduleOption', disableSubmodules: false,
parentCredentials: false, recursiveSubmodules: true, reference: '',
trackingSubmodules: false]], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'admin',
url: 'http://localhost:7990/scm/bout/boutique-a.git']]])
}
When a change is made in boutique-a in the branch 'feature/test-b' my job is triggered which is cool.
Now i have this other issue, how can i trigger when change are made in feature/*
It looks like i cannot access to env.BRANCH_NAME when im not in a multibranch pipeline

WaitForQualityGate exit with error on Jenkins slave

I'm running a Jenkins instance with 1 master / 1 slave, connected to a Sonarqube instance. I'm using pipeline jobs, and it works fine except on jenkins slave where the "WaitForQualityGate" stage doesn't work. It works fine on master.
My job exit with this error:
Java.lang.IllegalStateException: Unable to get SonarQube task id and/or server name. Please use the 'withSonarQubeEnv' wrapper to run your analysis.
Even if the stage "withSonarQubeEnv" is called before.
My configuration is:
Jenkins job have a "pipeline script" checking-out my source code + the pipeline script
Shared libraries are loaded implicitly
withSonarQubeEnv is called during "testCover" and waitForQualityGate is called during "testQualityGate"
Jenkins job pipeline script:
node(){
checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'jenkinsfile'], [$class: 'IgnoreNotifyCommit'], [$class: 'WipeWorkspace']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'credential', url: 'https://pipeline.git']]]
checkout changelog: true, scm: [$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'src'], [$class: 'WipeWorkspace']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'credential', url: 'https://sourcecode.git']]]
load 'jenkinsfile/Jenkinsfile'
}()
Shared library (testCover):
echo "Testing the coverage of the application"
withSonarQubeEnv('sonarqube') {
withCredentials([string(credentialsId: 'sonarqube-token', variable: 'sonarqube_token')]) {
def scannerCmd = "sonar-scanner -e";
scannerCmd += " -Dhttps.proxyHost=proxy.com";
scannerCmd += " -Dhttps.proxyPort=8888";
scannerCmd += " -Dhttp.proxyHost=proxy.com";
scannerCmd += " -Dhttp.proxyPort=8888";
scannerCmd += " -Dsonar.login=${env.sonarqube_token}";
scannerCmd += " -Dsonar.password=";
sh "${scannerCmd}"
}
}
Shared library (testQualityGate):
sleep 10
timeout(time: 3, unit: 'MINUTES') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
Pipeline job:
{->
node {
dir('src'){
stage ('Init') {
initLib('node7')
}
stage ('Build app') {
withCredentials([[
$class: 'UsernamePasswordMultiBinding',
credentialsId: 'npm-server',
usernameVariable: 'REG',
passwordVariable: 'TOKEN'
]]) {
sh "echo '\n//${env.REG}/:_authToken=${env.TOKEN}' >> .npmrc"
buildApp()
}
}
stage ('Test / Lint') {
testApp()
}
stage ('Cover / static analysis') {
testCover()
}
stage ('Quality Gate') {
testQualityGate()
}
stage ('Flowdock notification') {
notifyFlowdock()
}
}
}
}
EDIT: After investigating deeper, i found out that the problem might come from the 2 calls to node statement (1 in my pipeline script (job), 1 in my pipeline file). Unfortunately, that's not solving my issue =/
EDIT 2: I checked that the line "Working dir:" and "ANALYSIS SUCCESSFULL" are present in my build log, as Sonar plugin use those lines to find out the URL + the PATH for the ".sonar" folder (where the task-report.txt is), and they are ! So basically, it's working on Master node, but not on Slave, even if they both have the same output =/
I'm answering my own question to let you know that there was an actual issue that has been spotted in sonar plugin for jenkins. Here is the patch https://repox.sonarsource.com/sonarsource-public-builds/org/jenkins-ci/plugins/sonar/2.6.1.1212/sonar-2.6.1.1212.hpi
Thanks all the people in the google group (https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!topic/sonarqube/z_K_wz_8Vw8) for the help provided : )

Resources