Home > Software design >  Best way to change a docker image tag in a azure pipeline with kubernetes
Best way to change a docker image tag in a azure pipeline with kubernetes

Time:11-14

I have my azure pipeline doing a kubernetes@1 task with my kubernetes config:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tennant-service-deployment
  labels:
    app: tennant-service
spec:
  replicas: 3
  selector:
    matchLabels:
      app: tennant-service
  template:
    metadata:
      labels:
        app: tennant-service
    spec:
      containers:
        - name: tennant-service
          image: cronesharedwesteurope.azurecr.io/tennant-service:dev
          ports:
            - containerPort: 80   
      imagePullSecrets:
        - name: registrysecret

You can see that at the moment my image is hard coded in my yaml file (tennant-service:dev).

I want the user to tell me witha azure devops pipeline parameter which version he wants (e.g, dev / 1.0 / latest). But because the kubernetes is a yaml config file, I can't change it with a parameter, can I? Are there any tools to do exactly this?

CodePudding user response:

This is what we do at work,

  1. Create a build pipeline that builds the docker image with a tag, and the tag is the pipeline build number, e.g. #1.6.1-TenentServiceImage. (Assume that you have already pushed the image to ACR during the build process).

  2. In your deployment YAML pipeline, add a resource,

       resources:
        pipelines:
         - pipeline: TenentServiceImage
           source: Tenent Service Image
           branch: master
    
  3. In the same YAML, now you can reference the resource in your image path,

     image: cronesharedwesteurope.azurecr.io/tennant-service:$(resources.pipeline.TenentServiceImage.runName)
    
  4. When your devs try to deploy the container, they click Run and should see a new selection dropdown Resource and select the build they want to deploy. enter image description here

CodePudding user response:

Microsoft has some good documentation on setting up an Azure DevOps pipeline for Kubernetes containers. The example features use of the KubernetesManifest@0 task in the pipeline allowing to specify the container tag with a pipeline variable or parameter.

  • Related