-
Notifications
You must be signed in to change notification settings - Fork 0
92 lines (79 loc) · 2.83 KB
/
Copy pathcd.yml
File metadata and controls
92 lines (79 loc) · 2.83 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
name: Build and Deploy
concurrency: production
on:
push:
branches:
- main
workflow_dispatch:
jobs:
# ===============================================================
# JOB 1: Build and Push Docker Image to GHCR
# ===============================================================
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image_tag: ${{ steps.meta.outputs.version }} # 输出 commit SHA 标签给下一个 job
image_repo: ghcr.io/${{ github.repository }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: true
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: type=sha,prefix=,format=short
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# ===============================================================
# JOB 2: Deploy to Kubernetes using Helm
# ===============================================================
deploy-to-kubernetes:
runs-on: ubuntu-latest
needs: build-and-push-image # 依赖上一个 job
environment: Production
permissions:
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # 获取所有历史记录,以便比较文件变更
- name: Set up Helm
uses: azure/setup-helm@v4
- name: Set up Kubeconfig
run: |
mkdir -p ~/.kube
echo "${{ secrets.KUBECONFIG }}" > ~/.kube/config
chmod 600 ~/.kube/config
- name: Deploy with local Helm chart
run: |
# 定义变量
CHART_PATH="./charts/alienfamily-novel" # 你的本地 Chart 路径
RELEASE_NAME="alienfamily-novel" # 你想要在 K8s 中部署的 release 名称
NAMESPACE="alienfamily-novel" # 你要部署到的命名空间
# 直接使用本地 Chart 路径执行 Helm 部署
echo "Upgrading release $RELEASE_NAME in namespace $NAMESPACE using local chart at $CHART_PATH..."
helm upgrade --install $RELEASE_NAME $CHART_PATH \
--namespace $NAMESPACE \
--create-namespace \
--set image.tag=${{ needs.build-and-push-image.outputs.image_tag }} \
-f $CHART_PATH/values.yaml \
--atomic \
--wait