deploy.yml (4321B)
1 name: Deploy dashboard 2 on: 3 push: 4 branches: [main] 5 6 jobs: 7 deploy: 8 runs-on: ubuntu-latest 9 env: 10 SITE_NAME: loop-benchmarking 11 SITE_BASE: /var/www/research 12 steps: 13 - uses: actions/checkout@v3 14 15 - name: Setup Node 16 uses: actions/setup-node@v4 17 with: 18 node-version: '22' 19 cache: 'npm' 20 cache-dependency-path: dashboard/package-lock.json 21 22 - name: Install dependencies 23 run: npm ci 24 working-directory: dashboard 25 26 - name: Build site 27 run: npm run build 28 working-directory: dashboard 29 30 - name: Install rsync 31 run: | 32 apt-get update && apt-get install -y rsync 33 34 - name: Ensure site structure exists 35 uses: https://github.com/appleboy/ssh-action@v1.2.0 36 with: 37 host: ${{ secrets.HOST }} 38 username: ${{ secrets.USERNAME }} 39 key: ${{ secrets.SSH_KEY }} 40 envs: SITE_NAME,SITE_BASE 41 script: | 42 BASE="${SITE_BASE}/${SITE_NAME}" 43 if [ ! -d "$BASE" ]; then 44 echo "First deploy — initializing $SITE_NAME" 45 mkdir -p "$BASE/blue" "$BASE/green" 46 ln -sfn "$BASE/blue" "$BASE/current" 47 else 48 echo "Site structure already exists" 49 fi 50 51 - name: Deploy files 52 run: | 53 echo "${{ secrets.SSH_KEY }}" > /tmp/deploy_key 54 chmod 600 /tmp/deploy_key 55 56 TARGET=$(ssh -i /tmp/deploy_key -o StrictHostKeyChecking=no \ 57 ${{ secrets.USERNAME }}@${{ secrets.HOST }} \ 58 "if [ \"\$(readlink ${SITE_BASE}/${SITE_NAME}/current)\" = \"${SITE_BASE}/${SITE_NAME}/blue\" ]; then echo green; else echo blue; fi") 59 60 echo "Deploying to $TARGET" 61 62 # Deploy dashboard build (exclude artifacts/ from --delete so 63 # a failed artifact sync doesn't wipe previously deployed games) 64 rsync -av --delete --exclude='artifacts/' \ 65 -e "ssh -i /tmp/deploy_key -o StrictHostKeyChecking=no" \ 66 ./dashboard/dist/ ${{ secrets.USERNAME }}@${{ secrets.HOST }}:${SITE_BASE}/${SITE_NAME}/$TARGET/ 67 68 # Deploy artifacts separately (not part of Astro build) 69 # Artifacts are large (12GB+) and not fully tracked in git. 70 # They are rsynced manually from the dev machine. Skip gracefully 71 # if the directory doesn't exist on this CI runner. 72 if [ -d ./artifacts ]; then 73 rsync -av \ 74 -e "ssh -i /tmp/deploy_key -o StrictHostKeyChecking=no" \ 75 ./artifacts/ ${{ secrets.USERNAME }}@${{ secrets.HOST }}:${SITE_BASE}/${SITE_NAME}/$TARGET/artifacts/ 76 else 77 echo "No artifacts directory found, skipping artifact sync" 78 fi 79 80 rm /tmp/deploy_key 81 82 - name: Switch symlink 83 uses: https://github.com/appleboy/ssh-action@v1.2.0 84 with: 85 host: ${{ secrets.HOST }} 86 username: ${{ secrets.USERNAME }} 87 key: ${{ secrets.SSH_KEY }} 88 envs: SITE_NAME,SITE_BASE 89 script: | 90 BASE="${SITE_BASE}/${SITE_NAME}" 91 if [ "$(readlink $BASE/current)" = "$BASE/blue" ]; then 92 NEW=green 93 else 94 NEW=blue 95 fi 96 ln -sfn "$BASE/$NEW" "$BASE/current" 97 systemctl reload nginx 98 echo "Switched $SITE_NAME to $NEW" 99 100 - name: Smoke test 101 run: | 102 sleep 2 103 curl -f https://${SITE_NAME}.research.statagroup.com/ || exit 1 104 105 - name: Rollback on failure 106 if: failure() 107 uses: https://github.com/appleboy/ssh-action@v1.2.0 108 with: 109 host: ${{ secrets.HOST }} 110 username: ${{ secrets.USERNAME }} 111 key: ${{ secrets.SSH_KEY }} 112 envs: SITE_NAME,SITE_BASE 113 script: | 114 BASE="${SITE_BASE}/${SITE_NAME}" 115 if [ -L "$BASE/current" ]; then 116 if [ "$(readlink $BASE/current)" = "$BASE/blue" ]; then 117 OLD=green 118 else 119 OLD=blue 120 fi 121 ln -sfn "$BASE/$OLD" "$BASE/current" 122 systemctl reload nginx 123 echo "Rolled back $SITE_NAME to $OLD" 124 else 125 echo "No previous deployment to roll back to" 126 fi