Declarative jenkins pipeline

3/12/2019

I am using declarative Jenkins pipeline. I am newbie in Jenkins so i don't understand something. I don't know how to handle this error and can someone tell me what is best options to do builds.

I have bash script which build, tag and push docker image to repository. This is the part of my Jenkinsfile;

pipeline {
  agent {
    kubernetes {
      label 'bmf-worker'
      defaultContainer 'jnlp'
      yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
  component: ci
spec:
  # Use service account that can deploy to all namespaces
  serviceAccountName: service-reader
  containers:
  - name: docker
    image: docker
    command:
    - cat
    tty: true
  - name: kubectl
    image: gcr.io/cloud-builders/kubectl
    command:
    - cat
    tty: true
  - name: gcloud 
    image: google/cloud-sdk
    command: 
    - cat
    tty: true
"""
}
  }
  stages {
    stage('Code Checkout and Setup') {
      steps {
        echo 'Code Checkout and Setup'


      }
    }
    stage('Build') {
      parallel { 
        stage('Build') {
          steps {
            echo 'Start building Frontend and Backend Docker images'
          }
        }
        stage('Build BMF Frontend') {
          steps {
            container('gcloud') {
            echo 'Building Bmf Frontend Image'
            sh 'chmod +x build.sh'
            sh './build.sh --build_bmf_frontend'
          }
          }
        }
        stage('Tag BMF Frontend') {
          steps {
            container('gcloud') {
            echo 'Building Bmf Frontend Image'
            sh 'chmod +x build.sh'
            sh './build.sh --tag_frontend'
          }
          }
        }
        stage('Build BMF Backend') {
          steps {
            container('gcloud') {
            echo 'Buildinging Bmf Backend Images'
            sh 'chmod +x build.sh'
            sh './build.sh --build_bmf_backend'
          }
          }
        }
        stage('Tag BMF Backend') {
          steps {
            container('gcloud') {
            echo 'Building Bmf Frontend Image'
            sh 'chmod +x build.sh'
            sh './build.sh --tag_frontend'
          }
          }
        }
      }
    }

How to use podTemplate to execute my steps. When am using docker container for Stage Build BMF Backend i have these errors;

  1. Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
  2. /home/jenkins/workspace/BMF/bmf-web@tmp/durable-c146e810/script.sh: line 1: ./build.sh: not found

With gcloud container defined in podTemplate;

  1. time="2019-03-12T13:40:56Z" level=error msg="failed to dial gRPC: cannot connect to the Docker daemon. Is 'docker daemon' running on this host?: dial unix /var/run/docker.sock: connect: no such file or directory"

  2. Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

And how to tag docker images because I need docker to tag and git because tags is short commit. When am using docker there is no git.

My Jenkins master is on Google Cloud Kubernetes.

Can someone explain me better solution to execute jobs.

-- Mirke
jenkins
jenkins-pipeline
kubernetes

1 Answer

3/12/2019

The first issue you are facing is related to Docker, not Jenkins.

Docker commands can only be run by root, or by users in the docker group.

If you want the Jenkins user to be able to execute Docker commands then you can run the following command as root to add Jenkins to the Docker group:

usermod -aG docker jenkins

This is documented in the Docker docs.

Be aware that giving a user access to Docker effectively grants them root access, so be cautious of which users you add to this group.

-- JShorthouse
Source: StackOverflow