在 GitHub Actions 中设置 minikube 作为 CI 步骤

如何在 GitHub Actions 中使用 minikube 测试您的应用程序

要安装和启动 minikube 集群,请将以下步骤添加到您的 GitHub Actions 工作流中。

steps:
- name: start minikube
  id: minikube
  uses: medyagh/setup-minikube@latest

有关更多信息,请参阅 GitHub Actions 市场 setup-minikube

示例:在每次 PR 时构建镜像并部署到 minikube

要求

  • 有效的 Dockerfile
  • 有效的 deployment.yaml 文件,其中包含 imagePullPolicy: Never,请参阅下面的示例

创建工作流

  • 将此 yaml 复制到您的工作流文件中,例如在 .github/workflows/pr.yml

    name: CI
    on:
      - pull_request
    jobs:
      job1:
        runs-on: ubuntu-latest
        name: build example and deploy to minikube
        steps:
        - uses: actions/checkout@v4
          with:
            repository: medyagh/local-dev-example-with-minikube
        - name: Start minikube
          uses: medyagh/setup-minikube@latest
        - name: Try the cluster!
          run: kubectl get pods -A
        - name: Build image
          run: |
            minikube image build -t local/devex:v1 .        
        - name: Deploy to minikube
          run:
            kubectl apply -f deploy/k8s.yaml
            kubectl wait --for=condition=ready pod -l app=local-devex
        - name: Test service URLs
          run: |
            minikube service list
            minikube service local-devex-svc --url
            echo "------------------opening the service------------------"
            curl $(minikube service local-devex-svc --url)        
    

上面的示例工作流 yaml 将在每次传入的 PR 上执行以下步骤

  1. 检出源代码
  2. 安装并启动 minikube
  3. 仅通过运行 kubectl 命令来试用集群
  4. 使用 minikube 的 docker-env 功能构建 docker 镜像
  5. 将部署 yaml 文件应用于 minikube
  6. 检查服务是否已在 minikube 中创建

示例 minikube 部署 yaml 文件,包含一个服务

apiVersion: apps/v1
kind: Deployment
metadata:
    name: example
spec:
    selector:
        matchLabels:
            app: example
    replicas: 2
    template:
        metadata:
            labels:
                app: example
        spec:
            containers:
                - name: example-api
                  imagePullPolicy: Never
                  image: local/example:latest
                  resources:
                      limits:
                          cpu: 50m
                          memory: 100Mi
                      requests:
                          cpu: 25m
                          memory: 10Mi
                  ports:
                      - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
    name: example
spec:
    type: NodePort
    selector:
        app: example
    ports:
        - port: 8080
          targetPort: 8080

上次修改时间 2024 年 12 月 17 日: 更新 Github actions 文档 url (#20126) (dfc0696c2)