Add homepage #14
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: React App Deployment | |
on: | |
push: | |
branches: [main, develop] | |
workflow_dispatch: | |
inputs: | |
environment: | |
description: "Environment to deploy" | |
required: true | |
type: choice | |
options: | |
- production | |
- Dev | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
environment: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.environment || (github.ref == 'refs/heads/main' && 'production' || '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 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 | |
if [ "${{ github.event_name == 'workflow_dispatch' && github.event.inputs.environment || (github.ref == 'refs/heads/main' && 'production' || 'Dev') }}" == "Dev" ]; then | |
cd /opt/ChanjoKe-HIE/hie | |
else | |
cd /opt/chanjoke-hie/hie | |
fi | |
sudo docker compose pull client | |
# recreate the client service with the new image | |
sudo docker compose up -d client --force-recreate | |
sudo docker logout | |
echo "${{ github.event_name == 'workflow_dispatch' && github.event.inputs.environment || (github.ref == 'refs/heads/main' && 'production' || 'Dev') }} deployment completed successfully" | |
EOF | |
rm -f private_key |