Checkout using gradle script - gradle

I am new to gradle.I want to checkout remote repository using gradle script.
It is possible to run shell command Inside gradle task to clone remote repository with datetime stamp shell commands.

Yes, you can use a gradle Exec type task to execute any arbitrary command in the OS shell. See documentation and examples here.
You didn't say what type of repo you're using, but there is a gradle git plugin to do git operations including checkout.

Here's an example of how we do it:
project.tasks.create(
name: "checkOutCurrent", group: "Server", type:Exec,
description: "Checks out the current commit on the remote server.") {
workingDir project.rootDir
commandLine 'git', 'checkout', this.commit
}

Related

Is there a way for my build.gradle to pull from the project repository (possibly prompting the user to input for the passphrase)

I am currently creating a gradle build script. The whole purpose of the script is to encrypt files. Updates from this files will come from a repository (meaning every run on my script will be a version of encrypted files). In line with this when the script is run, it needs to pull updates from the repository first. My problem is, in console, when i do the pull command, it asks for a passphrase. However, I don't know how to implement this in the gradle build script. Is there a way to prompt the user for a password first and pass it to a git command in the script? I'm just new in this so I really need your help. thanks! :)
I have tried the Exec task which executes git pull (see code below).
I have also searched for other ways such as using config credential.helper cache but my references showed that it needs to do a git push (which prompts for password) first, for it to be stored in the cache.
task pullFromRepo(type: Exec) {
commandLine "git", "pull"
doLast {
println "done pulling from repository"
}
}
I expect a possible user prompt for the passphrase(?) retrieve this user input and pass it as argument in the git pull command. is that possible? thank you!! :)

Jenkins pipeline works in UI but not from SCM

I decided to port all my Jenkins job over to Jenkins-pipeline. I did a simple test with the following Jenkinsfiles in to the UI:
pipeline {
agent any
stages {
stage('Clone Repo') {
steps {
git changelog: false, credentialsId: 'xxxxxxxxxx ', url: 'https://github.com/xxxxx/xxx.git'
}
}
}
}
This works fine. I created a repo in github and checked in this Jenkinsfile. I changes Jenkins to Pipeline script from SCM and it is finding the Jenkinsfile but falling over with the error message below. I know I've missed something basic, but reading all the documentations; I couldn't work it out. Any help is appreciated.
Here's the Jenkins job. There's a jenkinsfile the ndh_poc.
In the Script Path field, specify the location (and name) of your Jenkinsfile. This location is the one that Jenkins checks out/clones the repository containing your Jenkinsfile, which should match that of the repository’s file structure.
In your case if your jenkinsfile name is Jenkinsfile and is present within the directory ndh_poc at the root of your repository then the Script Path should be ndh_poc/Jenkinsfile

sbt dist works in terminal but not with Jenkins as shell script

I am completely new to both sbt and Jenkins. I am trying to construct a build plan using Jenkins piplelines. The following commands run just fine within shell script: sbt compile & sbt package
However, sbt dist does not work. I get the error not a valid command
What is puzzling me, I can run the command from terminal just fine.
For what its workth, here is the contents of jenkinsfile (I know sbt dist should be in build, but I am still experimenting):
pipeline {
agent any
stages {
stage('Build') {
parallel {
stage('Build') {
steps {
sh 'echo "Compiling... "'
sh 'sbt compile'
}
}
stage('Deploy') {
steps {
sh 'echo "Deploying... "'
sh 'echo "packaging.. "'
sh 'sbt dist'
}
}
}
}
}
}
Jenkins 2.89.1, sbt tried both: 0.13 and 1.0.x
I see now from comments that you are working on Play application, so it is clear now where the dist task comes from.
In Jenkins first thing that you need to make when you running SBT tasks is that you have loaded to SBT your build.xml of your project. In your shell script it is not actually visible whether you are in the root project folder or not.
Also Jenkins has integration with SBT better then just running it through shell script. You need to add this plugin to Jenkins first. Look at the attached screenshot, where part of one of my Jenkins projects is shown:
There is a special kind of build step in Jenkins. In your project configuration you should see it under the "Build step" dropdown:
I believe you should start from there. If you still need to run through shell script make sure you also cd /path/to/your/project folder before running SBT commands.
I accepted #Alexander Arendar answer, as it is the correct answer. I just want to elaborate on how I got it right, so it can be useful for others.
The answer as #Alexander mentioned is If you still need to run through shell script make sure you also cd /path/to/your/project folder before running SBT commands. I was actually doing that. But what I was completely oblivious to, is that cd to directory and running sbt dist must be withiin the same jenkins step. I was doing that in two separate steps, and ending up running sbt dist in the original directory.
One worthy note is, cd to project was needed only for sbt dist since it comes with Play. Standard sbt commands (e.g.: sbt compile) were running fine outside the play project directory.

what is the shell script command to checkout a project from github

I'm running a Jenkins job on a GitHub project (Project A), as part of this job I want to checkout another different GitHub project (Project B) using shell script command
If you really want to use a shell command for that you could go for
$ git clone https://<YOUR_REPOSITORY_URL>.
However, if you are using a Jenkins pipeline job you might consider using following command in your Jenkinsfile:
stage('Checkout') {
git branch: '<BRANCH_NAME>', credentialsId: '<JENKINS_CREDENTIAL_ID>', url: 'git#<URL_OF_REPOSITORY>'
}

nohup: failed to run command `sh`: No such file or directory

I have the following pipeline script in Jenkins:
node {
withMaven(globalMavenSettingsFilePath: '/my/path/apache-maven-3.2.2/conf/settings.xml', jdk: 'JDK 1.8.0u92', maven: 'apache-maven-3.2.2', mavenSettingsFilePath: '/my/path/apache-maven-3.2.2/conf/settings.xml') {
sh '/my/path/apache-maven-3.2.2/bin/mvn clean install'
}
}
For this, I am getting:
nohup: failed to run command `sh`: No such file or directory
ERROR: script returned exit code -2
Why is this?
I am sure that the path to my Maven installation is correct. When I run a job without the pipeline, Maven builds with no errors and I can see that it uses the same path.
This might be the result of modifying PATH.
Check your script and Global Properties and remove modifications to PATH. It is now recommended to use PATH+extra instead. It would still be picked up, but without breaking actual PATH.
Related issue on Jenkins: https://issues.jenkins-ci.org/browse/JENKINS-41339
In the end, I used shell instead of sh and it worked. No idea why, they don't have a proper API.
I would suggest to use it like this:
withMaven(
maven: 'M3',
mavenSettingsConfig: 'maven-settings-for-the-task',
mavenLocalRepo: '.repository') {
// Run the maven build
sh "mvn clean install"
}
Apart from that I would not use absolute paths to global settings.xml nor to user settings.xml. I would prefer using usage of "Config File Provider Plugin" which has the advantage to have the settings.xml on Master and available on all nodes.
See also the documentation: https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Maven+Plugin
This error comes when you are trying to run script copied from windows machine to unix machine.
YOU need to change the format to unix using : dos2unix <scriptname.sh> and the run your script in unix ./<scriptname.sh>

Resources