In my application I have a build script in package.json.
The build makes dist folder and inside I have my application.
I set Jenkins master and Jenkins agent as say in boxboat setup jenkins with docker and watch the video in youtube.
But now after I did this, I don't think my bash commands running inside a container.
I want to clone the repo and run npm i and npm run build - inside the docker container.
How I modify this configuration to able to do that?
throttle(['throttleDocker']) {
node('docker') {
wrap([$class: 'AnsiColorBuildWrapper']) {
try{
stage('Build') {
checkout scm
sh '''
echo "in Setup"
docker ps -a
echo "after docker"
# ./ci/docker-down.sh
# ./ci/docker-up.sh
'''
}
stage('Test'){
parallel (
"unit": {
sh '''
echo "in unit"
# ./ci/test/unit.sh
'''
},
"functional": {
sh '''
echo "in functional"
# ./ci/test/functional.sh
'''
}
)
}
stage('Capacity Test') {
sh '''
echo "in Capacity Test"
# ./ci/test/stress.sh
'''
}
}
finally {
stage('Cleanup') {
sh '''
echo "in Cleanup"
# ./ci/docker-down.sh
'''
}
}
}
}
}
I tried to this codes but they don't work. I also add agent after try.
stage('Build') {
agent {
docker {
label 'docker'
image 'node:latest'
}
}
steps {
checkout scm
sh 'node -v'
}
...
You can try below scripted pipeline
node {
docker.image('yourimage').inside {
stage('Build'){
sh 'echo "Build stage inside container"'
}
stage('Test'){
sh 'echo "Test Stage inside container"'
}
}
}
Related
I run my Jenkins build inside a docker container (node:latest).
But the enviroments variables are not defiend in the container:
$GIT_BRANCH, $GIT_COMMIT
So I got this error:
GitHub has been notified of this commit’s build result
groovy.lang.MissingPropertyException: No such property: GIT_BRANCH for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
I have a lot of variables I need to pass into the container. how to do that with Jenkins?
I looking for solution that inherit all environment variables that exist in Jenkins process and my host machine/docker
Here my Jenkinsfile:
throttle(['throttleDocker']) {
node('docker') {
wrap([$class: 'AnsiColorBuildWrapper']) {
try{
docker.image('node:latest').inside {
stage('Checkout SCM'){
checkout scm
}
stage('PS'){
sh 'node -v'
sh 'ls'
}
stage('Verify Branch') {
echo "$GIT_BRANCH"
echo "$GIT_COMMIT"
}
stage('Build'){
sh "npm run build"
sh 'ls'
}
stage('Test'){
sh 'echo "Test Stage inside container."'
}
}
Maybe this
node ('linux-slave') {
withEnv(['PATH=/usr/local/Cellar/coreutils/8.25/libexec/gnubin:/usr/local/Cellar/gnu-sed/4.2.2/libexec/gnubin:/Users/fbelzunc/cloudbees/support/support-shinobi-tools:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home//bin:/Users/fbelzunc/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:/usr/local/sbin:/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.6.5/bin:/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/beaker-1.16.0/bin:/usr/local/Cellar/ruby/2.1.0/bin/:/path/testing']) {
docker.image('maven:3.3.3-jdk-8').inside {
sh 'echo $PATH'
sh 'mvn --version'
}
}
sh 'echo $PATH'
}
I have a Jenkin DSL JOB. It's for java build. I am stuck in a strange problem.
jobname is DSL, I saw a workspace with the name of DSL is created, But when the job runs it added another workspace with the name of DSL#2. The problem I can not get final jar file from DSL workspace
pipeline
{
agent any
stages
{
stage('Build')
{
agent {
docker { image 'maven:latest'
args '-v /home/ubuntu/jenkins/jenkins_home/.m2:/root/.m2'
}
}
steps {
git branch: "${params.branch}", url: "git#github.org/repo.git"
sh 'mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true'
sh "ls -la target/name.jar "
}
}
stage('Copy Artifects')
{
steps {
//print "$params.IP"
// sh '${params.IP}"
sh "ls -la && pwd "
sh "scp target/name.jar ubuntu#${params.IP}:/home/ubuntu/target/name.jar_2"
}
}
}
}
OUT Of the JOB
Compiling 19 source files to /var/jenkins_home/workspace/dsl#2/auth-client/target/classes
DSL#2 means you either have a concurrent job configured and two builds runnning at the same time, OR you got a bug https://issues.jenkins-ci.org/browse/JENKINS-30231
To address your issue:
you are building stage('Build') inside a docker container created from maven image.
However, stage('Copy Artifects') is run OUTSIDE of that container
To fix it, you need to move agent{} to pipeline{} level like this:
pipeline
{
agent {
docker {
image 'maven:latest'
args '-v /home/ubuntu/jenkins/jenkins_home/.m2:/root/.m2'
}
}
stages
{
stage('Build')
{
steps {
git branch: "${params.branch}", url: "git#github.org/repo.git"
sh 'mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true'
sh "ls -la target/name.jar "
}
}
stage('Copy Artifects')
{
steps {
sh "ls -la && pwd "
sh "scp target/name.jar ubuntu#${params.IP}:/home/ubuntu/target/name.jar_2"
}
}
}
}
I have this Jenkinsfile:
#!groovy
pipeline
{
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
}
agent {
label 'docker && new'
}
stages
{
stage('Docker build')
{
when {
branch 'dev'
}
steps
{
sh "echo ${env.BUILD_NUMBER}"
sh "./scripts/push.sh Docker http://xxxxx.xxxx ${env.BUILD_NUMBER} ${env.GIT_BRANCH}"
sh "echo ${env.BUILD_NUMBER}"
sh "echo ${env.GIT_BRANCH}"
}
}
stage("Initialise")
{
agent {
dockerfile {
filename 'Dockerfile'
label 'docker && new'
args '--entrypoint ""'
}
}
steps {
sh "terraform init -input=false"
}
}
stage("WorkspaceDev") {
agent {
dockerfile {
filename 'Dockerfile'
label 'docker && new'
args '--entrypoint ""'
}
}
when {
branch 'dev'
}
steps {
sh "terraform workspace select dev || terraform workspace new dev"
}
}
}
}
It builds a container from my Dockerfile, However when running this job it is creating a new docker container to run the next stage called WorkspaceDev. I need to use a separate agent for the very first stage and then dockerfile agent for all other stages
How can I use the same container built for the Initialise stage?
Problem:
When running this pipeline the "Docker build" stage is executing on the agent itself as expected.
It then gets to the "initialisation" stage. This build a new docker container (docker build (my Dockerfile I have specified in the agent section for this stage). This stage completes inside this container.
Next it gets to the "WorkspaceDev" stage - this then AGAIN rebuilds the container with docker build.
I want to use the same container built in the "Initialisation" stage
Can't you use agent { label 'NODE_LABEL' }?
Your question is not very clear, but it seem you are defining a global agent in the upper part of your Jenkinsfile. If the stage you are trying to run on a different agent is the "Docker build" stage, you just have to specify an agent like in your other stages.
There is the agent documentation:
https://jenkins.io/doc/book/pipeline/syntax/#agent
I think that the label has no meaning (so it is ignored) when you're using a Dockerfile-agent, so it creates a new container for each stage.
therefore, try the following:
#!groovy
pipeline
{
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
}
agent {
dockerfile {
filename 'Dockerfile'
args '--entrypoint ""'
}
}
stages
{
stage('Docker build')
{
agent {
dockerfile {
filename 'Dockerfile'
args '--entrypoint ""'
}
}
when {
branch 'dev'
}
steps
{
sh "echo ${env.BUILD_NUMBER}"
sh "./scripts/push.sh Docker http://xxxxx.xxxx ${env.BUILD_NUMBER} ${env.GIT_BRANCH}"
sh "echo ${env.BUILD_NUMBER}"
sh "echo ${env.GIT_BRANCH}"
}
}
stage("Initialise")
{
steps {
sh "terraform init -input=false"
}
}
stage("WorkspaceDev") {
when {
branch 'dev'
}
steps {
sh "terraform workspace select dev || terraform workspace new dev"
}
}
}
}
I am running jenkins master and slave as docker container. I have setup a slave node using jenkins/ssh-slave image with label 'worker'. I can successfully run my pipeline on the worker node. However, when I am trying to run docker build command using the Jenkinsfile, I am getting error docker: not found.
pipeline {
agent { label 'worker' }
tools {nodejs "node"}
stages {
stage ('Build APP') {
steps {
echo 'BUILDING APPLICATION'
sh 'npm install'
}
}
stage ('Create Package') {
steps {
script{
echo 'BUILDING DOCKER IMAGE'
docker.build("package${env.BUILD_NUMBER}")
}
}
}
stage('Package Test') {
agent { docker }
steps {
echo 'RUNNING IMAGE IN CONATAINER'
sh "docker run -p 5050:4000 -d package${env.BUILD_NUMBER}"
echo 'CHECKING HEALTH STATUS'
script {
try {
sh "curl -s --head --request GET http://127.0.0.1:5050/ | grep '200'"
echo 'Health Check Passed!'
} catch(Exception e) {
echo "Health Check Failed!"
}
}
}
}
In the third step 'package test' I have placed agent docker in the file but it doesn't seem to work. How can I place agent docker in a declarative pipeline?
I am creating a Jenkin Pipeline for below task.
Pull the latest code from vsts
Build the code and create .jar file out of it
creating a Docker image on the basis of the jar
tag the image
push the image into Docker registry
for this, I have written below Jenkinsfile
pipeline {
agent {
label "master"
}
stages {
stage('Build') {
steps {
echo '..........................Building Jar..........................'
sh 'npm install'
}
}
stage('Build-Image') {
steps {
echo '..........................Building Image..........................'
sh 'sudo docker build -t some-org/admin-portal:v0.1 --build-arg PORT=9007 --build-arg ENVIRONMENT=develop .'
}
}
stage('Tag-Image') {
steps {
echo '..........................Taging Image..........................'
sh 'sudo docker login some-repo -u username001 -p password'
sh 'sudo docker tag some-org/admin-portal:v0.1 some.dtr.io/some-org/admin-portal:v0.1'
}
}
stage('Push-Image') {
steps {
echo '..........................Pushing Image..........................'
sh 'sudo docker push some.dtr.io/some-org/admin-portal:v0.1'
}
}
}
}
Below is Jenkins job configuration snapshot for Pipeline
My Question is how can I change the agent label depending upon branch name or some conditions.
e.g if the branch is develop I want to use slave1 node and if the branch is production I want to use master
Any Help will be appreciable.
Thanks in Advance.
You can assign the agent labels inside the stage, so that you can execute the stages with required agents.
eg:
pipeline {
agent none
stages {
stage('Build') {
agent {
label "master"
}
steps {
echo '..........................Building Jar..........................'
sh 'npm install'
}
}
stage('Build-Image') {
agent {
label "master"
}
steps {
echo '..........................Building Image..........................'
sh 'sudo docker build -t some-org/admin-portal:v0.1 --build-arg PORT=9007 --build-arg ENVIRONMENT=develop .'
}
}
stage('Tag-Image') {
agent {
label "slave1"
}
steps {
echo '..........................Taging Image..........................'
sh 'sudo docker login some-repo -u username001 -p password'
sh 'sudo docker tag some-org/admin-portal:v0.1 some.dtr.io/some-org/admin-portal:v0.1'
}
}
stage('Push-Image') {
agent {
label "slave1"
}
steps {
echo '..........................Pushing Image..........................'
sh 'sudo docker push some.dtr.io/some-org/admin-portal:v0.1'
}
}
}
}