-
-
Notifications
You must be signed in to change notification settings - Fork 0
146 lines (121 loc) Β· 4.54 KB
/
ci-cd.yaml
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
#
# Requires secrets:
# GCP_HOST_GCR The base host of the Google Cloud Registry
# GCP_REGISTRY_PROJECT_ID The GCP Project ID of the Google Cloud Registry
# GCP_SA_KEY_GCR_PUSHER The GCP Service Account key used to push on the Google Cloud Registry
# GCP_GCR_REPOSITORY The repository name of the app on the Google Cloud Registry
# GCP_SA_KEY_CLOUDRUN_DEPLOYER The GCP Service Account key used to deploy services on Cloud Run
# GCP_PROJECT_ID The GCP Project ID to deploy the app
# GCP_CLOUDRUN_SERVICE The name of the Cloud Run service to deploy
# GCP_ZONE The region of the Cloud Run service
#
on:
push:
branches: [ master ]
pull_request:
release:
types: [ published ]
env:
IMAGE: ${{ secrets.GCP_HOST_GCR }}/${{ secrets.GCP_REGISTRY_PROJECT_ID }}/${{ secrets.GCP_GCR_REPOSITORY }}
TAG: ${{ github.ref_name }}-${{ github.sha }}
jobs:
test:
name: Test app
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
architecture: 'x64'
- name: Get Cache
uses: actions/[email protected]
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
${{ runner.os }}-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install wheel
pip install -r requirements.txt
- name: Pylint
run: |
pip install pylint
python -m pylint --fail-under=10 `find -regextype egrep -regex '(.*.py)$'` |
tee pylint.txt
- name: Flake8
run: |
pip install flake8
flake8 --exit-zero --ignore=E501,W505 .
docker-build-push-gcr:
name: Docker build & push on Google Cloud Registry
runs-on: ubuntu-latest
needs: [ test ]
if: github.event.ref == 'refs/heads/master' || github.event_name == 'release'
steps:
- uses: actions/checkout@v3
- uses: google-github-actions/[email protected]
with:
credentials_json: ${{ secrets.GCP_SA_KEY_GCR_PUSHER }}
- uses: google-github-actions/setup-gcloud@v0
with:
project_id: ${{ secrets.GCP_REGISTRY_PROJECT_ID }}
export_default_credentials: true
- name: Configure Docker to use the gcloud command-line tool
run: |-
gcloud --quiet auth configure-docker
- name: Build the Docker image
run: |-
docker build . --compress --tag $IMAGE:$TAG
- name: Push the Docker image to Google Container Registry
run: |-
docker push $IMAGE:$TAG
gcloud container images add-tag $IMAGE:$TAG $IMAGE:latest --quiet
- name: Cleanup old images on Google Container Registry
run: |-
limitDate=$(date "+%Y-%m-%d %H:%M:%S" -d "60 days ago")
digests=$(gcloud container images list-tags $IMAGE \
--filter="timestamp.datetime < '$limitDate'" \
--format="get(digest)")
for digest in $digests; do
(
gcloud container images delete --quiet --force-delete-tags $IMAGE@$digest
)
done
deploy-staging:
name: Deploy to Staging πΎ
runs-on: ubuntu-latest
needs: [ docker-build-push-gcr ]
if: github.event.ref == 'refs/heads/master'
environment: staging
steps:
- uses: google-github-actions/[email protected]
with:
credentials_json: ${{ secrets.GCP_SA_KEY_CLOUDRUN_DEPLOYER }}
- uses: google-github-actions/deploy-cloudrun@main
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
service: ${{ secrets.GCP_CLOUDRUN_SERVICE }}
image: ${{ env.IMAGE }}:${{ env.TAG }}
region: ${{ secrets.GCP_ZONE }}
deploy-prod:
name: Deploy to Prod π
runs-on: ubuntu-latest
needs: [ docker-build-push-gcr ]
if: github.event_name == 'release'
environment: production
steps:
- uses: google-github-actions/[email protected]
with:
credentials_json: ${{ secrets.GCP_SA_KEY_CLOUDRUN_DEPLOYER }}
- uses: google-github-actions/deploy-cloudrun@main
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
service: ${{ secrets.GCP_CLOUDRUN_SERVICE }}
image: ${{ env.IMAGE }}:${{ env.TAG }}
region: ${{ secrets.GCP_ZONE }}