From 2de86916b9acff55127699829a0a9530edbdb610 Mon Sep 17 00:00:00 2001 From: DimaGashko Date: Wed, 5 Mar 2025 18:42:58 +0200 Subject: [PATCH 1/2] dev env --- .github/workflows/deploy.yaml | 102 +++++++++++++++++----------------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index efea02f..5c7113c 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -1,55 +1,57 @@ name: Deploy on: - push: - branches: - - main - workflow_dispatch: + push: + branches: + - main + - dev + workflow_dispatch: jobs: - deploy: - runs-on: ubuntu-latest - - steps: - - name: Checkout Repository - uses: actions/checkout@v4 - - - name: Setup Node - uses: actions/setup-node@v4 - with: - node-version: 22 - cache: 'npm' - - - name: Create .env - run: | - echo "${{ secrets.DOT_ENV }}" > .env - - - name: Install Dependencies - run: npm ci - - - name: Build Project - run: npm run build - - - name: Setup SSH - run: | - mkdir -p ~/.ssh - echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa - chmod 600 ~/.ssh/id_rsa - ssh-keyscan -H ${{ secrets.REMOTE_HOST }} >> ~/.ssh/known_hosts - - - name: Deploy with rsync - run: | - TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S") - RELEASE_DIR="${{ secrets.REMOTE_PATH }}/releases/$TIMESTAMP" - CURRENT_LINK="${{ secrets.REMOTE_PATH }}/current" - - ssh ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} << EOF - mkdir -p $RELEASE_DIR - EOF - - rsync -avz --delete \ - ./dist/ ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }}:$RELEASE_DIR - - ssh ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} << EOF - ln -sfn $RELEASE_DIR $CURRENT_LINK - EOF + deploy: + runs-on: ubuntu-latest + environment: ${{ github.ref == 'refs/heads/main' && 'prod' || 'dev' }} + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 22 + cache: 'npm' + + - name: Create .env + run: | + echo "${{ secrets.DOT_ENV }}" > .env + + - name: Install Dependencies + run: npm ci + + - name: Build Project + run: npm run build + + - name: Setup SSH + run: | + mkdir -p ~/.ssh + echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H ${{ secrets.REMOTE_HOST }} >> ~/.ssh/known_hosts + + - name: Deploy with rsync + run: | + TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S") + RELEASE_DIR="${{ secrets.REMOTE_PATH }}/releases/$TIMESTAMP" + CURRENT_LINK="${{ secrets.REMOTE_PATH }}/current" + + ssh ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} << EOF + mkdir -p $RELEASE_DIR + EOF + + rsync -avz --delete \ + ./dist/ ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }}:$RELEASE_DIR + + ssh ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} << EOF + ln -sfn $RELEASE_DIR $CURRENT_LINK + EOF From d1692d80b4f9050768ef0266a51cec3d7d7b950d Mon Sep 17 00:00:00 2001 From: DimaGashko Date: Wed, 5 Mar 2025 18:47:48 +0200 Subject: [PATCH 2/2] clean up old releases --- .github/workflows/deploy.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 5c7113c..b30d198 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -55,3 +55,10 @@ jobs: ssh ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} << EOF ln -sfn $RELEASE_DIR $CURRENT_LINK EOF + + - name: Cleanup Old Releases (Keep Last 25) + run: | + ssh ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} << EOF + cd ${{ secrets.REMOTE_PATH }}/releases + ls -dt */ | tail -n +26 | xargs rm -rf -- + EOF