-
Notifications
You must be signed in to change notification settings - Fork 0
63 lines (53 loc) · 2.13 KB
/
ci-cd.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
name: CI/CD Pipeline
# 当代码推送到 main 分支时触发工作流
on:
push:
branches:
- main
jobs:
build-and-deploy:
# 使用最新的 Ubuntu 版本作为运行环境
runs-on: ubuntu-latest
# 定义环境变量
env:
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: ${{ 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|<your_docker_hub_username>|${{ secrets.DOCKER_USERNAME }}|g" k8s-backend/backend-deployment.yaml
# sed -i "s|<your_docker_hub_username>|${{ 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