feat/255/Fixing typos and modified time of execution for cron job #561
Workflow file for this run
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: 🚀 Deploy | |
on: | |
push: | |
branches: | |
- main | |
- dev | |
tags: | |
- "*" | |
pull_request: {} | |
permissions: | |
actions: write | |
contents: read | |
jobs: | |
lint: | |
name: ⬣ ESLint | |
runs-on: ubuntu-latest | |
steps: | |
- name: 🛑 Cancel Previous Runs | |
if: ${{ env.GITHUB_ACTOR != 'nektos/act' }} | |
uses: styfle/cancel-workflow-action@0.11.0 | |
- name: ⬇️ Checkout repo | |
uses: actions/checkout@v3 | |
- name: ⎔ Setup node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20 | |
- name: 📥 Download deps | |
uses: bahmutov/npm-install@v1 | |
- name: 🔬 Lint | |
run: npm run lint | |
typecheck: | |
name: ʦ TypeScript | |
runs-on: ubuntu-latest | |
steps: | |
- name: 🛑 Cancel Previous Runs | |
if: ${{ env.GITHUB_ACTOR != 'nektos/act' }} | |
uses: styfle/cancel-workflow-action@0.11.0 | |
- name: ⬇️ Checkout repo | |
uses: actions/checkout@v3 | |
- name: ⎔ Setup node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20 | |
- name: 📥 Download deps | |
uses: bahmutov/npm-install@v1 | |
- name: 🔎 Type check | |
run: npm run typecheck --if-present | |
vitest: | |
name: ⚡ Vitest | |
runs-on: ubuntu-latest | |
env: | |
DATABASE_URL: postgresql://lab_user:lab_pass@localhost/lab_test | |
SESSION_SECRET: asdf_secret | |
steps: | |
- name: 🛑 Cancel Previous Runs | |
if: ${{ env.GITHUB_ACTOR != 'nektos/act' }} | |
uses: styfle/cancel-workflow-action@0.11.0 | |
- name: ⬇️ Checkout repo | |
uses: actions/checkout@v3 | |
- name: ⎔ Setup node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20 | |
- name: 📥 Download deps | |
uses: bahmutov/npm-install@v1 | |
- uses: harmon758/postgresql-action@v1 | |
with: | |
postgresql version: "12" | |
postgresql db: lab_test | |
postgresql user: lab_user | |
postgresql password: lab_pass | |
- name: 🛠 Setup Database | |
run: npx prisma migrate reset --force | |
- name: ⚡ Run vitest | |
run: npm run test | |
cypress: | |
name: ⚫️ Cypress | |
runs-on: ubuntu-latest | |
env: | |
DATABASE_URL: postgresql://lab_user:lab_pass@localhost/lab_test | |
SESSION_SECRET: asdf_secret | |
BASE_URL: http://localhost:3000 | |
AUTH0_CLIENT_ID: ${{ secrets.AUTH0_CLIENT_ID }} | |
AUTH0_DOMAIN: ${{ secrets.AUTH0_DOMAIN }} | |
AUTH0_CLIENT_SECRET: ${{ secrets.AUTH0_CLIENT_SECRET }} | |
steps: | |
- name: 🛑 Cancel Previous Runs | |
if: ${{ env.GITHUB_ACTOR != 'nektos/act' }} | |
uses: styfle/cancel-workflow-action@0.11.0 | |
- name: ⬇️ Checkout repo | |
uses: actions/checkout@v3 | |
- name: 🏄 Copy test env vars | |
run: cp .env.example .env | |
- name: ⎔ Setup node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20 | |
- uses: harmon758/postgresql-action@v1 | |
with: | |
postgresql version: "12" | |
postgresql db: lab_test | |
postgresql user: lab_user | |
postgresql password: lab_pass | |
- name: 📥 Download deps | |
uses: bahmutov/npm-install@v1 | |
- name: 🛠 Setup Database | |
run: npx prisma migrate reset --force | |
- name: ⚙️ Build | |
run: npm run build | |
- name: 🌳 Cypress run | |
uses: cypress-io/github-action@v5 | |
with: | |
install: false | |
start: npm run start:mocks | |
wait-on: "http://localhost:8811" | |
env: | |
PORT: "8811" | |
deploy: | |
name: 🚀 Deploy | |
runs-on: ubuntu-latest | |
environment: dev | |
needs: [lint, typecheck, vitest, cypress] | |
# only build/deploy main branch on pushes | |
if: ${{ (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev') && github.event_name == 'push' }} | |
env: | |
NODE_ENV: production | |
steps: | |
- name: 🛑 Cancel Previous Runs | |
if: ${{ env.GITHUB_ACTOR != 'nektos/act' }} | |
uses: styfle/cancel-workflow-action@0.11.0 | |
- name: ⬇️ Checkout repo | |
uses: actions/checkout@v3 | |
- name: 👀 Read app props | |
uses: Muchaszewski/read-json-action@1.0.0 | |
id: app_props | |
with: | |
path: "package.json" | |
properties: "[name, engines.node]" | |
- name: Prepare Server For Build | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.LIGHTSAIL_IP }} | |
username: ${{ secrets.LIGHTSAIL_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
rm -rf ~/projectlab/tmp | |
mkdir -p ~/projectlab/tmp | |
- name: Copy Files To Lightsail Instance | |
uses: wlixcc/SFTP-Deploy-Action@v1.0 | |
with: | |
username: ${{ secrets.LIGHTSAIL_USER }} | |
server: ${{ secrets.LIGHTSAIL_IP }} | |
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} | |
local_path: "./*" | |
remote_path: "/home/admin/projectlab/tmp/" | |
args: "-o ConnectTimeout=60" | |
- name: Configure Server | |
uses: appleboy/ssh-action@master | |
env: | |
DB_URL: ${{ secrets.DB_URL }} | |
DATABASE_URL: postgresql://admin:password@localhost/projectlab | |
SESSION_SECRET: ${{ secrets.SESSION_SECRET }} | |
BASE_URL: ${{ secrets.BASE_URL }} | |
AUTH0_CLIENT_ID: ${{ secrets.AUTH0_CLIENT_ID }} | |
AUTH0_DOMAIN: ${{ secrets.AUTH0_DOMAIN }} | |
AUTH0_CLIENT_SECRET: ${{ secrets.AUTH0_CLIENT_SECRET }} | |
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }} | |
GITHUB_KEY: ${{ secrets.API_GITHUB_KEY }} | |
with: | |
timeout: 60s | |
command_timeout: 30m | |
envs: DB_URL,DATABASE_URL,SESSION_SECRET,BASE_URL,AUTH0_CLIENT_ID,AUTH0_DOMAIN,AUTH0_CLIENT_SECRET,GOOGLE_APPLICATION_CREDENTIALS,GITHUB_KEY | |
host: ${{ secrets.LIGHTSAIL_IP }} | |
username: ${{ secrets.LIGHTSAIL_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script_stop: true | |
script: | | |
# ignore failure on pm2 stop (first time?) | |
pm2 stop all || true | |
echo "*** Step: *** Replace app folders" | |
rm -rf ~/projectlab/app | |
mkdir -p ~/projectlab/app | |
cp -R ~/projectlab/tmp/. ~/projectlab/app/ | |
cd ~/projectlab/app | |
echo "*** Step: *** Create .env file" | |
echo DATABASE_URL="$DATABASE_URL" >> .env | |
echo SESSION_SECRET="$SESSION_SECRET" >> .env | |
echo BASE_URL="$BASE_URL" >> .env | |
echo AUTH0_CLIENT_ID="$AUTH0_CLIENT_ID" >> .env | |
echo AUTH0_DOMAIN="$AUTH0_DOMAIN" >> .env | |
echo AUTH0_CLIENT_SECRET="$AUTH0_CLIENT_SECRET" >> .env | |
echo GOOGLE_APPLICATION_CREDENTIALS="$GOOGLE_APPLICATION_CREDENTIALS" >> .env | |
echo GITHUB_KEY="$GITHUB_KEY" >> .env | |
echo "*** Step: *** npm ci and build" | |
npm ci # install | |
npm run build | |
echo "*** Step: *** Load prod database and run migrations and seeds" | |
pg_dump --dbname $DB_URL --clean --if-exists > db.sql | |
sudo -u postgres psql -c "DROP DATABASE IF EXISTS projectlab;" | |
sudo -u postgres psql -c "CREATE DATABASE projectlab;" | |
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE projectlab TO admin;" | |
psql -d "$DATABASE_URL" < db.sql | |
npx -y prisma migrate deploy # first npx with -y to avoid prompt to install | |
npx prisma db seed | |
echo "*** Step: *** Schedule cron to update profiles" | |
chmod +x /home/admin/projectlab/app/profilesmigration.sh | |
echo "0 13 * * * /home/admin/projectlab/app/profilesmigration.sh" | crontab - | |
echo "*** Cron job to get github activity ****" | |
chmod +x /home/admin/projectlab/app/githubActivity.sh | |
{ | |
line="0 0 * * * /home/admin/projectlab/app/githubActivity.sh" | |
crontab -l | grep -Fv "$line" | |
echo "$line" | |
} | crontab - | |
echo "*** Step: *** Start pm2 service" | |
pm2 start ecosystem.config.js | |
pm2 save | |
echo "*** Step: ** Cleanup" | |
rm -rf ~/projectlab/tmp | |
deploy-prod: | |
name: 🚀 Deploy Prod | |
runs-on: ubuntu-latest | |
environment: prod | |
needs: [lint, typecheck, vitest, cypress] | |
# only build/deploy prod on new version tags (v1.0.0) | |
if: startsWith(github.ref, 'refs/tags/v') | |
env: | |
NODE_ENV: production | |
steps: | |
- name: 🛑 Cancel Previous Runs | |
if: ${{ env.GITHUB_ACTOR != 'nektos/act' }} | |
uses: styfle/cancel-workflow-action@0.11.0 | |
- name: ⬇️ Checkout repo | |
uses: actions/checkout@v3 | |
- name: 👀 Read app props | |
uses: Muchaszewski/read-json-action@1.0.0 | |
id: app_props | |
with: | |
path: "package.json" | |
properties: "[name, engines.node]" | |
- name: Prepare Server For Build | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.LIGHTSAIL_IP }} | |
username: ${{ secrets.LIGHTSAIL_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
rm -rf ~/projectlab/tmp | |
mkdir -p ~/projectlab/tmp | |
- name: Copy Files To Lightsail Instance | |
uses: wlixcc/SFTP-Deploy-Action@v1.0 | |
with: | |
username: ${{ secrets.LIGHTSAIL_USER }} | |
server: ${{ secrets.LIGHTSAIL_IP }} | |
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} | |
local_path: "./*" | |
remote_path: "/home/admin/projectlab/tmp/" | |
args: "-o ConnectTimeout=60" | |
- name: Configure Server | |
uses: appleboy/ssh-action@master | |
env: | |
DB_URL: ${{ secrets.DB_URL }} | |
# On prod DATABASE_URL = DB_URL | |
DATABASE_URL: ${{ secrets.DB_URL }} | |
SESSION_SECRET: ${{ secrets.SESSION_SECRET }} | |
BASE_URL: ${{ secrets.BASE_URL }} | |
AUTH0_CLIENT_ID: ${{ secrets.AUTH0_CLIENT_ID }} | |
AUTH0_DOMAIN: ${{ secrets.AUTH0_DOMAIN }} | |
AUTH0_CLIENT_SECRET: ${{ secrets.AUTH0_CLIENT_SECRET }} | |
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }} | |
GITHUB_KEY: ${{ secrets.API_GITHUB_KEY }} | |
with: | |
timeout: 60s | |
command_timeout: 30m | |
envs: DB_URL,DATABASE_URL,SESSION_SECRET,BASE_URL,AUTH0_CLIENT_ID,AUTH0_DOMAIN,AUTH0_CLIENT_SECRET,GOOGLE_APPLICATION_CREDENTIALS,GITHUB_KEY | |
host: ${{ secrets.LIGHTSAIL_IP }} | |
username: ${{ secrets.LIGHTSAIL_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script_stop: true | |
script: | | |
# ignore failure on pm2 delete (first time?) | |
# use delete to also reload .env variables | |
pm2 delete projectlab || true | |
echo "*** Step: *** Replace app folders" | |
rm -rf ~/projectlab/app | |
mkdir -p ~/projectlab/app | |
cp -R ~/projectlab/tmp/. ~/projectlab/app/ | |
cd ~/projectlab/app | |
echo "*** Step: *** Create .env file" | |
echo DATABASE_URL="$DATABASE_URL" >> .env | |
echo SESSION_SECRET="$SESSION_SECRET" >> .env | |
echo BASE_URL="$BASE_URL" >> .env | |
echo AUTH0_CLIENT_ID="$AUTH0_CLIENT_ID" >> .env | |
echo AUTH0_DOMAIN="$AUTH0_DOMAIN" >> .env | |
echo AUTH0_CLIENT_SECRET="$AUTH0_CLIENT_SECRET" >> .env | |
echo GOOGLE_APPLICATION_CREDENTIALS="$GOOGLE_APPLICATION_CREDENTIALS" >> .env | |
echo GITHUB_KEY="$GITHUB_KEY" >> .env | |
echo "*** Step: *** npm ci and build" | |
npm ci # install | |
npm run build | |
echo "*** Step: *** Load prod database and run migrations and seeds" | |
pg_dump --dbname $DB_URL --clean --if-exists > db.sql ## Just for a quick backup | |
npx -y prisma migrate deploy # first npx with -y to avoid prompt to install | |
npx prisma db seed | |
echo "*** Step: *** Schedule cron to update profiles" | |
chmod +x /home/admin/projectlab/app/profilesmigration.sh | |
echo "0 13 * * * /home/admin/projectlab/app/profilesmigration.sh" | crontab - | |
echo "*** Cron job to get github activity ****" | |
chmod +x /home/admin/projectlab/app/githubActivity.sh | |
{ | |
line="0 9,21 * * * /home/admin/projectlab/app/githubActivity.sh" | |
crontab -l | grep -Fv "$line" | |
echo "$line" | |
} | crontab - | |
echo "*** Step: *** Start pm2 service" | |
pm2 start ecosystem.config.js | |
pm2 save | |
echo "*** Step: ** Cleanup" | |
rm -rf ~/projectlab/tmp |