fix: copy static files from correct location #12
Workflow file for this run
This file contains hidden or 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 Realworld React | |
| on: | |
| push: | |
| branches: [release] | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.ref }} | |
| cancel-in-progress: true | |
| env: | |
| NODE_VERSION: 22 | |
| jobs: | |
| ci: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: release | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: ${{ env.NODE_VERSION }} | |
| cache: "yarn" | |
| - name: Install dependencies | |
| run: yarn install --frozen-lockfile | |
| - name: Lint code | |
| run: yarn lint | |
| - name: Check types | |
| run: yarn check-types | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| needs: ci | |
| environment: production | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: release | |
| - name: Login to Docker Hub | |
| run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin | |
| - name: Build and push Docker image | |
| run: | | |
| docker build \ | |
| --build-arg VITE_APP_API_URL=${{ secrets.VITE_APP_API_URL }} \ | |
| --build-arg VITE_APP_APP_URL=${{ secrets.VITE_APP_APP_URL }} \ | |
| -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.APP_NAME }}:latest \ | |
| . | |
| docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.APP_NAME }}:latest | |
| - name: Logout from Docker Hub | |
| run: docker logout | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: build-and-push | |
| environment: production | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: release | |
| - name: Add VPS to known hosts | |
| run: | | |
| mkdir -p ~/.ssh | |
| ssh-keyscan -p ${{ secrets.SSH_PORT }} -H ${{ secrets.VPS_HOST }} >> ~/.ssh/known_hosts | |
| - name: Deploy to VPS | |
| env: | |
| SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
| SSH_PORT: ${{ secrets.SSH_PORT }} | |
| VPS_HOST: ${{ secrets.VPS_HOST }} | |
| APP_NAME: ${{ secrets.APP_NAME }} | |
| DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
| NGINX_SITE_NAME: ${{ secrets.NGINX_SITE_NAME }} | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "$SSH_PRIVATE_KEY" > ~/.ssh/deploy_key | |
| chmod 600 ~/.ssh/deploy_key | |
| # Copy nginx-host.conf to VPS for comparison | |
| scp -P $SSH_PORT -i ~/.ssh/deploy_key nginx/nginx-host.conf deploy@$VPS_HOST:/tmp/nginx-host.conf | |
| ssh -p $SSH_PORT -i ~/.ssh/deploy_key deploy@$VPS_HOST << EOF | |
| set -e | |
| # Pull the latest Docker image | |
| docker pull $DOCKERHUB_USERNAME/$APP_NAME:latest | |
| # Stop and remove old container if exists | |
| docker stop $APP_NAME 2>/dev/null || true | |
| docker rm $APP_NAME 2>/dev/null || true | |
| # Run new container | |
| docker run -d \ | |
| --name $APP_NAME \ | |
| --restart unless-stopped \ | |
| -p 127.0.0.1:3000:80 \ | |
| $DOCKERHUB_USERNAME/$APP_NAME:latest | |
| # Check if nginx-host.conf has changed and update if needed | |
| if ! cmp -s /tmp/nginx-host.conf /etc/nginx/sites-available/$NGINX_SITE_NAME.conf; then | |
| sudo cp /tmp/nginx-host.conf /etc/nginx/sites-available/$NGINX_SITE_NAME.conf | |
| sudo ln -sf /etc/nginx/sites-available/$NGINX_SITE_NAME.conf /etc/nginx/sites-enabled/$NGINX_SITE_NAME.conf | |
| # Test and reload nginx | |
| sudo nginx -t && sudo nginx -s reload | |
| fi | |
| # Cleanup | |
| rm -f /tmp/nginx-host.conf | |
| docker system prune -f | |
| EOF | |
| rm -f ~/.ssh/deploy_key |