You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.1 KiB

7 months ago
7 months ago
  1. #!/usr/bin/env sh
  2. # Script to deploy certificate to Netlify
  3. # https://docs.netlify.com/api/get-started/#authentication
  4. # https://open-api.netlify.com/#tag/sniCertificate
  5. # This deployment required following variables
  6. # export Netlify_ACCESS_TOKEN="Your Netlify Access Token"
  7. # export Netlify_SITE_ID="Your Netlify Site ID"
  8. # If have more than one SITE ID
  9. # export Netlify_SITE_ID="SITE_ID_1 SITE_ID_2"
  10. # returns 0 means success, otherwise error.
  11. ######## Public functions #####################
  12. #domain keyfile certfile cafile fullchain
  13. netlify_deploy() {
  14. _cdomain="$1"
  15. _ckey="$2"
  16. _ccert="$3"
  17. _cca="$4"
  18. _cfullchain="$5"
  19. _debug _cdomain "$_cdomain"
  20. _debug _ckey "$_ckey"
  21. _debug _ccert "$_ccert"
  22. _debug _cca "$_cca"
  23. _debug _cfullchain "$_cfullchain"
  24. if [ -z "$Netlify_ACCESS_TOKEN" ]; then
  25. _err "Netlify_ACCESS_TOKEN is not defined."
  26. return 1
  27. else
  28. _savedomainconf Netlify_ACCESS_TOKEN "$Netlify_ACCESS_TOKEN"
  29. fi
  30. if [ -z "$Netlify_SITE_ID" ]; then
  31. _err "Netlify_SITE_ID is not defined."
  32. return 1
  33. else
  34. _savedomainconf Netlify_SITE_ID "$Netlify_SITE_ID"
  35. fi
  36. _info "Deploying certificate to Netlify..."
  37. ## upload certificate
  38. string_ccert=$(sed 's/$/\\n/' "$_ccert" | tr -d '\n')
  39. string_cca=$(sed 's/$/\\n/' "$_cca" | tr -d '\n')
  40. string_key=$(sed 's/$/\\n/' "$_ckey" | tr -d '\n')
  41. for SITE_ID in $Netlify_SITE_ID; do
  42. _request_body="{\"certificate\":\"$string_ccert\",\"key\":\"$string_key\",\"ca_certificates\":\"$string_cca\"}"
  43. _debug _request_body "$_request_body"
  44. _debug Netlify_ACCESS_TOKEN "$Netlify_ACCESS_TOKEN"
  45. export _H1="Authorization: Bearer $Netlify_ACCESS_TOKEN"
  46. _response=$(_post "$_request_body" "https://api.netlify.com/api/v1/sites/$SITE_ID/ssl" "" "POST" "application/json")
  47. if _contains "$_response" "\"error\""; then
  48. _err "Error in deploying $_cdomain certificate to Netlify SITE_ID $SITE_ID."
  49. _err "$_response"
  50. return 1
  51. fi
  52. _debug response "$_response"
  53. _info "Domain $_cdomain certificate successfully deployed to Netlify SITE_ID $SITE_ID."
  54. done
  55. return 0
  56. }