Not Able to run python command inside jenkins pipeline script - jenkins

I am using Jenkins inside a docker container using following command
docker pull jenkins/jenkins
docker run -p 8080:8080 --name=jenkins-master jenkins/jenkins
getting this error
calc.py/var/jenkins_home/workspace/python calculator#tmp/durable-b7e99e01/script.sh: 1: /var/jenkins_home/workspace/python calculator#tmp/durable-b7e99e01/script.sh: calc.py: not found
repository link - -https://github.com/jatin633/Calculator.git
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building the application....'
}
}
stage('Test') {
steps {
echo 'Testing the application....'
git branch: 'python', url: 'https://github.com/jatin633/Calculator.git'
sh 'python calc.py'
sh 'python calculator_unittest.py'
}
}
stage('Deploy') {
steps {
echo 'Deploy the application....'
}
}
}
}

I think the calc.py file is not in root, so maybe try to change:
stage('Test') {
steps {
echo 'Testing the application....'
git branch: 'python', url: 'https://github.com/jatin633/Calculator.git'
sh 'python calc.py'
sh 'python calculator_unittest.py'
}
}
into:
stage('Checkout') {
steps {
echo 'Testing the application....'
git branch: 'python', url: 'https://github.com/jatin633/Calculator.git'
}
}
stage('Run Program') {
dir('sources') {
steps {
sh 'python calc.py'
sh 'python calculator_unittest.py'
}
}
}
Let me know if it helped. Regards!

Related

Jenkins MultiBranch Pipeline: Select to build only 2 specific branches

I have a Jenkins MultiBranch project and I want the test circle to run only on two specific branches on master and on dev. I tried to add on all stages the following
when { anyOf { branch 'master'; branch 'dev' } }
but the only thing I managed to achieve was to deactivate all branch runs
Here is my full pipeline Jenkinsfile
pipeline {
agent any
triggers {
cron('H 0 * * *')
}
options {
disableConcurrentBuilds()
}
stages {
stage('Prepare env') {
when { anyOf { branch 'master'; branch 'dev' } }
steps {
sh 'rm -rf venv'
sh 'rm -rf "${WORKSPACE}/uploads"'
sh 'rm -rf "${WORKSPACE}/downloads"'
sh 'mkdir "${WORKSPACE}/uploads"'
sh 'mkdir "${WORKSPACE}/downloads"'
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE')
{
sh 'docker kill $(docker ps -q)'
sh 'docker rm $(docker ps -a -q)'
sh 'docker volume rm $(docker volume ls -q)'
}
}
}
stage('Start Services') {
when { anyOf { branch 'master'; branch 'dev' } }
steps {
}
}
stage('Test Common') {
when { anyOf { branch 'master'; branch 'dev' } }
steps {
}
}
stage('Test Validations') {
when { anyOf { branch 'master'; branch 'dev' } }
steps {
}
}
stage('Test CSV Issuance') {
when { anyOf { branch 'master'; branch 'dev' } }
steps {
}
}
stage('Test XLS Issuance') {
when { anyOf { branch 'master'; branch 'dev' } }
steps {
}
}
stage('Clean env') {
when { anyOf { branch 'master'; branch 'dev' } }
steps {
sh 'rm -rf venv'
sh 'rm -rf "${WORKSPACE}/uploads"'
sh 'rm -rf "${WORKSPACE}/downloads"'
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE')
{
sh 'docker kill $(docker ps -q)'
sh 'docker rm $(docker ps -a -q)'
sh 'docker volume rm $(docker volume ls -q)'
}
}
}
}
Can you post the full pipeline you have?
You would use the when block on the stage you want run only on the two branches e.g.
pipeline {
agent any
stages
{
stage ("Testing") {
when {
anyOf {
branch 'master'
branch 'dev'
}
}
steps {
echo "run testing"
}
}
stage ("everything") {
steps{
echo "run on all branches"
}
}
}
}
tested pipeline
pipeline {
agent any
stages {
stage("stage") {
when { anyOf { branch 'master'; branch 'dev' } }
steps {
echo "Hello"
}
}
}
}
on master
[Pipeline] stage
[Pipeline] { (stage)
[Pipeline] echo
Hello
[Pipeline] }
[Pipeline] // stage
On Fish
[Pipeline] stage
[Pipeline] { (stage)
Stage "stage" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage

How to run nodeJS tests with Jenkins

I am learning about Jenkins CI and using pipeline to stage my jobs. I've ran into a halt where my tests aren't running. Please take a look at my "Jenkins Images" link. As you can see its stuck in the --coverage table. Typically, if i were to run my tests on my local machine, i would have to enter wcommand to get node to run all tests; however, i don't think it would be the same in a jenkins setting
Jenkins Image
Jenkinsfile
def gv
pipeline {
agent any
tools {nodejs "node"}
parameters {
choice(name: 'VERSION', choices: ['1.1.0', '1.2.0', '1.3.0'], description: '')
booleanParam(name: 'executeTests', defaultValue: true, description: '')
}
stages {
stage("init") {
steps {
script {
gv = load "script.groovy"
CODE_CHANGES = gv.getGitChanges()
}
}
}
stage("build frontend") {
steps {
dir("client") {
sh 'npm install'
echo 'building client'
}
}
}
stage("build backend") {
steps {
dir("server") {
sh 'npm install'
echo 'building server...'
}
}
}
stage("test") {
when {
expression {
script {
env.BRANCH_NAME.toString().equals('feature-jenkins') && CODE_CHANGES == false
}
}
}
steps {
dir("/client") {
sh 'npm test'
echo 'testing application'
}
}
}
stage("deploy") {
steps {
script {
gv.deployApp()
}
}
}
}
}

go: not found in declarative Jenkinsfile

I have Jenkins running on Docker and I have the following Jenkinsfile on github
node {
def root = tool name: 'Go 1.12.6', type: 'go'
ws("${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_ID}/src/github.com/project/repo") {
withEnv(["GOROOT=${root}", "GOPATH=${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_ID}/", "PATH+GO=${root}/bin"]) {
env.PATH="${GOPATH}/bin:$PATH"
stage('Clone repository') {
checkout scm
}
stage('Test repo') {
sh 'go test -v'
}
stage('Build image') {
app = docker.build("docker/repo")
}
stage('Push image') { */
docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') {
app.push("${env.BUILD_NUMBER}")
app.push("latest")
}
}
}
}
}
But no matter what I try I get the following error:
+ go version
/var/jenkins_home/jobs/repo/builds/45/src/github.com/project/repo#tmp/durable-00e72894/script.sh: line 1: go: not found
Jenkins reboots might just fix it

Declare configFileProvider only once in JenkinsFile and reference from all stages

I am trying to set up scripted JenkinsFile for our pipeline automation and would like to use configFileProvider for maven. As such i end up defining this block in all maven stages within the scripted JenkinsFile.
Is there a way to define it just once in the script and reference it across all stages. My sample JenkinsFile as of now looks something like this :-
node {
def mvnHome
def mvnSettings
stage('Prepare') {
mvnHome = tool 'maven-3.5.4'
}
stage('Checkout') {
checkout scm
}
stage('Build'){
configFileProvider(
[configFile(fileId: '**********', variable: 'MAVEN_SETTINGS')]) {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS install"
}
}
stage('Integration Test') {
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean verify"
}
stage('Sonar') {
configFileProvider(
[configFile(fileId: '**********', variable: 'MAVEN_SETTINGS')]) {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS sonar:sonar"
}
}
stage('Packaging') {
configFileProvider(
[configFile(fileId: '**********', variable: 'MAVEN_SETTINGS')]) {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS package"
}
}
stage('Deploy') {
configFileProvider(
[configFile(fileId: '**********', variable: 'MAVEN_SETTINGS')]) {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy}"
}
}
}
Any help or suggestions here would be greatly appreciated as always.
Cheers,
Ashley
Since it is scripted you don't have to follow a rigid structure and, although not pretty, you can change the order and put the stages inside the configFileProvider:
configFileProvider(
[configFile(fileId: '**********', variable: 'MAVEN_SETTINGS')]) {
stage('Build'){
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS install"
}
stage('Integration Test') {
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean verify"
}
stage('Sonar') {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS sonar:sonar"
}
stage('Packaging') {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS package"
}
stage('Deploy') {
sh "'${mvnHome}/bin/mvn' -s $MAVEN_SETTINGS deploy}"
}
}
Best,
André

Jenkins - Parallel stages with docker for PHP

I'm new with Jenkins, and I would like to get almost the same behavior as in GitLab CI for one of my PHP project.
I use Docker to test my project on several PHP versions.
What I want ?
Running build and test in parallel.
Build creates my application, sources come from a git repository, and I run the composer install command.
Dockerfile is stored in /var/lib/jenkins/Docker
My Dockerfile has a parameter (PHP_VERSION) which allow me to choose the PHP version I want
customWorkspace seems to work
Here is my Jenkinsfile to do so :
updateGitlabCommitStatus name: 'build', state: 'pending'
pipeline {
agent none
post {
failure {
updateGitlabCommitStatus name: 'build', state: 'failed'
}
success {
updateGitlabCommitStatus name: 'build', state: 'success'
}
}
stages {
stage('build') {
parallel {
stage('build-php5.4') {
agent {
dockerfile {
additionalBuildArgs '--build-arg PHP_VERSION=54'
dir '/var/lib/jenkins/Docker'
customWorkspace './build-php5.4'
}
}
steps {
sh 'pwd'
sh 'ls'
sh 'rm -Rf composer.lock vendor'
sh 'composer install'
}
}
stage('build-php7.0') {
agent {
dockerfile {
additionalBuildArgs '--build-arg PHP_VERSION=70'
dir '/var/lib/jenkins/Docker'
customWorkspace './build-php7.0'
}
}
steps {
sh 'pwd'
sh 'rm -Rf composer.lock vendor'
sh 'composer install'
}
}
}
}
stage('tests') {
parallel {
stage('test-php5.4') {
agent {
dockerfile {
additionalBuildArgs '--build-arg PHP_VERSION=54'
dir '/var/lib/jenkins/Docker'
customWorkspace './build-php5.4'
}
}
steps {
sh 'pwd'
sh 'php --version'
sh 'php vendor/phpunit/phpunit/phpunit tests'
}
}
stage('test-php7.0') {
agent {
dockerfile {
additionalBuildArgs '--build-arg PHP_VERSION=70'
dir '/var/lib/jenkins/Docker'
customWorkspace './build-php7.0'
}
}
steps {
sh 'pwd'
sh 'php --version'
sh 'php vendor/phpunit/phpunit/phpunit tests'
}
}
}
}
}
}
And here is the result :
It looks good, but it isn't, and I don't really understand the underlying behavior.
As you can see, the test-php54 stage uses the last created Docker container :
I'm sure I'm wrong on a lot of steps, but do you think I can do it this way ?
Ok, I found the main problem.
The fact is I use the same Dockerfile, but with different parameters.
If I create one Dockerfile for PHP 5.4 and another one for PHP 7.0, the stages are parallelized correctly.
updateGitlabCommitStatus name: 'build', state: 'pending'
pipeline {
agent none
post {
failure {
updateGitlabCommitStatus name: 'build', state: 'failed'
}
success {
updateGitlabCommitStatus name: 'build', state: 'success'
}
}
stages {
stage('build') {
parallel {
stage('build-php5.4') {
agent {
dockerfile {
dir '/var/lib/jenkins/Docker'
filename 'Dockerfile-php5.4'
customWorkspace './build-php5.4'
}
}
steps {
sh 'rm -Rf composer.lock vendor'
sh 'composer install'
}
}
stage('build-php7.0') {
agent {
dockerfile {
dir '/var/lib/jenkins/Docker'
filename 'Dockerfile-php7.0'
customWorkspace './build-php7.0'
}
}
steps {
sh 'rm -Rf composer.lock vendor'
sh 'composer install'
}
}
}
}
stage('tests') {
parallel {
stage('test-php5.4') {
agent {
dockerfile {
dir '/var/lib/jenkins/Docker'
filename 'Dockerfile-php5.4'
customWorkspace './build-php5.4'
}
}
steps {
sh 'php --version'
sh 'php vendor/phpunit/phpunit/phpunit tests'
}
}
stage('test-php7.0') {
agent {
dockerfile {
dir '/var/lib/jenkins/Docker'
filename 'Dockerfile-php7.0'
customWorkspace './build-php7.0'
}
}
steps {
sh 'php --version'
sh 'php vendor/phpunit/phpunit/phpunit tests'
}
}
}
}
}
}
This seems to work ! :)

Resources