-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5557a57
commit 12d7887
Showing
1 changed file
with
15 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,63 @@ | ||
name: CI/CD Pipeline | ||
|
||
# 当代码推送到 main 分支时触发工作流 | ||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
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|<your_docker_hub_username>|${{ vars.DOCKER_USERNAME }}|g" k8s-backend/backend-deployment.yaml | ||
# sed -i "s|<your_docker_hub_username>|${{ vars.DOCKER_USERNAME }}|g" k8s-frontend/frontend-deployment.yaml | ||
# 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 |