-
Notifications
You must be signed in to change notification settings - Fork 0
143 lines (127 loc) · 4.61 KB
/
cd-test-deploy.yml
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
name: CD Test Deploy
on:
# use target so secrets are not exposed
pull_request_target:
types: [ opened, synchronize, reopened ]
branches: [ dependencies ]
env:
NAMESPACE: pr-${{ github.event.pull_request.number }}
jobs:
FrontendCI:
uses: ./.github/workflows/ci-frontend.yml
BackendCI:
uses: ./.github/workflows/ci-backend.yml
Containerize:
needs: [ FrontendCI, BackendCI ]
runs-on: ubuntu-latest
strategy:
matrix:
app: [ frontend, backend ]
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login Docker Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and Push Docker Image
uses: docker/build-push-action@v6
with:
context: ./${{ matrix.app }}
platforms: linux/arm64
push: true
tags: ghcr.io/${{ github.repository }}/${{ matrix.app }}:${{ env.NAMESPACE }}
cache-from: type=gha,scope=buildkit-${{ matrix.app }}-${{ env.NAMESPACE }}
cache-to: type=gha,mode=max,scope=buildkit-${{ matrix.app }}-${{ env.NAMESPACE }}
Deploy:
needs: [ Containerize ]
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Create GitHub Environment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GH_PAT }}
script: |
const { NAMESPACE } = process.env
await github.rest.repos.createOrUpdateEnvironment({
owner: context.repo.owner,
repo: context.repo.repo,
environment_name: `test/${NAMESPACE}`,
})
- name: Setup Kubectl
uses: Azure/setup-kubectl@v4
- name: Configure Kubectl
env:
K8S_CONFIG: ${{ secrets.K8S_CONFIG }}
run: |
mkdir -p $HOME/.kube
echo "${K8S_CONFIG}" | base64 --decode > $HOME/.kube/config
shell: bash
- name: Clear any previous Release
run: |
kubectl create namespace $NAMESPACE --dry-run=client -o yaml | kubectl apply -f -
helm uninstall $NAMESPACE \
--namespace $NAMESPACE \
--ignore-not-found
shell: bash
- name: Install Chart
run: |
helm install $NAMESPACE ./.k8s/app \
--namespace $NAMESPACE \
--set ingress.domain=${{ env.NAMESPACE }}.test.k8splay.xyz \
--set app.image.tag=${{ env.NAMESPACE }} \
--set keycloak.realm=test \
--set keycloak.client.secret=${{ secrets.KEYCLOAK_TEST_SECRET }} \
--wait \
--atomic \
--timeout 600s
shell: bash
- name: Create GitHub Deployment
uses: actions/github-script@v7
env:
REF: ${{ github.head_ref }}
with:
github-token: ${{ secrets.GH_PAT }}
script: |
const { NAMESPACE, REF, GITHUB_RUN_ID } = process.env
// set all previous deployments to inactive
const deployments = await github.rest.repos.listDeployments({
owner: context.repo.owner,
repo: context.repo.repo,
environment: `test/${NAMESPACE}`,
per_page: 100,
})
for (const deployment of deployments.data) {
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deployment.id,
state: 'inactive',
environment: `test/${NAMESPACE}`,
})
}
// create new deployment
const deployment = await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: REF,
environment: `test/${NAMESPACE}`,
auto_merge: false,
transient_environment: true,
required_contexts: [],
})
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: deployment.data.id,
state: 'success',
environment: `test/${NAMESPACE}`,
environment_url: `https://${NAMESPACE}.test.k8splay.xyz`,
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${GITHUB_RUN_ID}`
})