-
-
Notifications
You must be signed in to change notification settings - Fork 3
84 lines (70 loc) · 2.53 KB
/
build-and-deploy.yml
File metadata and controls
84 lines (70 loc) · 2.53 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
name: Build & Deploy
run-name: Build & Deploy
on:
workflow_call:
inputs:
target_env:
description: 'The deployment environment e.g. production or development'
required: true
type: string
jobs:
build:
name: Run Build Process
runs-on: ubuntu-latest
environment: ${{ inputs.target_env }}
steps:
- name: Checkout code
# Checks out the repository code.
uses: actions/checkout@v4
- name: Load env file
run: echo "${{ secrets.APPLICATION_ENV_FILE }}" > .env
- name: Setup Node.js ${{ inputs.node-version }}
# Sets up NodeJS environment
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- name: Install Dependencies
# Install dependencies
run: npm ci
- name: Build Project
# Builds project
run: npm run build --if-present
- name: Upload build artifact (build)
# Uploads build output
uses: actions/upload-artifact@v4
with:
name: ${{ vars.PROJECT_NAME }}
path: build
retention-days: 1
deploy:
needs: build
name: Deploy
runs-on: ubuntu-latest
environment: ${{ inputs.target_env }}
steps:
- name: Checkout code
# Checks out the repository code.
uses: actions/checkout@v4
- name: Set up SSH key
# Sets up the SSH key for the server.
run: |
# Create the .ssh directory if it doesn't exist.
mkdir -p ~/.ssh
# Write the SSH private key to file.
echo "${{ secrets.DEPLOYMENT_SSH_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
# Add the server to known_hosts to avoid authenticity prompts.
ssh-keyscan -H ${{ secrets.DEPLOYMENT_SERVER }} >> ~/.ssh/known_hosts
- name: Download build artifacts
# Downloads build folder
uses: actions/download-artifact@v4
with:
name: ${{ vars.PROJECT_NAME }}
path: ${{ vars.PROJECT_NAME }}
- name: Deploy files to VPS
# Clears existing files and deploy new files
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }} \
"mkdir -p /var/www/rcb/${{ vars.PROJECT_NAME }} && rm -rf /var/www/rcb/${{ vars.PROJECT_NAME }}/*"
scp -o StrictHostKeyChecking=no -r ${{ vars.PROJECT_NAME }}/* \
${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }}:/var/www/rcb/${{ vars.PROJECT_NAME }}/