使用 Github 操作部署 Azure AKS

Azure AKS Deploy using Github Actions

我正在尝试将我的部署自动化到 Azure AKS,但我想弄清楚如何在我的清单文件中引用图像名称。目前我已经在清单文件中注释掉了图像名称,所以看看它是否有效但出现错误:

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

这是我的 Github 工作流程文件:

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

这是我的清单文件:

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

更新:@Rutnet 找到了使用 Azure/k8s-deploy1@1 操作传递新标签的方法。来自 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.

根据文档,清单文件需要使用默认标签引用原始图像。该操作将用指定的标签替换标签。有问题的清单有 image 评论。它应该是这样的:

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

原回复:

有几种方法可以实现这一点。在应用清单之前,您可以使用 Helm or Kustomize. In this case, you can just use sed 等模板工具。在清单文件中添加一个占位符并将其替换为 sed inline。请参阅以下示例:

...
- 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
...

在清单文件中添加 NGINX_IMAGE_URL 占位符:

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

HTH