-
Notifications
You must be signed in to change notification settings - Fork 11
116 lines (109 loc) · 3.75 KB
/
main.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
name: Continuous Integration and Delivery
on:
push:
branches:
- master
env:
IMAGE: docker.pkg.github.com/$(echo $GITHUB_REPOSITORY | tr '[A-Z]' '[a-z]')/app
jobs:
build:
name: Build Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout master
uses: actions/checkout@v3
- name: Log in to Github Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull image
run: |
docker pull ${{ env.IMAGE }}:latest || true
- name: Build Image
run: |
docker build \
--cache-from ${{ env.IMAGE }}:latest \
--tag ${{ env.IMAGE }}:latest \
--file ./project/Dockerfile.prod \
"./project"
- name: Push image
run: |
docker push ${{ env.IMAGE }}:latest
test:
name: Test Docker Image
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout master
uses: actions/checkout@v3
- name: Log in to GitHub Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull images
run: |
docker pull ${{ env.IMAGE }}:latest || true
- name: Build images
run: |
docker build \
--cache-from ${{ env.IMAGE }}:latest \
--tag ${{ env.IMAGE }}:latest \
--file ./project/Dockerfile.prod \
"./project"
- name: Run container
run: |
docker run \
-d \
--name docker-dash \
-e PORT=8050 \
-p 8050:8050 \
${{ env.IMAGE }}:latest
- name: Install requirements
run: docker exec docker-dash pip install black==22.12.0 flake8===6.0.0 pytest==7.2.1 bandit==1.7.4
- name: Pytest
run: docker exec docker-dash python -m pytest .
- name: Flake8
run: docker exec docker-dash python -m flake8 --max-line-length=119 .
- name: Black
run: docker exec docker-dash python -m black . --check
deploy:
name: Deploy to Google Cloud Run
runs-on: ubuntu-latest
needs: [build, test]
env:
IMAGE_NAME: gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_APP_NAME }}
steps:
- name: Checkout master
uses: actions/checkout@v3
- name: Log in to Github Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull image
run: |
docker pull ${{ env.IMAGE }}:latest || true
- name: Build image
run: |
docker build \
--cache-from ${{ env.IMAGE }}:latest \
--tag ${{ env.IMAGE_NAME }}:latest \
--file ./project/Dockerfile.prod \
"./project"
- name: Login to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
- name: Configure Docker
run: gcloud auth configure-docker --quiet
- name: Push to registry
run: docker push ${{ env.IMAGE_NAME }}
- id: deploy
name: Deploy Docker image
uses: "google-github-actions/deploy-cloudrun@v1"
with:
image: ${{ env.IMAGE_NAME }}
region: europe-north1
service: app
flags: --port=8080 --allow-unauthenticated
- name: Clean up old images
run: gcloud container images list-tags ${{ env.IMAGE_NAME }} --filter='-tags:*' --format="get(digest)" --limit=10 > tags && while read p; do gcloud container images delete "${{ env.IMAGE_NAME }}@$p" --quiet; done < tags