How to use Docker-compose on Jenkins? - jenkins

I want to use docker-compose on Jenkins. But I faced a problem.
Jenkins showed me that log. How can I solve this problem??
I created docker-compose.yml and I used jenkins/blueocean image.
Also, I installed docker-compose in Jenkins container.
pipeline {
agent any
environment {
REGESTRY = 'localhost:5000'
}
parameters {
string(name: 'Person', defaultValue: 'Mr Jenkins', description: 'description')
}
stages {
stage('Build composeImage'){
steps{
sh 'ls -al'
sh 'cat docker-compose.yml'
sh 'docker-compose -v'
sh 'docker-compose build'
}
}
stage('Publish images'){
steps{
sh 'docker push $REGESTRY/client'
sh 'docker push $REGESTRY/server'
}
}
}
}
docker-compose build
Can't find a suitable configuration file in this directory or any
parent. Are you in the right directory?
Supported filenames: docker-compose.yml, docker-compose.yaml
script returned exit code 1

Related

Jenkins build failed: gradlew: not found

I want to configure Jenkins 2.375.2 to build gradle project. But when I configure the pipe using Blue Ocean plugin and I run the pipeline I get error:
+ ./gradlew build
/var/lib/jenkins/workspace/jenkins_master#tmp/durable-dcccf1cd/script.sh: 1: ./gradlew: not found
Jenkins file:
pipeline {
agent any
stages {
stage('Build Image') {
steps {
sh "echo 'building..'"
// configure credentials under http://192.168.1.28:8080/user/test/credentials/ and put credentials ID
git credentialsId: '8f6bc3ab-9ef5-4d89-8e14-4972d63325c5 ', url: 'http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git', branch: 'master'
// execute Java -jar ... and build docker image
sh './gradlew build'
sh 'docker build -t springio/gs-spring-boot-docker .'
}
}
}
I tried to add Gradle config
But still I get the same error. Do you know how I can fix this issue?

Error when running any docker command by Jenkins on pipeline : /home are not currently supported

I am running jenkins and docker on Ubuntu server 20.04 LTS.
when starting jenkins pipeline to run docker commands, I have this error:
Sorry, home directories outside of /home are not currently supported.
See https://forum.snapcraft.io/t/11209 for details.
script returned exit code 1
Can anyone help me ? Thank you!
the script is below
pipeline {
agent any
tools {
nodejs "nodejs"
gradle 'gradle'
}
stages {
stage('clean'){
steps{
sh 'docker-compose down'
step([$class: 'WsCleanup'])
}
}
stage('clone'){
steps{
git branch: 'develop', credentialsId: 'key', url: 'url'
}
}
stage('front_build'){
steps{
dir('frontend'){
sh 'npm install'
sh 'npm run build'
}
}
}
stage('back_build'){
steps{
dir('backend'){
sh 'gradle clean'
sh 'gradle build'
}
}
}
stage('deploy'){
steps{
sh 'docker-compose up --build'
}
}
}
}

How to use helm commands in jenkins pipeline script

I have been trying to deploy the image built on jenkins by docker to helm charts, i have referred couple of documents on website https://dev.to/sword-health/seamless-ci-cd-with-jenkins-helm-and-kubernetes-5e00
and https://cloudcompilerr.wordpress.com/2018/06/03/docker-jenkins-kubernetes-run-jenkins-on-kubernetes-cluster/ and managed till the point where docker image gets pushed into dockerhub but i get stuck at helm
i'm not getting what the error exactly is.
JENKINS ERROR
+ helm list
/var/lib/jenkins/workspace/01#tmp/durable-68e91f76/script.sh: 1: /var/lib/jenkins/workspace/01#tmp/durable-68e91f76/script.sh: helm: not found
PIPELINESCRIPT
pipeline {
environment {
registry = "hemanthpeddi/springboot"
registryCredential = 'dockerhub'
}
agent any
tools {maven "maven" }
stages {
stage('Cloning Git') {
steps {
git 'https://github.com/hrmanth/game-of-life.git'
}
}
stage('Build'){
steps{
sh script: 'mvn clean package'
}
}
stage('Building image') {
steps{
script {
dockerImage = docker.build registry + ":$BUILD_NUMBER"
}
}
}
stage('Deploy Image') {
steps{
script {
docker.withRegistry( '', registryCredential ) {
dockerImage.push()
}
}
}
}
stage('Remove Unused docker image') {
steps{
sh "docker rmi $registry:$BUILD_NUMBER"
}
}
stage('Run Helm') {
steps {
script {
container('helm') {
sh "helm ls"
}
}
}
}
}
}
Is there any specific configuration that i'm missing before i use helm in jenkins? And i have configured my kubernetes IP in the cloud configuration in jenkins, Please help
Plugins Installed
Kubernetes Plugin
Docker Plugin
You need helm, it is not available by default. You could add helm as a tool in Jenkins and use it.
https://www.jenkins.io/doc/book/pipeline/syntax/#tools
you can install helm in the container itself by adding an extra stage
stage("install helm"){
steps{
sh 'wget https://get.helm.sh/helm-v3.6.1-linux-amd64.tar.gz'
sh 'ls -a'
sh 'tar -xvzf helm-v3.6.1-linux-amd64.tar.gz'
sh 'sudo cp linux-amd64/helm /usr/bin'
sh 'helm version'
}
}
I am not so familiar with that, but when you are using the "container('helm')" step, I think it refers to
Kubernetes Plugin.
So, reading this docs, I think that the "podTemplate" is missing in your configuration.
Thus what you need to do is to configure a Helm container in the "podTemplate" and put the name "helm". You can try to use, for example, the "alpine/helm" image.
See you later.

How to pass env vars into the docker container in Jenkins?

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'
}

How do I specify docker run args when using a dockerfile agent in jenkins

I am setting up a simple jenkins pipeline with a dockerfile agent, Jenkinsfile as follows:
pipeline {
agent {
dockerfile {
dir 'docker'
args '-v yarn_cache:usr/local/share/.cache/yarn'
}
}
environment {
CI = 'true'
}
stages {
stage('Build') {
steps {
sh 'yarn install'
sh 'yarn run build'
}
}
stage('Test') {
steps {
sh 'yarn run test'
}
}
}
}
I would like the yarn cache persist in a volume so I want the image to be started with '-v yarn_cache:usr/local/share/.cache/yarn'.
With the given Jenkinsfile, jenkins stalls after creating the image.
The args parameter is not actually documentented for the dockerfile agent but for the docker agent.
Do I really have to use a predefined (and uploaded) image just to be able to use parameters ?
Cheers Thomas
OK, figured it out:
It actually works just like I configured it only I have forgotten the leading / in the volume path. So with
args `'-v yarn_cache:/usr/local/share/.cache/yarn'`
it works just fine..

Resources