forked from Jayclifford345/adventure
-
Notifications
You must be signed in to change notification settings - Fork 2
135 lines (113 loc) · 4.03 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
name: Build and Push Docker Image
on:
push:
branches:
- main # Trigger on pushes to the main branch
- microservice
workflow_dispatch: # Allow manual triggers
jobs:
test:
runs-on: ubuntu-latest
env:
FOO: bar
steps:
# Step 1: Checkout the repository
- name: Checkout Code
uses: actions/[email protected]
# Step 2: Set up Python environment
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.13"
# Step 3: Install dependencies
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest pytest-cov
# Step 4: Run tests with coverage
- name: Run Tests with Coverage
run: |
PYTHONPATH=$(pwd):$PYTHONPATH pytest --cov=app --cov-report=xml --cov-report=html tests/
# Step 5: Upload coverage report as an artifact
- name: Upload Coverage Report
uses: actions/[email protected]
with:
name: coverage-report
path: htmlcov/
build-and-push:
needs: test
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the code
- name: Checkout code
uses: actions/[email protected]
- id: 'auth'
uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCLOUD_SA_KEY }}'
# Step 2: Set up Google Cloud SDK
- name: Set up Google Cloud SDK
uses: google-github-actions/[email protected]
with:
project_id: ${{ secrets.GCLOUD_PROJECT }}
# Step 3: Authenticate Docker with Google Cloud Artifact Registry
- name: Authenticate Docker with GCloud
run: |
gcloud config set account '${{ secrets.GCLOUD_SA_ACCOUNT }}'
gcloud auth configure-docker ${DOCKER_REGISTRY}
# Step 4: Build the Docker image
- name: Build Docker image
run: |
docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .
echo ${IMAGE_NAME}
# Step 5: Push the Docker image to Google Cloud Artifact Registry
- name: Push Docker image
run: |
gcloud auth configure-docker ${DOCKER_REGISTRY}
docker push ${IMAGE_NAME}:${IMAGE_TAG}
env:
DOCKER_REGISTRY: us-east4-docker.pkg.dev
GCLOUD_PROJECT: ${{ secrets.GCLOUD_PROJECT }}
IMAGE_NAME: us-east4-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT }}/dev-advocacy/adventure
IMAGE_TAG: latest
deploy:
needs:
- test
- build-and-push
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/[email protected]
- id: 'auth'
uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCLOUD_SA_KEY }}'
# Step 2: Set up Google Cloud SDK
- name: Set up Google Cloud SDK
uses: google-github-actions/[email protected]
with:
project_id: ${{ secrets.GCLOUD_PROJECT }}
- name: Get branch names.
id: branch-names
uses: tj-actions/branch-names@v8
- name: Deploy to Cloud Run
env:
REGION: us-east4
DOCKER_REGISTRY: us-east4-docker.pkg.dev
IMAGE_NAME: us-east4-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT }}/dev-advocacy/adventure
IMAGE_TAG: latest
GCP_PROJECT_ID: ${{ secrets.GCLOUD_PROJECT }}
# Network settings are important to permit connecting to the memcached instance
run: |
gcloud run deploy adventure-${{ steps.branch-names.outputs.current_branch }} \
--project ${GCP_PROJECT_ID} \
--image ${IMAGE_NAME}:${IMAGE_TAG} \
--region ${REGION} \
--network=default \
--subnet=default \
--vpc-egress=private-ranges-only \
--port 3000 \
--timeout 6m \
--vpc-connector adventure-vpc-connector \
--update-env-vars MEMCACHED_HOST=10.150.0.3,MEMCACHED_PORT=11211