cleanup #900
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: cleanup | |
| on: | |
| delete: | |
| jobs: | |
| cleanup: | |
| if: ${{ github.event_name == 'workflow_dispatch' || (!contains(fromJson('["main", "release"]'), github.event.ref)) }} | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Install Turso CLI | |
| run: curl -sSfL https://get.tur.so/install.sh | bash | |
| env: | |
| TURSO_INSTALL_SKIP_SIGNUP: 'true' | |
| - name: Delete preview database | |
| run: | | |
| set -o errexit | |
| set -o nounset | |
| set -o pipefail | |
| ref="${{ github.event.ref }}" | |
| branch="${ref##refs/heads/}" | |
| clean_branch="${branch//[^a-z0-9\-]/x}" | |
| # Truncate ref to a prefix + hash suffix to avoid collisions while maintaining readability | |
| # and ensuring total database name stays within 58 character limit | |
| # "payloadcms-preview-" is 19 characters, leaving 39 for the branch | |
| if [ ${#clean_branch} -gt 39 ]; then | |
| prefix="${clean_branch:0:31}" | |
| hash=$(echo -n "${clean_branch}" | sha256sum | cut -c1-7) | |
| clean_branch="${prefix}-${hash}" | |
| fi | |
| name="payloadcms-preview-${clean_branch}" | |
| echo "[INFO] Attempting to delete database ${name}" | |
| # Switch to the correct organization | |
| /home/runner/.turso/turso org switch nwac | |
| # Check if database exists before attempting to delete | |
| if /home/runner/.turso/turso db show "${name}" >/dev/null 2>&1; then | |
| echo "[INFO] Database ${name} exists, deleting..." | |
| /home/runner/.turso/turso db destroy --yes "${name}" | |
| echo "[INFO] Successfully deleted database ${name}" | |
| else | |
| echo "[ERROR] Database ${name} does not exist, expected it to be there for cleanup" | |
| exit 1 | |
| fi | |
| env: | |
| TURSO_API_TOKEN: ${{ secrets.TURSO_API_TOKEN }} | |
| - name: Install Vercel CLI | |
| run: npm install -g vercel | |
| - name: Remove Vercel Environment Variables | |
| run: | | |
| set -o errexit | |
| set -o nounset | |
| set -o pipefail | |
| ref="${{ github.event.ref }}" | |
| branch="${ref##refs/heads/}" | |
| if [ "$branch" = "main" ] || [ "$branch" = "release" ]; then | |
| echo "[ERROR] Refusing to delete environment variables from production branch: ${branch}" | |
| exit 1 | |
| fi | |
| ENV_VARS=( | |
| "DATABASE_URI" | |
| "DATABASE_AUTH_TOKEN" | |
| "PAYLOAD_SEED_PASSWORD" | |
| "NEXT_PUBLIC_ROOT_DOMAIN" | |
| ) | |
| echo "[INFO] Removing environment variables for branch: ${branch}" | |
| for VAR in "${ENV_VARS[@]}"; do | |
| echo "[INFO] Removing environment variable: $VAR" | |
| vercel env rm "$VAR" preview "$branch" --yes --token=${{ secrets.VERCEL_TOKEN }} || echo "[WARN] Variable $VAR not found or already removed" | |
| done | |
| env: | |
| VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} | |
| VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} | |
| VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} | |
| - name: Remove Vercel deployments and aliases | |
| run: | | |
| set -o errexit | |
| set -o nounset | |
| set -o pipefail | |
| ref="${{ github.event.ref }}" | |
| branch="${ref##refs/heads/}" | |
| echo "Cleaning up deployments and aliases for branch: $branch" | |
| if [ "$branch" = "main" ] || [ "$branch" = "release" ]; then | |
| echo "[ERROR] Refusing to delete deployments and aliases from production branch: ${branch}" | |
| exit 1 | |
| fi | |
| # Get all deployments for the project filtered by branch | |
| vercel list --scope=nwac --meta githubCommitRef=$branch --token=${{ secrets.VERCEL_TOKEN }} 2>/dev/null | while read -r DEPLOYMENT_URL; do | |
| if [ ! -z "$DEPLOYMENT_URL" ]; then | |
| echo "Found deployment: $DEPLOYMENT_URL" | |
| # Remove the deployment and its aliases | |
| echo "Removing deployment: $DEPLOYMENT_URL" | |
| vercel remove --scope=nwac "$DEPLOYMENT_URL" --yes --token=${{ secrets.VERCEL_TOKEN }} || echo "[WARN] Failed to remove deployment: $DEPLOYMENT_URL" | |
| fi | |
| done | |
| env: | |
| VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} | |
| VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} | |
| VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} |