-
Notifications
You must be signed in to change notification settings - Fork 0
125 lines (117 loc) · 3.66 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
117
118
119
120
121
122
123
124
125
name: Continuous Integration and Delivery
on: [push]
env:
FLY_APP_NAME: tdd-fastapi
IMAGE: ghcr.io/$(echo $GITHUB_REPOSITORY | tr '[A-Z]' '[a-z]')/summarizer
jobs:
build:
name: Build Docker Image
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: main
- name: Log in to GitHub Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
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 ./server/Dockerfile.prod \
"./server"
- name: Push image
run: |
docker push ${{ env.IMAGE }}:latest
test:
name: Test Docker Image
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
needs: build
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: main
- name: Log in to GitHub Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
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 ./server/Dockerfile.prod \
"./server"
- name: Run container
run: |
docker run \
-d \
--name fastapi-tdd \
-e PORT=8765 \
-e ENVIRONMENT=dev \
--network ${{ job.container.network }} \
-e DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres \
-e DATABASE_TEST_URL=postgres://postgres:postgres@postgres:5432/postgres \
-p 5003:8765 \
${{ env.IMAGE }}:latest
- name: Format
run: docker exec fastapi-tdd uv run ruff format .
- name: Lint
run: docker exec fastapi-tdd uv run ruff check .
- name: Sort imports
run: docker exec fastapi-tdd uv run ruff check --select I
- name: Pytest
run: docker exec fastapi-tdd uv run pytest .
- name: Stop docker container to avoid network warning
run: docker stop fastapi-tdd
deploy:
name: Deploy to Fly.io
needs: test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: main
- name: Log in to GitHub Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull image
run: |
docker pull ${{ env.IMAGE }}:latest || true
- name: Set up Fly.io CLI
uses: superfly/flyctl-actions/setup-flyctl@master
- name: Deploy to Fly.io
run: flyctl deploy -a ${{ env.FLY_APP_NAME }} --image ${{ env.IMAGE }}:latest
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}