Issue
I have created a Jenkins job that uses a groovy script (gradle) to download a file.
stages {
stage('Import and Unzip') {
steps {
cleanWs()
checkout([$class: 'GitSCM',
branches: [[name: "$GIT_BRANCH" ]],
extensions: [[$class: 'PruneStaleBranch']],
userRemoteConfigs: [[
url: '[email protected]/repo1.git',
]]
])
script{
bat './gradlew --build-file scripts/Jenkins/build.gradle'
}
dir('scripts/Jenkins') {
bat '../../../gradlew downloadfile'
}
}
}
}
So now when I run this job I have a file in the Jenkins workspace …
I want to add and commit it on another github repo : [email protected]/repo2.git
Any ideas how to do it ?
Thanks in advance
Solution
Solution
You should be able do the following
- Create a new directory in your workspace
- Checkout the repository you wish to push to ( I’m calling it repo2.git )
- Copy the file from the workspace to the new directory you created ( the one where repo2 is cloned )
- Run the appropriate git commands in bat
Without knowing the name of the file, repository, or repository branch I can’t give you exact code, but it should look something like the following
Sample Code
stages {
stage('clone repos') {
steps {
cleanWs()
// clones repo1 in ${WORKSPACE}/repo1
dir('repo1') {
checkout([
$class: 'GitSCM',
branches: [[name: "$GIT_BRANCH" ]],
extensions: [[$class: 'PruneStaleBranch']],
userRemoteConfigs: [[
url: '[email protected]/repo1.git',
]]
])
}
// clones repo2 in ${WORKSPACE}/repo2
dir('repo2') {
checkout([
$class: 'GitSCM',
branches: [[name: "$OTHER_GIT_BRANCH" ]],
extensions: [[$class: 'PruneStaleBranch']],
userRemoteConfigs: [[
url: '[email protected]/repo2.git',
]]
])
}
}
}
stage('Import and Unzip') {
steps {
dir('repo1') {
bat './gradlew --build-file scripts/Jenkins/build.gradle'
dir('scripts/Jenkins') {
bat '../../../gradlew downloadfile'
}
}
}
}
stage('commit and push') {
steps {
dir('repo2') {
/**
* Copies the downloaded file from repo1 into the
* directory where we cloned repo2 then executing the
* appropriate git commands
**/
bat '''
cp ../repo1/scripts/Jenkins/<filename> .
git add <filename>
git commit -m "commit msg"
git push
'''
}
}
}
}
I refactored your code slightly so that it checkouts the source and destination repositories in separate directories in its own stage. I think this is a cleaner way to organize the file.
Note you obviously need to replace with the file you wish to add to the other repository. You also need to change the repository url in the second checkout block to the actual URL. You will also need to add $OTHER_GIT_BRANCH as the parameter for the branch you want to push to. You may need to change the relative paths ( I tried my best without actually building a pipeline )
Answered By – Chris Maggiulli
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0