-
Notifications
You must be signed in to change notification settings - Fork 0
99 lines (86 loc) · 3.03 KB
/
deploy-to-VM.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
name: Deploy to VM
on:
push:
branches:
- master
workflow_dispatch:
inputs:
version:
description: 'Manual version override (optional)'
required: false
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Automatic Semantic Versioning
id: semver
if: github.event_name == 'push'
uses: anothrNick/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.ACTIONS_TOKEN }}
DEFAULT_BUMP: patch
RELEASE_BRANCHES: master
TAG_PREFIX: v
- name: Set Version
run: echo "VERSION=${{ steps.semver.outputs.new_tag || github.event.inputs.version || 'latest' }}" >> $GITHUB_ENV
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/backend:${{ env.VERSION }}
- name: Create app deirectory if doesn't exist yet
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.VM_HOST }}
username: ${{ secrets.VM_USERNAME }}
key: ${{ secrets.VM_PRIVATE_KEY }}
passphrase: ${{ secrets.VM_SSH_PASSPHRASE }}
script: |
mkdir -p ~/rivnegray
- name: Copy docker-compose file
uses: appleboy/scp-action@master
with:
host: ${{ secrets.VM_HOST }}
username: ${{ secrets.VM_USERNAME }}
key: ${{ secrets.VM_PRIVATE_KEY }}
passphrase: ${{ secrets.VM_SSH_PASSPHRASE }}
source: "docker-compose.app.yml"
target: "~/rivnegray/"
overwrite: true
- name: Deploy to VM
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.VM_HOST }}
username: ${{ secrets.VM_USERNAME }}
key: ${{ secrets.VM_PRIVATE_KEY }}
passphrase: ${{ secrets.VM_SSH_PASSPHRASE }}
script: |
export BACKEND_IMAGE_TAG=${{ env.VERSION }}
cd ~/rivnegray/
docker-compose -f docker-compose.app.yml down
docker-compose -f docker-compose.app.yml up -d
- name: Update .env with backend version
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.VM_HOST }}
username: ${{ secrets.VM_USERNAME }}
key: ${{ secrets.VM_PRIVATE_KEY }}
passphrase: ${{ secrets.VM_SSH_PASSPHRASE }}
script: |
cd ~/rivnegray/
if grep -q 'BACKEND_IMAGE_TAG=' .env; then
sed -i 's|BACKEND_IMAGE_TAG=.*|BACKEND_IMAGE_TAG=${{ env.VERSION }}|' .env
else
echo "BACKEND_IMAGE_TAG=${{ env.VERSION }}" >> .env
fi