docker - 使用 github 工作流部署到 ​​kubernetes 集群

我已经使用 kops 在 AWS 上设置了一个 kubernetes 集群。

我正在尝试使用 github 操作自动部署。

name: Build and Deploy

on:
  push:
    branches:
      - develop

jobs:
  build_docker_image:
    - uses: actions/checkout@v2
    - name: Build the tagged Docker image
      run: docker build --target dev -t org/customer-service-backend:la
  push_docker_image_to_github-packages:
    - uses: docker/build-push-action@v2
      with:
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
        registry: docker.pkg.github.com
        repository: org/customer-service-backend:latest
        tag_with_ref: true
  deploy_to_kubernetes_cluster:
    ... what to do here?

我能够构建图像并推送到 gihub 包。

我已经在存储库的根目录中创建了 deployment.yml

如何部署到 kubernetes 集群?

另外,我用 latest 标记图像。可以吗,还是我需要使用 GITHUB_REF 进行标记?

更新

我可以配置所有的东西。我只需要获取 kubeconfig 即可对现有集群进行身份验证。

name: Build and Deploy

on:
  push:
    branches:
      - develop

jobs:

  build_docker_image:
    - uses: actions/checkout@v2
    - name: Build the tagged Docker image
      run: docker build --target dev -t org/customer-service-backend:${{ github.sha }}

  push_docker_image_to_github_packages:
    needs: build_docker_image
    - uses: docker/build-push-action@v2
      with:
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
        registry: docker.pkg.github.com
        repository: org/customer-service-backend:${{ github.sha }}
        tag_with_ref: true

  deploy_to_kubernetes_cluster:
    needs: push_docker_image_to_github_packages
    name: Set Kubernetes Context
    uses: azure/k8s-set-context@v1
      with:
        method: kubeconfig
        kubeconfig: ${{ secrets.KUBE_CONFIG }} # Use secret (https://developer.github.com/actions/managing-workflows/storing-secrets/)
    run: |
      sed -i'' -e 's/IMAGE_LABEL/${{ github.sha }}/g' deployment.yml
      kubectl apply -f deployment.yml



最佳答案

通过查看您的工作流配置文件,所有作业并行运行。

但是,这可能不是您想要的。

推送图像需要构建图像,部署作业需要更新的构建图像。

在 Access kubernetes 集群上,只需访问您的集群即可,

cat $HOME/.kube/config

并复制输出。

现在,使用 KUBE_CONFIG 作为环境变量在 github 中创建一个 secret。

注意 - 这是访问 kubernetes 集群的一种方法,还有其他方法,选择适合您需要的方法

name: Build and Deploy

on:
  push:
    branches:
      - develop

jobs:

  build_docker_image:
      name: Build Docker Image
      runs-on: ubuntu-latest
      steps:
        - name: Checkout Repo
          uses: actions/checkout@v2
        - name: Build the tagged Docker image
          run: docker build --target dev -t your_org/customer-service-backend:${{ github.sha }} .

  push_docker_image_to_github_packages:
    name: Push Docker Image to Github Packages
    needs: build_docker_image
    runs-on: ubuntu-latest
    steps:
      - name: Push Docker Image
        uses: docker/build-push-action@v2
        with:
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
          registry: docker.pkg.github.com
          repository: your_org/customer-service-backend:${{ github.sha }}

  deploy_to_kubernetes_cluster:
    name: Deploy to Kubernetes Cluster
    needs: push_docker_image_to_github_packages
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v2
      - name: Set Kubernetes Context
        uses: azure/k8s-set-context@v1
        with:
          method: kubeconfig
          kubeconfig: ${{ secrets.KUBE_CONFIG }} # Use secret (https://developer.github.com/actions/managing-workflows/storing-secrets/)
      - name: Deploy to Cluster
        run: |
          sed -i'' -e 's/IMAGE_LABEL/${{ github.sha }}/g' deployment.yml
          kubectl apply -f deployment.yml

https://stackoverflow.com/questions/66684294/

相关文章:

c - 将 foo(int *) 作为参数传递给 X 中的 foo(void*)

c++ - C++中通过函数删除一个对象

git - 有没有没有版本提交的 'bump the version' 的方法?

javascript - ("[object Promise]") 无法序列化为 JSON

java - 使用 Java 8 迭代 Map 数组并返回 Map

Flutter - 'showSnackBar' 已弃用 - 如何更新?

javascript - 动画仅适用于第一次单击按钮

python - 从具有相同键的多个字典中获取值

python - 将数据规范化为 [-1 and 1] ,但需要保留 0 值

python - 在 pandas 的 to_markdown() 中抑制科学记数法