I am trying to create pipeline jobs with jenkins dsl. the pipeline job takes the cpsscm if I specify the git url only without branches or credentials. but when I change the brancha nd add credentials, it doesn;t work
pipelineJob("foo"){
definition {
cpsSCM {
git(GIT_URL,BRANCH)
}
}
}
The above works. but the following doesn't work
pipelineJob("foobar"){
definition {
cpsScm {
scm{
git{
branch(BRANCH)
remote{
credentials('kjsks2304-sid34-234')
url(GIT_URL)
}
}
}
scriptPath("JenkinsFile")
}
}
}
}
the credentials is the id in the credentials plugin in jenkins. The git repo I am using is a private bitbucket repository
Try here this might help you achieve it, you can use the playground.
https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.helpers.workflow.WorkflowDefinitionContext.cpsScm
Related
We are deploying our application using Jenkins pipeline like this -
pipeline {
agent any
stages {
stage('Build For Production') {
when { branch 'development' }
steps {
sh './bin/build.sh'
}
}
stage('Build For Production') {
when { branch 'master' }
steps {
sh './bin/copy_needed_auth.sh'
sh './bin/build.sh'
}
}
}
}
When a developer pushes code on bitbucket, The application is deployed automatically. Using branch, we set our deployment strategy.
when { branch 'master' }
But we need to set a manual chacking for deploying on production (master branch) like - when a developer will merge code in the master branch, he will also set some tag or something like that so that Jenkins pipeline will check branch + other manual logic to deploy in production.
we are doing like this -
when {
branch 'master'
tag: 'release-*'
}
But it's not working. Is there any other strategy to do that?
Use following code to use several when conditions:
when {
allOf {
branch 'master';
tag "release-*"
}
}
Related docs you can find here
I already wrote an example Jenkinsfile to checkout and build and deploy a signal project. Is there a way to do all these for multiple project in different git repo the same time just using one Jenkinsfile ? I know I can set up these projects as independent jobs and use a Jenkinsfile to call them,but I'm wondering if I can do this without independent jobs.
Thanks.
You can make use of Job DSL Plugin to achieve this.
Jenkins Job DSL API will help you to write DSL scripts. You can find all the built-in DSL methods that will be needed to construct jobs.
Example pipeline script:
pipeline {
agent any
stages {
stage('Job1') {
steps {
//Pipeline Job
jobDsl scriptText: '''pipelineJob(\"$job1\") {
definition {
cpsScm {
scm {
git {
remote{
name('origin')
url('https://github.com/satta19/user-node.git')
credentials('git2-cred')
}
branch ('master')
}
}
scriptPath('Jenkinsfile')
}
}
}'''
}
}
stage('Job2') {
steps {
//Freestyle job
jobDsl scriptText: '''job(\"$job2\") {
steps {
shell(\'echo Hello World!\')
}
}'''
}
}
}
}
Note: I have taken the jobs name as string parameter i.e. $job1 and $job2 in the above example pipeline script.
I've got a multibranch pipeline, defined in a scripted pipeline (from a library) that is coordinating ~100 builds, each build across multiple slaves (different operating systems). One of the Operating systems is Windows, which has a 255 character path limitation. Because some of our jobs have ~200 character paths in them (which we can't control because it is a vendor provided hell), i need to change the step/node workspace on our windows slaves, ideally changing it with the node() step, so that git is automatically checked out only once into the custom workspace.
I've tried all kinds of various styles:
This works in the Declarative Pipeline:
stage('blah') {
node {
label 'win'
customWorkspace "c:\\w\\${JOB_NAME"
}
steps {
...
}
}
But i can't find the equivalent for scripted pipelines:
pipeline {
stage('stage1') {
node('win-node') {
// the git repository is checked out to ${env.WORKSPACE}, but it's unusable due to the path length issue
ws("c:\\w\\${JOB_NAME}") {
// this switches the workspace, but doesn't clone the git repo again
body()
}
}
}
}
Ideally, i'd like something like this:
pipeline {
stage('stage1') {
node('win-node', ws="c:\\w\\${JOB_NAME}") {
body()
}
}
}
Any recommendations?
Not tested (specially define options inside node), but you could try to skip default checkout and do it after changing the workspace, something like this:
pipeline {
stage('stage1') {
node('win-node') {
options {
skipDefaultCheckout true // prevent checkout to default workspace
}
ws("c:\\w\\${JOB_NAME}") {
checkout scm // perform default checkout here
body()
}
}
}
}
I have a Bitbucked repo, and I want to satrt my Jenkins pipeline job only afrer commit with tag like "release-1.0.*"
So, I seted my job up with pipeline script:
pipeline {
agent any
stages {
stage ('Prepare') {
when {
tag "release*"
}
steps {
git branch: 'tag1', url: 'git#bitbucket.org:m*********ny/tests.git'
}
}
stage ('Deploy') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: "JenkinsSrv", transfers: [sshTransfer(execCommand: 'pwd')])])
}
}
}
post ('POST BUILD'){
always {
echo 'This is post action!!!'
}
}
}
Also, I turned on Bitbucked webhook plugin, than my repo notify Jenkins about new changes.
But my solution doesn't work. Help me resolve this case.
enter image description here
According to the official documentation for a Jenkins pipeline, the option you are looking for is the changelog condition inside the when directive. For example:
when { changelog 'release*' }
i found one link in google
https://docs.xebialabs.com/xl-deploy/concept/jenkins-xl-deploy-plugin.html
here the following steps are present but it is throwing
No dsl method xldCreatePackage
node {
stage('Checkout') {
git url: '<git_project_url>'
}
stage('Package') {
xldCreatePackage artifactsPath: 'build/libs', manifestPath: 'deployit-manifest.xml', darPath: '$JOB_NAME-$BUILD_NUMBER.0.dar'
}
stage('Publish') {
xldPublishPackage serverCredentials: '<user_name>', darPath: '$JOB_NAME-$BUILD_NUMBER.0.dar'
}
stage('Deploy') {
xldDeploy serverCredentials: '<user_name>', environmentId: 'Environments/Dev', packageId: 'Applications/<project_name>/$BUILD_NUMBER.0'
}
}
You need to install the XL Deploy plugin into your Jenkins installation.
Right now you have the Pipeline plugin installed which gives you the Jenkinsfile & pipeline capability only. The XebiaLabs Jenkins plugin will take advantage of that but you need to plugin to give you the functionality you want.