Rutnet
Rutnet

Reputation: 1673

Azure AKS Deploy using Github Actions

I am trying to automate my deployment to Azure AKS, but trying to work out how to reference the image name in my manifest file. At the moment I have commented out the image name in the manifest file so see if that works but getting an error:

##[error]TypeError: Cannot read property 'trim' of undefined

This is my Github workflow file:

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    
    - uses: Azure/docker-login@v1
      with:
        login-server: registry.azurecr.io
        username: ${{ secrets.REGISTRY_USERNAME }}
        password: ${{ secrets.REGISTRY_PASSWORD }}
    
    - run: |
        docker build . --file Dockerfile_nginx -t registry.azurecr.io/k8sdemo:${{ github.sha }}
        docker push registry.azurecr.io/k8sdemo:${{ github.sha }}
      
    - uses: Azure/k8s-set-context@v1
      with:
        kubeconfig: ${{ secrets.KUBE_CONFIG }}
      

    - uses: Azure/k8s-deploy@v1
      with:
        manifests: |
          k8s/mg-k8s/nginx.yaml
        images: |
          registry.azurecr.io/k8sdemo:${{ github.sha }}
        imagepullsecrets: |
          secret

This is my manifest file:

apiVersion: v1
kind: Service
metadata:
  name: nginxstaticservice
  namespace: mg-staging
  labels:
    app: nginxstatic
spec:
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
  # selector:
  #   app: nginxstatic

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginxstatic-deployment
  namespace: mg-staging
  labels:
    app: nginxstatic
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginxstatic
  template:
    metadata:
      labels:
        app: nginxstatic
    spec:
      containers:
      - name: nginxstatic
        # image: 
        imagePullPolicy: "Always"
        ports:
        - containerPort: 80

        volumeMounts:
          - name: nginx-config
            mountPath: /etc/nginx/conf.d
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-configmap
      imagePullSecrets:
      - name: secret

Upvotes: 1

Views: 1357

Answers (1)

Faheem
Faheem

Reputation: 3569

Update: @Rutnet figured out the way to pass the new tag using Azure/k8s-deploy1@1 action. From the docs:

(Optional) Fully qualified resource URL of the image(s) to be used for substitutions on the manifest files. This multiline input accepts specifying multiple artifact substitutions in newline separated form. For example -

images: |   
contosodemo.azurecr.io/foo:test1  
contosodemo.azurecr.io/bar:test2

In this example, all references to contosodemo.azurecr.io/foo and contosodemo.azurecr.io/bar are searched for in the image field of the input manifest files. For the matches found, the tags test1 and test2 are substituted.

Based on the documentation, the manifest file needs to have references to the original image with a default tag. The action will replace the tags with the ones specified. The manifest in question has the image commented. It should have been something like:

    spec:
      containers:
      - name: nginxstatic
        image: registry.azurecr.io/k8sdemo:some_tag

Original Reply:

There are several ways of achieving this. You can use templating tools like Helm or Kustomize. In this case, you can just use sed before you apply the manifest. Add a place holder in the manifest file and replace that with sed inline. See the following example:

...
- run: |
    sed -i.bak "/NGINX_IMAGE_URL/registry.azurecr.io\/k8sdemo:${{ github.sha }}" k8s/mg-k8s/nginx.yaml

- uses: Azure/k8s-deploy@v1
      with:
        manifests: |
          k8s/mg-k8s/nginx.yaml
        images: |
          registry.azurecr.io/k8sdemo:${{ github.sha }}
        imagepullsecrets: |
          secret
...

Add the NGINX_IMAGE_URL place holder in the manifest file:

...
    spec:
      containers:
      - name: nginxstatic
        image: NGINX_IMAGE_URL
...

HTH

Upvotes: 1

Related Questions