I have the following code which is not working for me.
dir('anyName'){
checkout scm: [$class: 'GitSCM', branches: [[name: PIPELINE_VERSION]],
userRemoteConfigs: [[credentialsId: 'some-id', url: 'some git repo']]
]
}
def some = load "./pipelines/environment/some.groovy"
But I get the following error. How can I load a file and later use its internal function.
java.nio.file.NoSuchFileException:
/mnt/data/jenkins/workspace//pipelines/environment/some.groovy
From the dir step documentation:
Change current directory. Any step inside the dir block will use this directory as current and any relative path will use it as base path.
The dir command changes the base directory only for code executed inside the dir {} block, therefore when you load your file after the dir block you are back to the original workspace and the file is not found.
to solve it you can use the full path to load the file:
dir('anyName'){
checkout scm: [$class: 'GitSCM', branches: [[name: PIPELINE_VERSION]],
userRemoteConfigs: [[credentialsId: 'some-id', url: 'some git repo']]
]
}
// From here we are back to the default workspace
def some = load "./anyName/pipelines/environment/some.groovy"
Or alternatively load the file inside the dir block:
dir('anyName'){
checkout scm: [$class: 'GitSCM', branches: [[name: PIPELINE_VERSION]],
userRemoteConfigs: [[credentialsId: 'some-id', url: 'some git repo']]
]
def some = load "./pipelines/environment/some.groovy"
}
Related
I have my Jenkinsfile located in repo "A" and I was to checkout a repo in repo "B". What's the correct way to do that?
This is how I have currently doing it, but it's not working:
stage("Checkout my repo repo ") {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']],
extensions: [[$class: 'RelativeTargetDirectory',
relativeTargetDir: 'copy-repo-here']],
userRemoteConfigs: [[credentialsId: 'my-creds',
url: 'git#github.com:my-git-repo.git']]])
}
}
Any help is appreciated!
I am trying to write a stage in Jenkinsfile where I'll pass the branch name from the Jenkins job to checkout the code at a specific location.
stage("Prepare") {
steps {
checkout([$class: 'GitSCM',
branches: [[name: '*/master' ]],
extensions: scm.extensions,
userRemoteConfigs: [[
url: 'https://gitlab.example.com/user/example_repo.git',
credentialsId: 'my-gitlab-repo-creds'
]]
])
}
}
Also, how can we define the location where to checkout the project.
You're missing the power of the SCM checkout step. In turn, your pipeline is missing some important configuration to get what you want:
pass the branch name from the Jenkins job to checkout the code
define the location where to checkout the project
This can all be done. I'll explain the options used to accomplish this.
1) Pass the branch name to checkout step
You can achieve this with the BRANCH_NAME environment variable.
2) Define project checkout location
Add the following extension and get rid of the scm.extensions value.
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'myrepo']]
Specify a local directory (relative to the workspace root) where the Git repository will be checked out. If left empty, the workspace root itself will be used.
For the branches option,
The safest way is to use the refs/heads/ syntax. This way the expected branch is unambiguous.
For example:
branches: [[name: 'refs/heads/${env.BRANCH_NAME}']]
Piecing it all together,
checkout(
[
$class: 'GitSCM',
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'example_repo']],
branches: [[name: 'refs/heads/${env.BRANCH_NAME}']],
userRemoteConfigs: [
[
url: 'https://gitlab.example.com/user/example_repo.git',
credentialsId: 'my-gitlab-repo-creds',
name: 'origin'
]
]
]
)
The above code will checkout the ${env.BRANCH_NAME} branch of https://gitlab.example.com/user/example_repo.git to the $WORKSPACE_DIR/example_repo folder.
I was fixing a pipeline which was written for mercurial scm and there I found an installation parameter how would I give the same parameter to the git scm.
checkout([$class: 'MercurialSCM',
credentialsId: '',
installation: 'HG-3.4',
source: 'https://',
clean: true,
revision: branch,`branch`
browser: [$class: 'BitBucket', url: 'https://']])
checkout([$class: 'GitSCM',
branches: [[name: '${branch}']], browser: [$class: 'BitbucketWeb', repoUrl: 'https://'],
extensions: [[$class: 'CleanBeforeCheckout']],
userRemoteConfigs: [[credentialsId: 'creds',
url: 'https://']]])
I have a project that spins up EC2 instances as build slaves, the username is "ec2-user".
I am checking out a repository with submodules like this:
checkout([
$class: 'GitSCM',
branches: [[name: 'deadbeefdeadbeefcafebabe']],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false]
], submoduleCfg: [],
userRemoteConfigs: [
[credentialsId: 'deadbeef-cafe-babe-cafe-babebeef1234',
refspec: 'refs/changes/12/34567/89',
url: 'ssh://user#host:29418/some/project']
]
])
Please note that the URL has a username.
Now, the .gitmodules file looks like this:
[submodule "path/in/project"]
path = path/in/project
url = ssh://host:29418/other/project
branch = somebranch
The submodule description does not have a username specified. When it is time for the submodule clone, the clone is denied with invalid credentials. Manually editing the .gitmodules file and adding the user# in front of the URL works, but this is a bad workaround at best. Changing the URL on disk and then calling checkout again does also not work.
How can I make Jenkins use the username it used in the parent checkout? If that is not possible, what is a workaround which does not involve changing the source repo?
How can I limit the scope of a Jenkins pipeline project to only be built if a file in specific subdirectory is changed using Jenkinsfile?
I have a single Git repository with two directories. Each directory contains a separate subproject and I would like to build each subproject separately by Jenkins using Jenkinsfile. The project has the following file structure:
parent
|
+- subA
| |
| + Jenkinsfile
| + more files related to sub project A
|
+- subB
|
+ Jenkinsfile
+ more files related to sub project B
The Jenkinsfile for subA has the following configuration:
checkout scm: [
$class: 'GitSCM',
branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'https://[path]/parent.git']],
extensions: [[
$class: 'PathRestriction', includedRegions: 'subA/.*'
]]
]
The Jenkinsfile for subB is similar, the only difference being that it has specified subB as includedRegions.
In the Jenkins server, I have created two pipeline projects and pointed them to each Jenkinsfile respectively. If a file is changed in the folder subA, Jenkins pipeline project A is triggered and if a file is changed in folder subB, Jenkins pipeline project B is triggered, which is what I expect.
The problem is that the Jenkins pipeline project A is also triggered if a file is changed in subB and vice versa.
Jenkins version: 2.3
Note:
Configuring the setting Additional Behaviours -> Polling ignores commits in certain paths -> Included Regions to subA/.* or subB/.* respectively in the old Jenkins (ver 1.649) GUI results in the expected behavior.
Update:
Adding excludedRegions to the Jenkinsfiles, e.g.
checkout scm: [
$class: 'GitSCM',
branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'https://[path]/parent.git']],
extensions: [[
$class: 'PathRestriction', excludedRegions: '', includedRegions: 'subA/.*'
]]
]
does not change the behavior. Both subprojects are still rebuilt, despite files are only changed in one subdirectory.
This is not supported yet as this issue implies.
There is an open issue preventing proper function: https://issues.jenkins-ci.org/browse/JENKINS-36195
It has a workaround, which is to disable remote polling ([$class: 'DisableRemotePoll']):
checkout([$class: 'GitSCM',
branches: [[name: "*/master"]],
extensions: [
[$class: 'PathRestriction', excludedRegions: '', includedRegions: '<fill me in with regex \n delimited, leave excludedRegions as empty>'],
[$class: 'DisableRemotePoll']
],
submoduleCfg: [],
userRemoteConfigs: [[url: "<my git url>", credentialsId: "$GIT_KEY"]]])