LIVE - Update and Restart the NutriLog
App
#20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: LIVE - Update and Restart the `NutriLog` App | |
# Trigger action manually | |
on: workflow_dispatch | |
env: | |
APP_DIR: /home/${{vars.USER}}/inst/app/nutrilog/ # Directory of the application | |
PRJ: nutrilog # The name of the project | |
SERVICE: appNutriLog.service # The name of the service to manage | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out the repository on the GitHub environment. | |
uses: actions/checkout@v3 | |
- name: Install dependencies and build the project. | |
run: | | |
npm install --omit=optional --omit=dev | |
- name: Add application files to the archive. | |
uses: montudor/action-zip@v1 | |
with: | |
args: zip -qq --exclude=*.git* -r ${{env.PRJ}}.zip ./ | |
- name: Validate the existence of the archive. | |
run: | | |
pwd | |
ls -lh | |
- name: Validate SSH connection. | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{vars.HOST}} | |
username: ${{vars.USER}} | |
key: ${{secrets.SSH_KEY}} | |
script: | | |
pwd | |
whoami | |
- name: Copy files to Remote Server. | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{vars.HOST}} | |
username: ${{vars.USER}} | |
key: ${{secrets.SSH_KEY}} | |
source: ${{env.PRJ}}.zip | |
target: ${{env.APP_DIR}} | |
- name: Extract files on Remote Server and switch site to the new instance. | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{vars.HOST}} | |
username: ${{vars.USER}} | |
key: ${{secrets.SSH_KEY}} | |
script: | | |
cd "${{env.APP_DIR}}/" | |
rm -fr prod.old | |
mkdir prod.new | |
unzip ${{env.PRJ}}.zip -d ./prod.new/ &> /dev/null | |
# Load nvm and set up node | |
source ~/.nvm/nvm.sh | |
nvm use node | |
# Stop the service and run database export to separate backup file | |
sudo /bin/systemctl stop ${{env.SERVICE}} | |
node ./prod/bin/tequila.mjs db-export -f data.nutrilog.json | |
# Switch the app version then reset the db structure (export / init / import) | |
mv ./prod ./prod.old | |
mv ./prod.new ./prod | |
cp ./prod.old/cfg/local* ./prod/cfg/ | |
node ./prod/bin/tequila.mjs db-reset | |
# Start the service | |
sudo /bin/systemctl start ${{env.SERVICE}} | |
# Rename the archive with a timestamp | |
TIMESTAMP=$(date +%Y%m%d-%H%M%S) | |
mv ${{env.PRJ}}.zip ${{env.PRJ}}-${TIMESTAMP}.zip | |
echo "Archive renamed to: ${{env.PRJ}}-${TIMESTAMP}.zip" | |
ls -lh ${{env.APP_DIR}} |