delete-deployment-preview.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. #!/bin/bash
  2. # Set the pipefail option.
  3. set -o pipefail
  4. # Get the Vercel API endpoints.
  5. GET_DEPLOYMENTS_ENDPOINT="https://api.vercel.com/v6/deployments"
  6. DELETE_DEPLOYMENTS_ENDPOINT="https://api.vercel.com/v13/deployments"
  7. # Create a list of deployments.
  8. deployments=$(curl -s -X GET "$GET_DEPLOYMENTS_ENDPOINT/?projectId=$VERCEL_PROJECT_ID&teamId=$VERCEL_ORG_ID" -H "Authorization: Bearer $VERCEL_TOKEN ")
  9. #deployments=$(curl -s -X GET "$GET_DEPLOYMENTS_ENDPOINT/?projectId=$VERCEL_PROJECT_ID" -H "Authorization: Bearer $VERCEL_TOKEN ")
  10. # Filter the deployments list by meta.base_hash === meta tag.
  11. filtered_deployments=$(echo -E $deployments | jq --arg META_TAG "$META_TAG" '[.deployments[] | select(.meta.base_hash | type == "string" and contains($META_TAG)) | .uid] | join(",")')
  12. filtered_deployments="${filtered_deployments//\"/}" # Remove double quotes
  13. # Clears the values from filtered_deployments
  14. IFS=',' read -ra values <<<"$filtered_deployments"
  15. echo "META_TAG ${META_TAG}"
  16. echo "Filtered deployments ${filtered_deployments}"
  17. # Iterate over the filtered deployments list.
  18. for uid in "${values[@]}"; do
  19. echo "Deleting ${uid}"
  20. delete_url="${DELETE_DEPLOYMENTS_ENDPOINT}/${uid}?teamId=${VERCEL_ORG_ID}"
  21. echo $delete_url
  22. # Make DELETE a request to the /v13/deployments/{id} endpoint.
  23. curl -X DELETE $delete_url -H "Authorization: Bearer $VERCEL_TOKEN"
  24. echo "Deleted!"
  25. done