-
Notifications
You must be signed in to change notification settings - Fork 0
191 lines (164 loc) · 5.87 KB
/
build-and-test.yml
File metadata and controls
191 lines (164 loc) · 5.87 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
name: Python-CICD-FastAPI-Basic
# on:
# workflow_call:
# push:
# branches-ignore: [dev, master]
on:
push:
# branches:
# - dev
# - feature/*
branches: [dev, master, feature/*, release/*]
pull_request:
branches: ['*']
jobs:
build-tests:
name: Build and tests
runs-on: ubuntu-latest
env:
elasticsearch_version: '7.13.0'
# It's not working to run as daemon
# elasticsearch_version: '8.8.0'
elasticsearch_port: 9203
services:
postgres:
image: postgres:latest
env:
POSTGRES_DB: postgres
POSTGRES_PASSWORD: ${{ secrets.postgres_password }}
POSTGRES_USER: postgres
POSTGRES_HOST_AUTH_METHOD: trust
PGPORT: 15432
ports:
- 15432:15432
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
redis:
# Docker Hub image
image: redis
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Opens tcp port 6379 on the host and service container
- 6379:6379
steps:
- uses: actions/checkout@v3
- name: Install poetry
id: Build-Poetry
run: pipx install poetry
- uses: actions/setup-python@v4
with:
python-version: 3.9
cache: poetry
- name: Install Python dependencies
run: poetry install
- name: Configure sysctl limits
run: |
sudo swapoff -a
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144
- name: Postgres is reachable
id: Test-Postgres
run: |
# for psql
# sudo apt-get upgrade -y
sudo apt-get install -y postgresql-client
# Test psql connection
PGPASSWORD=${{ secrets.postgres_password }} psql -h localhost -p 15432 -U postgres -d postgres -c "select 1"
- name: Redis is reachable
id: Test-Redis
run: |
sudo apt-get install redis-tools
redis-cli -h localhost -p 6379 ping
- name: Install Elasticsearch
id: Install-Elasticsearch
run: |
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${{ env.elasticsearch_version }}-linux-x86_64.tar.gz
tar -xzf elasticsearch-${{ env.elasticsearch_version }}-linux-x86_64.tar.gz
sudo elasticsearch-${{ env.elasticsearch_version }}/bin/elasticsearch-plugin install analysis-stempel;
sudo elasticsearch-${{ env.elasticsearch_version }}/bin/elasticsearch-plugin install analysis-ukrainian;
sudo elasticsearch-${{ env.elasticsearch_version }}/bin/elasticsearch-plugin install analysis-smartcn;
sudo elasticsearch-${{ env.elasticsearch_version }}/bin/elasticsearch-plugin install analysis-phonetic;
sudo elasticsearch-${{ env.elasticsearch_version }}/bin/elasticsearch-plugin install analysis-icu;
- name: Run Elasticsearch
id: Run-Elasticsearch
run: |
ES_JAVA_OPTS="-Xms1g -Xmx1g" elasticsearch-${{ env.elasticsearch_version }}/bin/elasticsearch -E http.port=${{ env.elasticsearch_port }} -d
sleep 30
- name: Elasticsearch is reachable
id: Test-Elasticsearch
run: |
curl --verbose --show-error http://localhost:${{ env.elasticsearch_port }}
- name: Run tests
id: Build-tests
run: poetry run pytest -sv ./tests
- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
name: code-coverage-report
path: htmlcov
- name: Post to a Slack channel
if: failure()
uses: act10ns/slack@v1
with:
status: ${{ job.status }}
steps: ${{ toJson(steps) }}
channel: '#alert'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
build-docker:
name: Build and Push to DockerHub
if: github.ref == 'refs/heads/master'
needs: build-tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
driver: docker-container
- name: Login to Docker Hub
run: echo ${{ secrets.DOCKERHUB_ACCESS_TOKEN }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
- name: Build and push Docker image
run: |
docker buildx create --use
docker buildx build \
--file Dockerfile \
--tag ${{ secrets.DOCKERHUB_USERNAME }}/fn-python-fastapi-gha:es \
--push .
- name: Post to a Slack channel
if: failure()
uses: act10ns/slack@v1
with:
status: ${{ job.status }}
steps: ${{ toJson(steps) }}
channel: '#alert'
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
alert:
name: Slack-Alerts
needs: build-docker
# if: ${{ github.event.workflow_run.conclusion == 'failure' }}
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Post to a Slack channel
uses: act10ns/slack@v1
with:
status: ${{ job.status }}
steps: ${{ toJson(steps) }}
channel: '#alert'
# message: GitHub Ropository - ${{ github.event.repository.name }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
if: always()