Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

replace the image value in kubernetes yaml.

Reethu July 22, 2021

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'




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 !

1 answer

1 accepted

0 votes
Answer accepted
Oleksandr Kyrdan
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 22, 2021

Hi @Reethu

Thank you for your question!

To make image name flexible try envsubst (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.

 

Cheers,
Oleksandr Kyrdan

Oleksandr Kyrdan
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 22, 2021

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:

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.

Reethu July 22, 2021

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'



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

Oleksandr Kyrdan
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 23, 2021

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.

Like Reethu likes this
Reethu July 23, 2021

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

It worked for me now !!

Like Oleksandr Kyrdan likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events