yatta
yatta

Reputation: 519

How to version Docker images on Jenkins and pass it to the Kubernetes YAML files

Basically I'm trying to create a pipeline on my local Jenkins, create an image and then send to Docker Hub. Then I'll deploy this image on our local Kubernetes(Server IP:10.10.10.4).

So Jenkins pipeline script is below;

docker build -t test123/demo:$BUILD_NUMBER . 
docker login --username=testuser --password=testpass
docker push test123/demo:$BUILD_NUMBER
ssh -tt [email protected] 'kubectl apply -f hybris-deployment.yaml'

So the problem is; I can tag succesfully images with $BUILD_NUMBER and push to Docker hub. Then I have to use this $BUILD_NUMBER in the Kubernetes server's YAML file and deploy it.

But I can't pass this $BUILD_NUMBER to the Kubernetes server. Somehow I should send this build number with the ssh command and use it in the YAML file as a tag.

Any idea how can I do that? Thanks!

Upvotes: 3

Views: 1977

Answers (1)

Harsh Manvar
Harsh Manvar

Reputation: 30113

you create your pipeline something like

if you deployment.yaml file is like

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: test-image
  labels:
    app: test-image
spec:
  selector:
    matchLabels:
      app: test-image
      tier: frontend
  strategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: test-image
        tier: frontend
    spec:
      containers:
      - image: TEST_IMAGE_NAME
        name: test-image
        ports:
        - containerPort: 8080
          name: http
        - containerPort: 443
          name: https

in pipeline, you can change the image and replace this part in deployment.yaml

sed -i "s,TEST_IMAGE_NAME,gcr.io/$PROJECT_ID/$REPO_NAME/$BRANCH_NAME:$SHORT_SHA," deployment.yaml

so this command will replace line, in deployment.yaml and this way image URL updated and you can apply the YAML file.

you can see the example jenkin file here : https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes/blob/master/sample-app/Jenkinsfile

whole project link : https://github.com/GoogleCloudPlatform/continuous-deployment-on-kubernetes

Upvotes: 4

Related Questions