Skip to content

Use node 20

Use node 20 #4

Workflow file for this run

name: React App Deployment
on:
push:
branches:
- main
- develop
workflow_dispatch:
jobs:
build_and_deploy_dev:
if: github.ref == 'refs/heads/develop'
runs-on: ubuntu-latest
environment: Dev
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Create env file
run: |
echo "VITE_SERVER_BASE_URL=${{ secrets.VITE_SERVER_BASE_URL }}" > .env
- name: Build & push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: docker/Dockerfile.prod
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/chanjokeclient:latest
build-args: |
VITE_SERVER_BASE_URL=${{ secrets.VITE_SERVER_BASE_URL }}
- name: Deploy to Dev server
env:
SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }}
run: |
echo "$SSH_PRIVATE_KEY" > private_key && chmod 600 private_key
ssh -o StrictHostKeyChecking=no -i private_key ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << EOF
CONTAINER_NAME=chanjoke
IMAGE_NAME=${{ secrets.DOCKER_USERNAME }}/chanjokeclient:latest
if docker ps -a --format '{{.Names}}' | grep -q \$CONTAINER_NAME; then
echo "Stopping and removing existing container"
docker stop \$CONTAINER_NAME || true
docker rm \$CONTAINER_NAME || true
fi
echo '${{ secrets.DOCKER_PASSWORD }}' | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
if [ \$? -ne 0 ]; then
echo "Error: Docker login failed"
exit 1
fi
docker pull --quiet \$IMAGE_NAME
if [ \$? -ne 0 ]; then
echo "Error: Failed to pull Docker image \$IMAGE_NAME"
exit 1
fi
sudo docker run -d --name \$CONTAINER_NAME -p 8098:3000 \$IMAGE_NAME
sudo docker logout
echo "Dev deployment completed successfully"
EOF
rm -f private_key
deploy_prod:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
steps:
- name: Deploy to Production server
env:
SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }}
run: |
echo "$SSH_PRIVATE_KEY" > private_key && chmod 600 private_key
ssh -o StrictHostKeyChecking=no -i private_key ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << EOF
CONTAINER_NAME=chanjoke
IMAGE_NAME=${{ secrets.DOCKER_USERNAME }}/chanjokeclient:latest
if docker ps -a --format '{{.Names}}' | grep -q \$CONTAINER_NAME; then
echo "Stopping and removing existing container"
docker stop \$CONTAINER_NAME || true
docker rm \$CONTAINER_NAME || true
fi
echo '${{ secrets.DOCKER_PASSWORD }}' | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
if [ \$? -ne 0 ]; then
echo "Error: Docker login failed"
exit 1
fi
docker pull --quiet \$IMAGE_NAME
if [ \$? -ne 0 ]; then
echo "Error: Failed to pull Docker image \$IMAGE_NAME"
exit 1
fi
sudo docker run -d --name \$CONTAINER_NAME -p 8098:3000 \$IMAGE_NAME
sudo docker logout
echo "Production deployment completed successfully"
EOF
rm -f private_key