nice clean recipes pear.dunkirk.sh
recipes
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add rollback

+94 -6
+94 -6
.github/workflows/deploy.yml
··· 25 25 with: 26 26 go-version-file: go.mod 27 27 28 + - name: Install cross-compiler 29 + run: sudo apt-get update && sudo apt-get install -y gcc-aarch64-linux-gnu 30 + 28 31 - name: Build 29 - run: GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -ldflags "-X main.gitHash=$(git rev-parse --short HEAD)" -o pear . 32 + run: GOOS=linux GOARCH=arm64 CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc go build -ldflags "-X main.gitHash=$(git rev-parse --short HEAD)" -o pear . 30 33 31 34 - name: Setup Tailscale 32 35 uses: tailscale/github-action@v4 ··· 42 45 echo "StrictHostKeyChecking accept-new" >> ~/.ssh/config 43 46 44 47 - name: Deploy 48 + id: deploy 45 49 run: | 50 + C="\033[36m" G="\033[32m" Y="\033[33m" D="\033[2m" R="\033[0m" 51 + echo "start=$(date +%s)" >> "$GITHUB_OUTPUT" 52 + PREV_SHORT=$(ssh pear@terebithia "cd ~/app && git rev-parse --short HEAD") 53 + PREV_FULL=$(ssh pear@terebithia "cd ~/app && git rev-parse HEAD") 54 + 55 + echo "prev_short=$PREV_SHORT" >> "$GITHUB_OUTPUT" 56 + echo "prev_full=$PREV_FULL" >> "$GITHUB_OUTPUT" 57 + 58 + echo -e "${C}:: pear${R} ${D}${PREV_SHORT}${R}" 59 + 60 + # Snapshot DB before deploy 61 + ssh pear@terebithia "cp /var/lib/pear/data/pear.db /var/lib/pear/data/pear.db.pre-deploy 2>/dev/null || true" 62 + 63 + # Pull latest code 64 + ssh pear@terebithia << 'PULL' 65 + set -e 66 + cd ~/app 67 + git fetch --all --quiet 68 + git reset --hard origin/main --quiet 69 + PULL 70 + 71 + NEW_SHORT=$(ssh pear@terebithia "cd ~/app && git rev-parse --short HEAD") 72 + NEW_FULL=$(ssh pear@terebithia "cd ~/app && git rev-parse HEAD") 73 + echo "new_short=$NEW_SHORT" >> "$GITHUB_OUTPUT" 74 + echo "new_full=$NEW_FULL" >> "$GITHUB_OUTPUT" 75 + 76 + echo -e "${C}:: pear${R} ${D}${PREV_SHORT} → ${NEW_SHORT}${R}" 77 + 78 + # Upload binary and restart 79 + echo "::group::upload & restart" 46 80 scp pear pear@terebithia:~/app/pear-new 47 - ssh pear@terebithia << 'DEPLOY' 81 + ssh pear@terebithia << 'INSTALL' 48 82 set -e 49 83 cd ~/app 50 84 chmod +x pear-new 51 85 mv -f pear-new pear 52 86 sudo /run/current-system/sw/bin/systemctl restart pear.service 53 - DEPLOY 87 + INSTALL 88 + echo "::endgroup::" 89 + 90 + echo -e "${G}:: deployed pear${R}" 54 91 55 92 - name: Health check 93 + id: health 56 94 run: | 57 95 for i in $(seq 1 12); do 58 96 if curl -sf -o /dev/null "https://pear.dunkirk.sh"; then 59 - echo "Healthy" 97 + echo "status=healthy" >> "$GITHUB_OUTPUT" 60 98 exit 0 61 99 fi 62 100 echo "Attempt $i/12 failed" 63 101 sleep 5 64 102 done 65 - echo "Health check failed" 66 - exit 1 103 + echo "status=failed" >> "$GITHUB_OUTPUT" 104 + exit 1 105 + 106 + - name: Summary 107 + if: always() 108 + run: | 109 + DURATION="$(($(date +%s) - ${{ steps.deploy.outputs.start }}))s" 110 + PREV_SHORT="${{ steps.deploy.outputs.prev_short }}" 111 + PREV_FULL="${{ steps.deploy.outputs.prev_full }}" 112 + NEW_SHORT="${{ steps.deploy.outputs.new_short }}" 113 + NEW_FULL="${{ steps.deploy.outputs.new_full }}" 114 + HEALTH="${{ steps.health.outputs.status }}" 115 + REPO="https://github.com/${{ github.repository }}" 116 + 117 + if [ "${{ job.status }}" = "success" ]; then STATUS="Deployed"; else STATUS="Failed"; fi 118 + 119 + cat >> "$GITHUB_STEP_SUMMARY" << EOF 120 + | status | commits | health | 121 + |---|---|---| 122 + | \`${STATUS}\` in \`${DURATION}\` | [\`${PREV_SHORT}\`](${REPO}/commit/${PREV_FULL}) → [\`${NEW_SHORT}\`](${REPO}/commit/${NEW_FULL}) | \`${HEALTH:-skipped}\` | 123 + EOF 124 + 125 + - name: Rollback on failure 126 + if: failure() 127 + run: | 128 + echo "::warning::Deploy failed — rolling back pear" 129 + 130 + ssh pear@terebithia << 'ROLLBACK' 131 + set -e 132 + RED="\033[31m" Y="\033[33m" G="\033[32m" D="\033[2m" R="\033[0m" 133 + cd ~/app 134 + 135 + PREV=$(cat /tmp/pear-prev-commit 2>/dev/null || echo "") 136 + if [ -n "$PREV" ]; then 137 + SHORT=$(echo "$PREV" | cut -c1-7) 138 + echo -e "${Y}:: rolling back to ${SHORT}${R}" 139 + sudo /run/current-system/sw/bin/systemctl stop pear.service || true 140 + 141 + if [ -f /var/lib/pear/data/pear.db.pre-deploy ]; then 142 + echo -e "${Y}:: restoring database snapshot${R}" 143 + cp /var/lib/pear/data/pear.db.pre-deploy /var/lib/pear/data/pear.db 144 + fi 145 + 146 + git reset --hard "$PREV" --quiet 147 + CGO_ENABLED=1 go build -ldflags "-X main.gitHash=$(git rev-parse --short HEAD)" -o pear . 148 + sudo /run/current-system/sw/bin/systemctl restart pear.service 149 + echo -e "${G}:: rolled back to ${SHORT}${R}" 150 + else 151 + echo -e "${RED}:: no previous commit recorded, cannot rollback${R}" 152 + exit 1 153 + fi 154 + ROLLBACK