-
Notifications
You must be signed in to change notification settings - Fork 1
135 lines (115 loc) · 3.74 KB
/
ci-backend.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: Backend CI Pipeline
on:
workflow_dispatch:
push:
branches:
- 'integration'
paths:
- 'backend/**'
- '!backend/*.md'
jobs:
build-and-test:
name: Build & Test
runs-on: ubuntu-latest
services:
db:
image: postgres:13
env:
POSTGRES_USER: app
POSTGRES_PASSWORD: changethis123
POSTGRES_DB: app
POSTGRES_HOST: localhost
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
cd backend
curl -sSL https://install.python-poetry.org | python3 -
poetry install
- name: Copy env file
run: |
cd backend
cp .env.sample .env
- name: Run app
run: |
cd backend
poetry run bash ./prestart.sh
poetry run uvicorn app.main:app --host 0.0.0.0 --port 8000 &
- name: Run tests
run: |
cd backend
poetry run pytest | tee /dev/null
build-push-image:
name: Build & Push Image
runs-on: ubuntu-latest
needs: build-and-test
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set environment variables
run: |
COMMIT_SHA=$(git rev-parse --short HEAD)
echo "IMAGE_NAME=${{ secrets.DOCKERHUB_USERNAME }}/backend" >> $GITHUB_ENV
echo "IMAGE_TAG_1=latest" >> $GITHUB_ENV
echo "IMAGE_TAG_2=${{ github.run_number }}-$COMMIT_SHA" >> $GITHUB_ENV
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v4
with:
context: "{{defaultContext}}:backend"
push: true
tags: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG_1 }}, ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG_2 }}
update-deployment:
name: Update Deployment File
runs-on: ubuntu-latest
needs: build-push-image
env:
GIT_USER_NAME: DrInTech
GIT_USER_EMAIL: ${{ secrets.TF_CERT_EMAIL }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Set environment variables
run: |
COMMIT_SHA=$(git rev-parse --short HEAD)
echo "IMAGE_NAME=${{ secrets.DOCKERHUB_USERNAME }}/backend" >> $GITHUB_ENV
echo "IMAGE_TAG_2=${{ github.run_number }}-$COMMIT_SHA" >> $GITHUB_ENV
- name: Update Deployment YAML
run: |
sed -i "s|image: ${{ env.IMAGE_NAME }}:.*|image: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG_2 }}|" compose.yml
- name: Commit and Push Changes
run: |
BRANCH_NAME=${{ github.ref_name }}
git config --global user.email "${{ env.GIT_USER_EMAIL }}"
git config --global user.name "${{ env.GIT_USER_NAME }}"
git add compose.yml
git commit -m "Update Docker Image Tag to ${{ env.IMAGE_TAG_2 }}"
git pull --rebase origin $BRANCH_NAME
git push origin $BRANCH_NAME
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}