-
Notifications
You must be signed in to change notification settings - Fork 0
128 lines (110 loc) · 4.33 KB
/
deploy-server.yaml
File metadata and controls
128 lines (110 loc) · 4.33 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
name: Build and Push Docker Images
on:
push:
branches:
- master
paths:
- "server/**"
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME_API: ${{ github.repository }}/fastapi
IMAGE_NAME_CELERY: ${{ github.repository }}/celery
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for FastAPI
id: meta-api
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_API }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix={{branch}}-
- name: Extract metadata for Celery
id: meta-celery
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_CELERY }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix={{branch}}-
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push FastAPI Docker image
uses: docker/build-push-action@v5
with:
context: ./server
file: ./server/Dockerfile
push: true
tags: ${{ steps.meta-api.outputs.tags }}
labels: ${{ steps.meta-api.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and push Celery Docker image
uses: docker/build-push-action@v5
with:
context: ./server
file: ./server/Dockerfile.celery
push: true
tags: ${{ steps.meta-celery.outputs.tags }}
labels: ${{ steps.meta-celery.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Generate summary
run: |
echo "## Docker Images Built and Pushed 🚀" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### FastAPI Image" >> $GITHUB_STEP_SUMMARY
echo "- **Registry:** ${{ env.REGISTRY }}" >> $GITHUB_STEP_SUMMARY
echo "- **Repository:** ${{ env.IMAGE_NAME_API }}" >> $GITHUB_STEP_SUMMARY
echo "- **Tags:** ${{ steps.meta-api.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Celery Image" >> $GITHUB_STEP_SUMMARY
echo "- **Registry:** ${{ env.REGISTRY }}" >> $GITHUB_STEP_SUMMARY
echo "- **Repository:** ${{ env.IMAGE_NAME_CELERY }}" >> $GITHUB_STEP_SUMMARY
echo "- **Tags:** ${{ steps.meta-celery.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
deploy-to-azure:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Ansible
run: |
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y
ansible-galaxy collection install community.docker
ansible --version
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/azure_vm_key
chmod 600 ~/.ssh/azure_vm_key
ssh-keyscan -H ${{ secrets.AZURE_VM_HOST }} >> ~/.ssh/known_hosts
- name: Deploy with Ansible
run: |
ansible-playbook -i ./infra/backend/ansible/inventory.ini ./infra/backend/ansible/redeploy.yml \
--extra-vars "@infra/backend/ansible/group_vars/server-redeploy.yml" \
-e "fastapi_image=${{ env.REGISTRY }}/$(echo ${{ env.IMAGE_NAME_API }}:${{ github.ref_name }}-${GITHUB_SHA::7} | tr '[:upper:]' '[:lower:]')" \
-e "celery_image=${{ env.REGISTRY }}/$(echo ${{ env.IMAGE_NAME_CELERY }}:${{ github.ref_name }}-${GITHUB_SHA::7} | tr '[:upper:]' '[:lower:]')"