Skip to content

Commit 34d4b9c

Browse files
authored
Automated Deployment Workflow
* Test workflow to SSH into EC2 * Dockerfile to containerize frontend * Update workflow to build docker image and run container * Deploy to different locations based on branch * Use proper reference to environment variables * Revert to singular Dockerfile * Remove unused environment var from dockerfile * Remove old docker image before building new one * Test with real dev site * Swap ports of dev and prod * Add nextauth to env of container * Write secret directly to env file * Echo newline into env file * Take secret part out - that doesn't need to happen every deploy * Add main branch to workflow
1 parent 6300007 commit 34d4b9c

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

.github/workflows/deploy-dev.yaml

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Test deploy on EC2
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- dev
8+
9+
jobs:
10+
deploy:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
- name: Set environment variables
18+
run: |
19+
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
20+
echo "DEPLOY_DIR=/data/MusicCPRFEProd" >> $GITHUB_ENV
21+
echo "IMAGE_NAME=frontend:prod" >> $GITHUB_ENV
22+
echo "CONTAINER_NAME=frontend-prod" >> $GITHUB_ENV
23+
echo "HOST_PORT=3003" >> $GITHUB_ENV
24+
else
25+
echo "DEPLOY_DIR=/data/MusicCPRFEDev" >> $GITHUB_ENV
26+
echo "IMAGE_NAME=frontend:dev" >> $GITHUB_ENV
27+
echo "CONTAINER_NAME=frontend-dev" >> $GITHUB_ENV
28+
echo "HOST_PORT=3000" >> $GITHUB_ENV
29+
fi
30+
31+
- name: Set up SSH
32+
run: |
33+
mkdir -p ~/.ssh
34+
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
35+
chmod 600 ~/.ssh/id_rsa
36+
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
37+
38+
- name: Deploy to EC2
39+
run: |
40+
ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
41+
set -e
42+
echo "Deploying to ${{ env.DEPLOY_DIR }}..."
43+
44+
cd ${{ env.DEPLOY_DIR }}
45+
46+
echo "Pulling latest changes..."
47+
git pull origin ${{ github.ref_name }}
48+
49+
echo "Stopping and removing old container"
50+
docker stop ${{ env.CONTAINER_NAME }}|| true
51+
docker rm ${{ env.CONTAINER_NAME }}|| true
52+
53+
echo "Removing old container"
54+
docker rmi ${{ env.IMAGE_NAME }} || true
55+
56+
echo "Building new image..."
57+
docker build -t ${{ env.IMAGE_NAME }} .
58+
59+
echo "Starting new container..."
60+
docker run -d --name ${{ env.CONTAINER_NAME }} -p ${{ env.HOST_PORT }}:3000 ${{ env.IMAGE_NAME }}
61+
EOF

Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:18
2+
3+
WORKDIR /app
4+
5+
COPY package.json package-lock.json ./
6+
RUN npm install
7+
8+
COPY . .
9+
10+
RUN npm run build
11+
12+
CMD ["npm", "run", "start"]
13+

0 commit comments

Comments
 (0)