diff --git a/.github/workflows/ci-cd.yaml b/.github/workflows/ci-cd.yaml index c581fc5..fe9b45b 100644 --- a/.github/workflows/ci-cd.yaml +++ b/.github/workflows/ci-cd.yaml @@ -1,5 +1,6 @@ name: CI/CD Pipeline +# 当代码推送到 main 分支时触发工作流 on: push: branches: @@ -7,47 +8,56 @@ on: jobs: build-and-deploy: + # 使用最新的 Ubuntu 版本作为运行环境 runs-on: ubuntu-latest + # 定义环境变量 env: - BACKEND_IMAGE: ${{ vars.DOCKER_USERNAME }}/k8s-backend:1.0 - FRONTEND_IMAGE: ${{ vars.DOCKER_USERNAME }}/k8s-frontend:1.0 + BACKEND_IMAGE: ${{ secrets.DOCKER_USERNAME }}/k8s-backend:1.0 + FRONTEND_IMAGE: ${{ secrets.DOCKER_USERNAME }}/k8s-frontend:1.0 steps: + # 从 GitHub 仓库中检出代码 - name: Checkout code uses: actions/checkout@v2 + # 设置 Docker Buildx 环境,以支持多平台构建 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 + # 登录到 Docker Hub,使用 GitHub 变量中的 Docker 用户名和 GitHub Secrets 中的 Docker 密码 - name: Login to DockerHub uses: docker/login-action@v1 with: - username: ${{ vars.DOCKER_USERNAME }} + username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} + # 构建并推送后端 Docker 镜像 - name: Build and push backend image run: | cd k8s-backend docker build -t $BACKEND_IMAGE . docker push $BACKEND_IMAGE + # 构建并推送前端 Docker 镜像 - name: Build and push frontend image run: | cd k8s-frontend docker build -t $FRONTEND_IMAGE . docker push $FRONTEND_IMAGE + # 安装和设置 kubectl 工具,指定安装最新版本的 kubectl - name: Set up kubectl uses: azure/setup-kubectl@v1 with: version: 'latest' +# 部署到 Kubernetes 集群,使用 GitHub Secrets 中的 KUBECONFIG 环境变量,并替换部署文件中的 Docker Hub 用户名 # - name: Deploy to Kubernetes # env: # KUBECONFIG: ${{ secrets.KUBECONFIG }} # run: | -# sed -i "s||${{ vars.DOCKER_USERNAME }}|g" k8s-backend/backend-deployment.yaml -# sed -i "s||${{ vars.DOCKER_USERNAME }}|g" k8s-frontend/frontend-deployment.yaml +# sed -i "s||${{ secrets.DOCKER_USERNAME }}|g" k8s-backend/backend-deployment.yaml +# sed -i "s||${{ secrets.DOCKER_USERNAME }}|g" k8s-frontend/frontend-deployment.yaml # kubectl apply -f k8s-backend/backend-deployment.yaml # kubectl apply -f k8s-frontend/frontend-deployment.yaml