replace the image value in kubernetes yaml.
Asked by Reethu:

Hi,

I have created a bitbucket pipeline like below and now able to push image to container registry with build number as tag.

But failing to pull the same image in kubernetes deployment yml.  

**pipeline.yml**

steps:  
- step: \&Build  
name: Build and publish docker image.  
services:  
- docker # Enable Docker for your repository  
image: maven:3.6.1-jdk-8  
script:  
- mvn -DskipTests -B package # -B batch mode makes Maven less verbose  
- export IMAGE_NAME=$REGISTRY_NON_PROD/$SERVICE_NAME:${BITBUCKET_BUILD_NUMBER}  
- docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_PASSWORD $REGISTRY_NON_PROD  
- docker build -t $REGISTRY_NON_PROD/$SERVICE_NAME:${BITBUCKET_BUILD_NUMBER} .  
- docker push $IMAGE_NAME

- step: \&Deploy  
image:  
name: atlassian/default-image:2  
name: Deploy Image  
trigger: manual  
script:  
- sed -i "s\|{{image}}\|$IMAGE_NAME\|g" aks-deployment.yaml  
- pipe: microsoft/azure-aks-deploy:1.0.2  
variables:  
AZURE_APP_ID: $AZURE_APP_ID  
AZURE_PASSWORD: $AZURE_PASSWORD  
AZURE_TENANT_ID: $AZURE_TENANT_ID  
AZURE_AKS_NAME: $AZURE_AKS_NAME  
AZURE_RESOURCE_GROUP: $AZURE_RESOURCE_GROUP  
KUBECTL_COMMAND: 'apply'  
KUBECTL_ARGUMENTS: '-f aks-deployment.yaml'  

<br />

<br />

<br />

**aks-deployment.yaml** containers:  
- name: containername  
image: {{image}}  
imagePullPolicy: IfNotPresent  

on trying to deploy this am getting error like   

'The CronJob "jobname" is invalid: spec.jobTemplate.spec.template.spec.containers\[0\].image: Required value'  

Can someone please help with this.  
Thanks in advance !  


Answers:
author: Oleksandr Kyrdan
body: Hi [@Reethu](/t5/user/viewprofilepage/user-id/4435945)

Thank you for your question!

To make image name flexible try [envsubst](https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html) (the program substitutes the values of environment variables) instead:

```
- envsubst < aks-deployment.yaml.template > aks-deployment.yaml
```

Add to your repository template **aks-deployment.yaml.template** file:  

```
containers:
    - name: containername
image: ${IMAGE_NAME}
imagePullPolicy: IfNotPresent
```

**Note! Make sure that ${IMAGE_NAME} exists in the step environment.**

Also, good example provided in [example-google-gke-kubectl-run repository.](https://bitbucket.org/bitbucketpipelines/example-google-gke-kubectl-run/src/eb38fb8f620c734fddfb844e6af7e250c1ace617/bitbucket-pipelines.yml#lines-20)

Cheers,  
Oleksandr Kyrdan

comments: author: Oleksandr Kyrdan
body: Note! For future maintenance purposes we have forked the microsoft/azure-aks-deploy pipe from <https://bitbucket.org/microsoft/azure-aks-deploy>.

Now it is available as [**atlassian/azure-aks-deploy:1.0.2**](https://bitbucket.org/atlassian/azure-aks-deploy):

```
script:
  - pipe: atlassian/azure-aks-deploy:1.0.2
    variables:
      AZURE_APP_ID: $AZURE_APP_ID
      AZURE_PASSWORD: $AZURE_PASSWORD
      AZURE_TENANT_ID: $AZURE_TENANT_ID
      AZURE_AKS_NAME: 'my-cluster'
      AZURE_RESOURCE_GROUP: 'my-resource-group'
      KUBECTL_COMMAND: 'version'
```

To discover more pipes, visit our [**Bitbucket Pipes Marketplace**](https://bitbucket.org/product/features/pipelines/integrations).


author: Reethu
body: Thanks Oleksandr .  

- step: \&Deploy  
image:  
name: atlassian/default-image:2  
name: Deploy Image  
trigger: manual  
script:  
- export Build_Id=${BITBUCKET_BUILD_NUMBER}  
- envsubst \< aks-deployment.yaml \> aks.yaml  
- pipe: atlassian/azure-aks-deploy:1.0.2  
variables:  
AZURE_APP_ID: $AZURE_APP_ID  
AZURE_PASSWORD: $AZURE_PASSWORD  
AZURE_TENANT_ID: $AZURE_TENANT_ID  
AZURE_AKS_NAME: $AZURE_AKS_NAME  
AZURE_RESOURCE_GROUP: $AZURE_RESOURCE_GROUP  
KUBECTL_COMMAND: 'apply'  
KUBECTL_ARGUMENTS: '-f aks.yaml'  
DEBUG: 'true'  

<br />

<br />

I have aks-deployment.yaml in my repository with the variable ${Build_Id}  


author: Oleksandr Kyrdan
body: Yep, it's better to create the template file in your repository.

You don't need destination file anymore.

Destination file will be generated automatically on each build.


author: Reethu
body: Thanks Oleksandr. I was not aware of the fact that Destination file will be generated automatically on each build.  

It worked for me now !!


Tags: bitbucket-pipelines

azure

kubernetes