Why does my jenkins script seem to 'forget origin' even though it pull code from it? - jenkins

below is my script. idea is to create projects based off of a template project so the developer would not need to do repeat this work every time a new project comes along... the template project will have all necessary scripts to run CI, release automatically, and deploy automatically...
node {
try {
stage('Clean Up') {
deleteDir()
stage('Verify New Github Repo Exists') {
sh 'git ls-remote git#github.com:account/${githubProject}.git' // check if repo exist
stage('Clone Github Repo') {
// clone into directory and cd
sh 'git clone git#github.com:account/${githubProject}.git .'
sh 'git remote -v'
sh 'ls -lash'
stage('Merge Template Into Project') {
// add the template into the project
sh 'git remote add template git#github.com:account/${appType}-jenkins-ci-template.git'
sh 'git remote -v' // this shows both origin and template repos
sh 'git fetch --all'
sh 'git merge template/master' // able to merge origin's master with template's
sh 'ls -lash'
sh 'git log --graph --abbrev-commit --max-count=10'
sh 'git push -u origin master' // when the code get here, it fails to push, ends up with ERROR: Repository not found.
// do the work to replace all __APP_NAME__ with the actual app name/service
// commit and push to master
stage('Configure Project Properties') {
// clone and copy property files from template project
// do the work to replace all __APP_NAME__ with the actual app name/service
// commit and push to master
stage('Wrap Up') {
// creating jenkins jobs will continue to be manual
}
}
}
}
}
}
} catch (e) {
//notifyFailure(e, "Script failure!")
currentBuild.result = "FAILURE"
}
}

Related

Update file content in gitlab using jenkin

Is there a way, after updating the file content via Jenkinsfile, to push it back to Git Lab and replace the previous file?
I fetched all the files to the working dir and changed the content with sed.
pipeline
{
agent any
stages {
stage('Update deployment file') {
steps {
sh 'sed -i "s/source/update/g" file.txt'
}
}
stage('Push to gitlab') {
steps {
?????????
}
}
Thanks in advance.
You can simply use a shell block for this. If you need credentials to push, you may have to append them to the URL or configure them in the git client.
stage(''Push to gitlab''){
steps{
sh '''
git add file.txt
git commit -m "Updates file1.txt"
git push
'''
}
}
In extension to the Answer before.
You can set the ssh Key for git with the following snipped:
sshagent(['<credentialsID>']) {
sh("git push origin HEAD:${BRANCH}")
}

Jenkins Multibranch Pipeline: How to checkout only once?

I have created very basic Multibranch Pipeline on my local Jenkins via BlueOcean UI. From default config I removed almost all behaviors except one for discovering branches. The config looks line follows:
Within Jenkinsfile I'm trying to setup following scenario:
Checkout branch
(optionally) Merge it to master branch
Build Back-end
Build Front-end
Snippet from my Jenkinsfile:
pipeline {
agent none
stages {
stage('Setup') {
agent {
label "master"
}
steps {
sh "git checkout -f ${env.BRANCH_NAME}"
}
}
stage('Merge with master') {
when {
not {
branch 'master'
}
}
agent {
label "master"
}
steps {
sh 'git checkout -f origin/master'
sh "git merge --ff-only ${env.BRANCH_NAME}"
}
}
stage('Build Back-end') {
agent {
docker {
image 'openjdk:8'
}
}
steps {
sh './gradlew build'
}
}
stage ('Build Front-end') {
agent {
docker {
image 'saddeveloper/node-chromium'
}
}
steps {
dir ('./front-end') {
sh 'npm install'
sh 'npm run buildProd'
sh 'npm run testHeadless'
}
}
}
}
}
Pipeline itself and building steps works fine, but the problem is that Jenkins adds "Check out from version control" step before each stage. The step looks for new branches, fetches refs, but also checks out current branch. Here is relevant output from full build log:
// stage Setup
> git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh git checkout -f my-branch
// stage Merge with master
> git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh git checkout -f origin/master
sh git merge --ff-only my-branch
// stage Build Back-end
> git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh ./gradlew build
// stage Build Front-end
> git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh npm install
sh npm run buildProd
sh npm run testHeadless
So as you see it effectively resets working directory to particular commit before every stage git checkout -f f067...595.
Is there any way to disable this default checkout behavior?
Or any viable option how to implement such optional merging to master branch?
Thanks!
By default, git scm will be executed in a Jenkins pipeline. You can disable it by doing:
pipeline {
agent none
options {
skipDefaultCheckout true
}
...
Also, I'd recommend take a look to other useful pipeline options https://jenkins.io/doc/book/pipeline/syntax/#options

How to copy Jenkins config files in a jenkins pipeline to Web server

I have some files.properties in Jenkins config File that I need to copy to a server during the jenkins pipeline.
pipeline code is more a less as showed, just to get an idea.
How can I add a step that copy this config file from jenkins on a destination server after las step after step DEPLOY WAR TO SERVER in pipeline like for example : "sh Scp file.properties jenkins#destinationserver:/destination/path/file.properties"
code {
stage ('Code Checkout') {
git branch: 'master',
credentialsId: 'b346fbxxxxxxxxxxxxxxxxxxx',
url: 'https://xxxxxxx#bitbucket.org/gr/code.git'
}
stage ('Check Branch') {
sh 'git branch'
}
stage('Compile and Build WAR') {
sh 'mvn clean compile war:war'
stage ('Deploy WAR to server') {
sh "scp .war jenkins#serverIp:/var/lib/tomcat/.war"
}
This is quite easy. You need to install the Config File Provider Plugin and then you can generate the appropriate line by visiting htts://localhost/jenkins/pipeline-syntax/. From there in the dropdown you can choose configFileProvider and fill the rest of the form.
The end result will be something like this:
configFileProvider(
[configFile(fileId: 'maven-settings-or-a-UUID-to-your-config-file', variable: 'MAVEN_SETTINGS')]) {
sh 'mvn -s $MAVEN_SETTINGS clean package'
}

Using Jenkins to deploy to staging and production based on condition

My project has a Jenkinsfile that runs smoothly. The problem is that I need to run some commands only on certain occasions. I'm using the Github plugin. I need to run the deploy only when it is in the master or a new tag, one will be for staging and the other will be production.
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'node -v'
sh 'yarn install'
sh 'yarn test -- --coverage'
}
}
stage('Build') {
steps {
sh 'yarn build'
}
}
stage('Deploy') {
steps {
sh 'aws s3 sync ./build s3://my.bucket --only-show-errors'
}
}
}
}
I need the master to deploy to a bucket and when it is new tag to another. How can I create this conditional?
How about the following working as two conditionals for two separate deployment scenarios? I think it's better to work with this using variables to indicate deployment scenarios instead of splitting this to two distinctly different steps though. You could for example write a shell script that would handle everything inside depending on tags/branches/whatever you need instead of forcing yourself to control this on pipeline level.
Each stage will have it's steps executed only when when part is satisfied. Stage Deploy will only work for master branch, while stage Deploy_NonMaster will only work any non master branch. Using the method written in when conditionals you can check for anything, including tags or whatnot.
stage ('Deploy') {
when {
expression {
GIT_BRANCH = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
return (GIT_BRANCH == 'master')
}
}
steps {
echo 'Do stuff/deploy.'
}
}
stage ('Deploy_NonMaster') {
when {
expression {
GIT_BRANCH = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()
return !(GIT_BRANCH == 'master')
}
}
steps {
echo 'Do stuff/deploy.'
}
}

Jenkins Pipeline Error

I am using the pipeline plugin in Jenkins, but unable to run shell commands. I am receiving the following error:
[develop - pipeline] Running shell script
nohup: failed to run command ‘sh’: No such file or directory
The node is an Ubuntu instance.
node ('aws-ondemand') {
//println env.BUILD_NUMBER
try {
stage 'Checkout and Build'
git url: 'git#github.com:MyAndroidRepo.git',
branch: 'develop'
sh 'git submodule init'
sh 'git submodule update'
sh './gradlew clean build'
}catch (e) {
//currentBuild.result = "FAILED"
//notifyFailed()
throw e
}
}
Nevermind. Script is fine. I was injecting env variables in the build step. I removed it and now its working.

Resources