-
Notifications
You must be signed in to change notification settings - Fork 0
124 lines (103 loc) · 4.47 KB
/
deploy.yml
File metadata and controls
124 lines (103 loc) · 4.47 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
name: Kubernetes Deploy CD
on:
repository_dispatch:
types: [build-completed]
workflow_dispatch:
concurrency:
group: deploy-environment
cancel-in-progress: false # Prevents new deployments from canceling an ongoing one
jobs:
deploy:
runs-on: self-hosted
env:
KUBECONFIG: /root/.kube/config
steps:
- name: Check for Running Deployments
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
RUNNING_WORKFLOWS=$(gh run list --repo ${{ github.repository }} --workflow deploy.yml --status in_progress --json databaseId --jq 'length')
if [ "$RUNNING_WORKFLOWS" -gt 0 ]; then
echo "🚨 Another deployment is already running. Exiting."
exit 0
fi
shell: bash
- name: Checkout code
uses: actions/checkout@v6
- name: Wait for all builds to complete
uses: actions/github-script@v8
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const checkRuns = await github.rest.checks.listForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.ref,
});
const builds = checkRuns.data.check_runs.filter(check => check.name.includes("build"));
console.log("Waiting for all builds:", builds.map(b => b.name));
let allCompleted = false;
while (!allCompleted) {
await new Promise(resolve => setTimeout(resolve, 10000)); // Wait 10s before retrying
const updatedChecks = await github.rest.checks.listForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.ref,
});
const updatedBuilds = updatedChecks.data.check_runs.filter(check => check.name.includes("build"));
allCompleted = updatedBuilds.every(b => b.status === "completed");
console.log("Current statuses:", updatedBuilds.map(b => `${b.name}: ${b.status}`));
}
console.log("All builds completed!");
- name: Create Deployment
id: deployment
uses: chrnorm/deployment-action@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
environment: production
description: "Deploy commit ${{ github.sha }} to Kubernetes"
transient-environment: false
production-environment: true
- name: Mark Deployment as In Progress
uses: chrnorm/deployment-status@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
state: in_progress
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
- name: Update Kubernetes Deployment
run: |
# Update deployment
kubectl set image deployment/emailservice-api \
emailservice-api=ghcr.io/ninepiece2/emailservice-api:latest \
-n emailservice-api
# Add deployment annotations
kubectl annotate deployment emailservice-api \
kubernetes.io/change-cause="Deployed commit ${{ github.sha }} from branch ${{ github.ref_name }} via GitHub Actions run https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}. Updated images: APP=ghcr.io/ninepiece2/emailservice-api:latest" \
--overwrite -n emailservice-api
# Restart deployments to pick up new images
kubectl rollout restart deployment/emailservice-api -n emailservice-api
- name: Mark Deployment as Rolling Out
uses: chrnorm/deployment-status@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
state: in_progress
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
description: "Rolling out to Kubernetes..."
- name: Wait for Rollout to Finish
run: |
echo "⏳ Waiting for rollout to finish..."
kubectl rollout status deployment/emailservice-api -n emailservice-api --timeout=15m
- name: Mark Deployment as Successful
if: success()
uses: chrnorm/deployment-status@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
state: success
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
- name: Mark Deployment as Failed
if: failure()
uses: chrnorm/deployment-status@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
state: failure
deployment-id: ${{ steps.deployment.outputs.deployment_id }}