-
Notifications
You must be signed in to change notification settings - Fork 0
72 lines (59 loc) · 2.27 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
name: Docker Image CICD
run-name: "${{ github.event.head_commit.message }} (${{ github.sha }})"
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
docker-check-health:
name: Ensure Docker container passes health check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Create Docker network
run: docker network create -d bridge traefik-network
- name: Build the Docker image
run: docker compose up --detach --wait --wait-timeout 30
deploy:
name: Deploy to the production server
runs-on: ubuntu-latest
needs: docker-check-health
steps:
- uses: actions/checkout@v4
- name: Tailscale connection
uses: tailscale/github-action@v2
with:
oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }}
oauth-secret: ${{ secrets.TS_OAUTH_SECRET }}
tags: tag:cicd-pipeline
- name: SSH setup
run: |
mkdir -p ~/.ssh/
echo "$SSH_FINGERPRINT" > ~/.ssh/known_hosts
echo "$SSH_KEY" > ~/.ssh/srv.key
chmod 600 ~/.ssh/srv.key
cat >>~/.ssh/config <<END
Host srv
HostName $SSH_HOST
User $SSH_USER
IdentityFile ~/.ssh/srv.key
StrictHostKeyChecking yes
END
env:
SSH_USER: ${{ secrets.USERNAME }}
SSH_KEY: ${{ secrets.SSH_PRIV_KEY }}
SSH_HOST: ${{ secrets.TS_SERVER_IP }}
SSH_FINGERPRINT: ${{ secrets.SSH_FINGERPRINT }}
- name: Bring down Traefik container on server
run: ssh srv 'if [ -f ${{ github.event.repository.name }}/compose.yaml ]; then cd ${{ github.event.repository.name }} && docker compose down; fi '
- name: Delete directory on server
continue-on-error: true
run: ssh srv 'rm -rf ${{ github.event.repository.name }}'
- name: Deploy files
run: rsync -avh --exclude=".[!.]*" * srv:/root/${{ github.event.repository.name }}
- name: Create Docker network if it does not exist
run: ssh srv 'if docker network ls --format "{{.Name}}" | grep -q traefik-network; then echo "Network already exists"; else docker network create traefik-network;fi'
- name: Always bring up container on server
if: ${{ always() }}
run: ssh srv 'cd ${{ github.event.repository.name }} && docker compose up -d'