Merge pull request #34 from brandonnorsworthy/updates #31
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: cicd-dev | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
cicd-dev: | |
runs-on: ubuntu-latest | |
environment: dev | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
- name: Install dependencies | |
run: npm install | |
- name: Build the project | |
run: npm run build | |
- name: Run tests | |
env: | |
NODE_ENV: ${{ github.env }} | |
EXPRESS_PORT: ${{ secrets.EXPRESS_PORT }} | |
ALLOWED_ORIGINS: ${{ secrets.ALLOWED_ORIGINS }} | |
POSTGRES_USER: ${{ secrets.POSTGRES_USER }} | |
POSTGRES_HOST: ${{ secrets.POSTGRES_HOST }} | |
POSTGRES_DATABASE: ${{ secrets.POSTGRES_DATABASE }} | |
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }} | |
POSTGRES_PORT: ${{ secrets.POSTGRES_PORT }} | |
POSTGRES_SSL: ${{ secrets.POSTGRES_SSL }} | |
JWT_SECRET: ${{ secrets.JWT_SECRET }} | |
BCRYPT_PEPPER: ${{ secrets.BCRYPT_PEPPER }} | |
BCRYPT_SALT_ROUNDS: ${{ secrets.BCRYPT_SALT_ROUNDS }} | |
run: npm test | |
- name: Copy files to dist | |
run: | | |
cp package.json dist/ | |
cp package-lock.json dist/ | |
cp *.pem dist/ | |
- name: Create environment file | |
run: | | |
echo "NODE_ENV=${{ github.env }}" > dist/.env | |
echo "EXPRESS_PORT=${{ secrets.EXPRESS_PORT }}" >> dist/.env | |
echo "ALLOWED_ORIGINS=${{ secrets.ALLOWED_ORIGINS }}" >> dist/.env | |
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> dist/.env | |
echo "POSTGRES_HOST=${{ secrets.POSTGRES_HOST }}" >> dist/.env | |
echo "POSTGRES_DATABASE=${{ secrets.POSTGRES_DATABASE }}" >> dist/.env | |
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> dist/.env | |
echo "POSTGRES_PORT=${{ secrets.POSTGRES_PORT }}" >> dist/.env | |
echo "POSTGRES_SSL=${{ secrets.POSTGRES_SSL }}" >> dist/.env | |
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> dist/.env | |
echo "BCRYPT_PEPPER=${{ secrets.BCRYPT_PEPPER }}" >> dist/.env | |
echo "BCRYPT_SALT_ROUNDS=${{ secrets.BCRYPT_SALT_ROUNDS }}" >> dist/.env | |
- name: Create tarball | |
run: | | |
tar -czvf dist.tar.gz -C dist . | |
- name: Setup SSH | |
run: | | |
mkdir -p ~/.ssh/ | |
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan -H ${{ secrets.EC2_IP }} >> ~/.ssh/known_hosts | |
- name: Deploy to EC2 | |
run: | | |
scp -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ./dist.tar.gz ${{ secrets.EC2_USER }}@${{ secrets.EC2_IP }}:/home/ec2-user/ | |
ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_IP }} << 'EOF' | |
cd ~ | |
pm2 stop app | |
rm -rf app | |
mkdir app | |
mv dist.tar.gz ./app | |
cd app | |
tar -xzvf dist.tar.gz | |
npm install | |
pm2 start npm --name app -- run start | |
EOF |