-
Notifications
You must be signed in to change notification settings - Fork 0
170 lines (151 loc) · 5.09 KB
/
ci_cd.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
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
name: Deploy Backend to Homelab k3s
on:
push:
branches:
- main
env:
REGISTRY: ghcr.io
IMAGE_NAME: parsa-asgari/backend-homelab
jobs:
test:
runs-on: ubuntu-latest
# Service containers to run with `container-job`
services:
# Label used to access the service container
db:
# Docker Hub image
image: postgres:14.4-alpine
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
# Step 1: Checkout code
- name: Checkout code
uses: actions/checkout@v3
# Step 2: Set up Python
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.10'
# Step 3: Install dependencies
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Step 4: Run tests
- name: Run migrations & tests
run: piccolo migrations forwards all && pytest
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
POSTGRES_DB: postgres
build_and_push_image:
runs-on: ubuntu-latest
needs: [test]
permissions:
contents: write
packages: write
id-token: write
attestations: write
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Get semantic version
id: version
uses: paulhatch/semantic-version@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
major_pattern: '^feat!:'
minor_pattern: '^feat:'
patch_pattern: '^fix:'
default: patch
bump: auto
- name: Create Git tag
run: |
if git rev-parse ${{ steps.version.outputs.version }} >/dev/null 2>&1; then
echo "Tag ${{ steps.version.outputs.version }} already exists locally."
else
git tag ${{ steps.version.outputs.version }}
fi
if git ls-remote --tags origin | grep -q "refs/tags/${{ steps.version.outputs.version }}$"; then
echo "Tag ${{ steps.version.outputs.version }} already exists in remote."
else
git push origin ${{ steps.version.outputs.version }}
fi
- uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
labels: ${{ steps.meta.outputs.labels }}
load: false
create_github_release:
runs-on: ubuntu-latest
needs: [build_and_push_image]
permissions:
contents: write
steps:
- name: Create GitHub Release
uses: actions/create-release@v1
with:
tag_name: ${{ needs.build_and_push_image.outputs.version }}
release_name: Release ${{ needs.build_and_push_image.outputs.version }}
body: |
New release with Docker image version ${{ needs.build_and_push_image.outputs.version }} deployed to Kubernetes.
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
deploy:
runs-on: ubuntu-latest
needs: [build_and_push_image, create_github_release]
permissions:
contents: write
steps:
- name: Set up Kubernetes kubeconfig
run: |
echo "${{ secrets.KUBECONFIG_FILE }}" > kubeconfig
chmod 600 kubeconfig
env:
KUBECONFIG_FILE: ${{ secrets.KUBECONFIG_FILE }}
- name: Generate docker compose with version
uses: rondefreitas/[email protected]
with:
template: .kubernetes/backend-homelab-k8s-manifest.yml.j2
output_file: backend-homelab-k8s-manifest.yml
strict: true
variables: |
image=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build_and_push_image.outputs.version }}
- name: Deploy to Kubernetes
uses: azure/setup-kubectl@v3
- name: Apply Kubernetes manifests
run: |
sed -i "s|image:.*|image: ${{ secrets.DOCKER_USERNAME }}/my-app:${{ steps.version.outputs.version }}|" backend-homelab-k8s-manifest.yml
kubectl apply -f backend-homelab-k8s-manifest.yml --kubeconfig=kubeconfig
env:
KUBECONFIG: ${{ secrets.KUBECONFIG_FILE }}