-
Notifications
You must be signed in to change notification settings - Fork 284
158 lines (134 loc) · 5.14 KB
/
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
name: Gofr-web workflow
on:
push:
paths:
- 'docs/**'
branches:
- add-docs
env:
GCR_PROJECT: zs-products
APP_NAME: gofr-web
CLUSTER_NAME: products-cluster
STAGE_NAMESPACE: gofr-stage
PROD_NAMESPACE: gofr
jobs:
stage_build:
runs-on: ubuntu-latest
if: (github.ref == 'refs/heads/add-docs') && (github.event_name == 'push')
outputs:
image: ${{ steps.output-image.outputs.image }}
strategy:
matrix:
node-version: [ 16.x ]
steps:
- uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Pull Website Package
run: |
docker pull ghcr.io/gofr-dev/website:workflow-package
- name: Login to GCR
uses: docker/login-action@v1
with:
registry: gcr.io
username: _json_key
password: ${{ secrets.GCR_KEY }}
- name: building docker image
uses: docker/build-push-action@v2
with:
tags: gcr.io/${{ env.GCR_PROJECT }}/${{ env.APP_NAME }}:${{ github.sha }}
context: ./
file: ./docs/Dockerfile
push: true
- id: output-image
run: echo "image=`echo gcr.io/${{ env.GCR_PROJECT }}/${{ env.APP_NAME }}:${{ github.sha }}`" >> "$GITHUB_OUTPUT"
stage_deployment:
runs-on: ubuntu-latest
needs: stage_build
env:
image: ${{ needs.stage_build.outputs.image }}
steps:
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v0
with:
project_id: ${{ env.GCR_PROJECT }}
service_account_key: ${{ secrets.DEPLOY_KEY }}
export_default_credentials: true
# - name: Update Kubectl component
# run: gcloud --quiet components update kubectl
#
# - name: Set GCloud Project and Fetch Cluster Credentials
# run: gcloud container clusters get-credentials ${{ env.CLUSTER_NAME }} --zone=us-central1 --project=${{ env.GCR_PROJECT }}
#
# - name: Set Deployment Image
# run: kubectl set image deployment/${{ env.APP_NAME }} ${{ env.APP_NAME }}=${{ env.image }} --namespace ${{ env.STAGE_NAMESPACE }}
check-tag:
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')
outputs:
tag_exists: ${{ steps.tag-check.outputs.tag_exists }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v0
with:
project_id: ${{ env.GCR_PROJECT }}
service_account_key: ${{ secrets.DEPLOY_KEY }}
export_default_credentials: true
- name: Check if tag exists
id: tag-check
run: |
if gcloud container images describe gcr.io/${{ env.GCR_PROJECT }}/${{ env.APP_NAME }}:${{ github.sha }} 2>/dev/null; then
echo "tag_exists=true" >> "$GITHUB_OUTPUT"
else
echo "tag_exists=false" >> "$GITHUB_OUTPUT"
fi
- name: Print commit has value
run: |
echo "${{ github.sha }}"
- name: Print tag_exists value
run: |
echo "${{ steps.tag-check.outputs.tag_exists }}"
retag:
name: Retagging existing image
runs-on: ubuntu-latest
needs: check-tag
if: ${{ needs.check-tag.outputs.tag_exists == 'true' }}
outputs:
image: ${{ steps.output-image.outputs.image }}
steps:
- name: Extract Release Tag
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Re tag and Push Docker Image to GCR
run: |
docker pull gcr.io/${{ env.GCR_PROJECT }}/${{ env.APP_NAME }}:${{ github.sha }}
docker tag gcr.io/${{ env.GCR_PROJECT }}/${{ env.APP_NAME }}:${{ github.sha }} gcr.io/${{ env.GCR_PROJECT }}/${{ env.APP_NAME }}:${{ env.RELEASE_VERSION }}
docker push gcr.io/${{ env.GCR_PROJECT }}/${{ env.APP_NAME }}:${{ env.RELEASE_VERSION }}
- id: output-image
run: echo "image=`echo gcr.io/${{ env.GCR_PROJECT }}/${{ env.APP_NAME }}:${{ env.RELEASE_VERSION }}`" >> "$GITHUB_OUTPUT"
prod_deployment:
runs-on: ubuntu-latest
name: 🚀 Deploy to Prod
needs: retag
env:
image: ${{ needs.retag.outputs.image }}
steps:
- name: Extract Release Tag
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v0
with:
project_id: ${{ env.GCR_PROJECT }}
service_account_key: ${{ secrets.DEPLOY_KEY }}
export_default_credentials: true
- name: Update Kubectl component
run: gcloud --quiet components update kubectl
- name: Set GCloud Project and Fetch Cluster Credentials
run: gcloud container clusters get-credentials ${{ env.CLUSTER_NAME }} --zone=us-central1 --project=${{ env.GCR_PROJECT }}
- name: Set Deployment Image for API
run: kubectl set image deployment/${{ env.APP_NAME }} ${{ env.APP_NAME }}=${{ env.image }} --namespace ${{ env.PROD_NAMESPACE }}