Bitbucket jenkin kubernetes CI/CD

11/25/2019

Can anyone share Jenkin file or steps to create CI/CD for bitbucket private repository to jenkin.

Build docker images and upload to docker hub and deploy to kubernetes.

i have try downloading plugin for bit bukcet no help so far.

I have not much-used Jenkin pipeline i have used freestyle project not much experience on Jenkin help appreciated.

pipeline {

  environment {
    registry = "*******/test_case"
    registryCredential = 'dockerhub'
  }
  agent any
  stages {
    stage('Cloning Git') {
  steps {
    git 'https://harshmanvar@bitbucket.org/harshmanvar/configuring-ci-cd-on-kubernetes-with-jenkins.git'
  }
}

    stage('Docker Build') {
      steps {
        sh "docker build -t ****/test_case:${env.BUILD_NUMBER} ."
      }
    }
    stage('Docker Push') {
      steps {
        withCredentials([usernamePassword(credentialsId: 'dockerhub', passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) {
          sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPassword}"
          sh "docker push ******/test_case:${env.BUILD_NUMBER}"
        }
      }
    }
    stage('Docker Remove Image') {
      steps {
        sh "docker rmi ********/test_case:${env.BUILD_NUMBER}"
      }
    }
    stage('Apply Kubernetes Files') {
      steps {
          withKubeConfig([credentialsId: 'kubeconfig']) {
          sh 'cat deployment.yaml | sed "s/{{BUILD_NUMBER}}/$BUILD_NUMBER/g" | kubectl apply -f -'
          sh 'kubectl apply -f service.yaml'
        }
      }
  }
}
post {
    success {
      slackSend(message: "Pipeline is successfully completed.")
    }
    failure {
      slackSend(message: "Pipeline failed. Please check the logs.")
    }
}
}

in first step it's not cloing repo while my repo is open not private.

Thanks

-- Harsh Manvar
docker
git
jenkins
kubernetes

2 Answers

11/26/2019

Whether your git repository private or not, just setup your private access token in git developer setting and add it to the Jenkins credential. After that try this script cloning Git stage:

stage('Cloning Git') {   
  steps {
    git credentialsId:'Your_Credential_Id', 
    url:'https://harshmanvar@bitbucket.org/harshmanvar/configuring-ci-cd-on-kubernetes-with-jenkins.git', branch:'master'
   }}

I hope this will be helpful to you.!

-- Hansika Wanniarachchi
Source: StackOverflow

11/26/2019

You should checkout to your commit which was pushed to bitbucket instead of cloning manually what i did was this you can read more about it in official docs related to checkout, here is my first step

  stage('checkout scm'){
            script {
                try {
                    def scmVars = checkout scm
                    echo "$scmVars"
                    bitbucketStatusNotify(buildState: 'INPROGRESS', credentialsId: 'OAuth_Bit_Notify' )
                } catch(err) {
                    bitbucketStatusNotify(buildState: 'FAILED',  buildDescription: 'unable to clone repo with submodule', credentialsId: 'OAuth_Bit_Notify' )
                }
            }
        }

bitbucketStatusNotify is a plugin used over here to send status to bitbucket, hope it helps

-- Mohsin Amjad
Source: StackOverflow