2023-06-07 08:19:45 +00:00
|
|
|
name: Code Deployment and Environment Setup
|
2023-05-17 10:27:22 +00:00
|
|
|
on:
|
2023-05-17 10:34:32 +00:00
|
|
|
push:
|
|
|
|
branches:
|
2023-05-17 16:21:45 +00:00
|
|
|
- main
|
2023-05-17 10:27:22 +00:00
|
|
|
jobs:
|
|
|
|
build:
|
2023-06-07 08:19:45 +00:00
|
|
|
name: Run remote SSH commands
|
2023-05-17 10:27:22 +00:00
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
2023-06-07 08:19:45 +00:00
|
|
|
- name: Clone or pull repository
|
|
|
|
uses: appleboy/ssh-action@v0.1.10
|
2023-05-17 10:27:22 +00:00
|
|
|
with:
|
|
|
|
host: ${{ secrets.HOST }}
|
|
|
|
username: ${{ secrets.USER }}
|
|
|
|
key: ${{ secrets.KEY }}
|
2023-06-07 08:19:45 +00:00
|
|
|
script_stop: false
|
2023-05-17 10:27:22 +00:00
|
|
|
script: |
|
2023-05-17 14:27:48 +00:00
|
|
|
if [ -d ${{ secrets.PATH }} ]; then
|
|
|
|
cd ${{ secrets.PATH }}
|
|
|
|
git pull
|
|
|
|
else
|
2023-06-07 08:19:45 +00:00
|
|
|
mkdir -p ${{ secrets.PATH }}
|
2023-05-17 14:27:48 +00:00
|
|
|
cd ${{ secrets.PATH }}
|
2023-06-15 13:06:24 +00:00
|
|
|
git clone git@github.com:${{ github.repository }}.git .
|
2023-05-17 14:27:48 +00:00
|
|
|
fi
|
2023-06-07 08:19:45 +00:00
|
|
|
|
|
|
|
- name: Update environment variables
|
|
|
|
uses: appleboy/ssh-action@v0.1.10
|
|
|
|
with:
|
|
|
|
host: ${{ secrets.HOST }}
|
|
|
|
username: ${{ secrets.USER }}
|
|
|
|
key: ${{ secrets.KEY }}
|
|
|
|
script_stop: false
|
|
|
|
script: |
|
|
|
|
# Bash function to replace env variables
|
|
|
|
replace_env() {
|
|
|
|
local env_var=$1
|
|
|
|
local new_value=$2
|
2023-06-07 10:08:29 +00:00
|
|
|
awk -v var="${env_var}" -v val="${new_value}" -F '=' '{OFS=FS} $1 == var {$2 = val} 1' .env > .env.tmp && mv .env.tmp .env
|
2023-06-07 08:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cd ${{ secrets.PATH }}
|
|
|
|
|
|
|
|
# Replace .env with vars
|
2023-06-07 08:53:39 +00:00
|
|
|
replace_env 'NAME' '${{ vars.NAME }}'
|
|
|
|
replace_env 'ASTRO_HOST' '${{ vars.ASTRO_HOST }}'
|
|
|
|
replace_env 'PAYLOAD_HOST' '${{ vars.PAYLOAD_HOST }}'
|
|
|
|
replace_env 'PAYLOAD_URL' '${{ vars.PAYLOAD_URL }}'
|
|
|
|
replace_env 'PAYLOAD_PORT' '${{ vars.PAYLOAD_PORT }}'
|
2023-06-07 08:19:45 +00:00
|
|
|
|
|
|
|
# Replace .env with secrets
|
2023-06-07 08:53:39 +00:00
|
|
|
replace_env 'PAYLOAD_SECRET' '${{ secrets.PAYLOAD_SECRET }}'
|
2023-06-15 13:15:21 +00:00
|
|
|
replace_env 'MONGODB_URI' 'mongodb://${{ secrets.MONGODB_USER }}:${{ secrets.MONGODB_PW }}@mongo:27017'
|
2023-06-07 08:53:39 +00:00
|
|
|
replace_env 'MONGODB_USER' '${{ secrets.MONGODB_USER }}'
|
|
|
|
replace_env 'MONGODB_PW' '${{ secrets.MONGODB_PW }}'
|
|
|
|
replace_env 'TOKEN' '${{ secrets.TOKEN }}'
|
2023-06-07 08:19:45 +00:00
|
|
|
|
2023-06-15 13:36:50 +00:00
|
|
|
# Append GitHub repository to .env
|
|
|
|
echo "REPOSITORY=${{ github.repository }}" >> .env
|
|
|
|
|
2023-06-07 08:42:55 +00:00
|
|
|
mkdir -p ./astro
|
|
|
|
cp .env ./astro/.env
|
2023-06-07 08:19:45 +00:00
|
|
|
|
|
|
|
- name: Start Production Services
|
|
|
|
uses: appleboy/ssh-action@v0.1.10
|
|
|
|
with:
|
|
|
|
host: ${{ secrets.HOST }}
|
|
|
|
username: ${{ secrets.USER }}
|
|
|
|
key: ${{ secrets.KEY }}
|
|
|
|
script: |
|
|
|
|
cd ${{ secrets.PATH }}
|
2023-07-28 15:02:19 +00:00
|
|
|
yarn prod payload
|
|
|
|
yarn prod astro
|