generated from trywilco/Anythink-Market-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
157 lines (137 loc) · 5.16 KB
/
k8s.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
name: Build and deploy to Kubernetes
on:
push:
branches:
- main
concurrency:
group: k8s
cancel-in-progress: true
jobs:
check-kubernetes-enabled:
runs-on: ubuntu-20.04
outputs:
kubernetes-enabled: ${{ steps.kubernetes-flag-defined.outputs.DEFINED }}
steps:
- id: kubernetes-flag-defined
if: "${{ env.ENABLE_KUBERNETES != '' }}"
run: echo "DEFINED=true" >> $GITHUB_OUTPUT
env:
ENABLE_KUBERNETES: ${{ secrets.ENABLE_KUBERNETES }}
check-secret:
runs-on: ubuntu-20.04
needs: [check-kubernetes-enabled]
outputs:
aws-creds-defined: ${{ steps.aws-creds-defined.outputs.DEFINED }}
kubeconfig-defined: ${{ steps.kubeconfig-defined.outputs.DEFINED }}
if: needs.check-kubernetes-enabled.outputs.kubernetes-enabled == 'true'
steps:
- id: aws-creds-defined
if: "${{ env.AWS_ACCESS_KEY_ID != '' && env.AWS_SECRET_ACCESS_KEY != '' }}"
run: echo "DEFINED=true" >> $GITHUB_OUTPUT
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- id: kubeconfig-defined
if: "${{ env.KUBECONFIG != '' }}"
run: echo "DEFINED=true" >> $GITHUB_OUTPUT
env:
KUBECONFIG: ${{ secrets.KUBECONFIG }}
build-backend:
name: Build backend image
runs-on: ubuntu-20.04
needs: [check-secret]
if: needs.check-secret.outputs.aws-creds-defined == 'true'
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-2
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Set the image tag
run: echo IMAGE_TAG=${GITHUB_REPOSITORY/\//-}-latest >> $GITHUB_ENV
- name: Set repository name
run: |
if [ ${{ secrets.CLUSTER_ENV }} == 'staging' ]; then
echo "REPO_NAME=staging-anythink-backend" >> $GITHUB_ENV
else
echo "REPO_NAME=anythink-backend" >> $GITHUB_ENV
fi
- name: Build, tag, and push backend image to Amazon ECR
id: build-image-backend
run: |
docker build \
-t ${{ steps.login-ecr.outputs.registry }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} \
-f backend/Dockerfile.aws \
.
docker push ${{ steps.login-ecr.outputs.registry }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }}
build-frontend:
name: Build frontend images
runs-on: ubuntu-20.04
needs: [check-secret]
if: needs.check-secret.outputs.aws-creds-defined == 'true'
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-2
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Set the image tag
run: echo IMAGE_TAG=${GITHUB_REPOSITORY/\//-}-latest >> $GITHUB_ENV
- name: Set repository name
run: |
if [ ${{ secrets.CLUSTER_ENV }} == 'staging' ]; then
echo "REPO_NAME=staging-anythink-frontend" >> $GITHUB_ENV
else
echo "REPO_NAME=anythink-frontend" >> $GITHUB_ENV
fi
- name: Build, tag, and push frontend image to Amazon ECR
id: build-image-frontend
run: |
docker build \
-t ${{ steps.login-ecr.outputs.registry }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} \
-f frontend/Dockerfile.aws \
.
docker push ${{ steps.login-ecr.outputs.registry }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }}
deploy:
name: Deploy latest tag using helm
runs-on: ubuntu-20.04
if: needs.check-secret.outputs.kubeconfig-defined == 'true'
needs:
- build-frontend
- build-backend
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Create kube config
run: |
mkdir -p $HOME/.kube/
echo "${{ secrets.KUBECONFIG }}" > $HOME/.kube/config
chmod 600 $HOME/.kube/config
- name: Install helm
run: |
curl -LO https://get.helm.sh/helm-v3.8.0-linux-amd64.tar.gz
tar -zxvf helm-v3.8.0-linux-amd64.tar.gz
mv linux-amd64/helm /usr/local/bin/helm
helm version
- name: Lint helm charts
run: helm lint ./charts/
- name: Set the image tag
run: echo IMAGE_TAG=${GITHUB_REPOSITORY/\//-}-latest >> $GITHUB_ENV
- name: Deploy
run: |
helm upgrade --install --timeout 10m anythink-market ./charts/ \
--set clusterEnv=${{ secrets.CLUSTER_ENV }} \
--set frontend.image.tag=${{ env.IMAGE_TAG }} \
--set backend.image.tag=${{ env.IMAGE_TAG }}