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.

61 lines
1.9 KiB

  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. # returns 0 means success, otherwise error.
  9. ######## Public functions #####################
  10. #domain keyfile certfile cafile fullchain
  11. netlify_deploy() {
  12. _cdomain="$1"
  13. _ckey="$2"
  14. _ccert="$3"
  15. _cca="$4"
  16. _cfullchain="$5"
  17. _debug _cdomain "$_cdomain"
  18. _debug _ckey "$_ckey"
  19. _debug _ccert "$_ccert"
  20. _debug _cca "$_cca"
  21. _debug _cfullchain "$_cfullchain"
  22. if [ -z "$Netlify_ACCESS_TOKEN" ]; then
  23. _err "Netlify_ACCESS_TOKEN is not defined."
  24. return 1
  25. else
  26. _savedomainconf Netlify_ACCESS_TOKEN "$Netlify_ACCESS_TOKEN"
  27. fi
  28. if [ -z "$Netlify_SITE_ID" ]; then
  29. _err "Netlify_SITE_ID is not defined."
  30. return 1
  31. else
  32. _savedomainconf Netlify_SITE_ID "$Netlify_SITE_ID"
  33. fi
  34. _info "Deploying certificate to Netlify..."
  35. ## upload certificate
  36. string_ccert=$(sed 's/$/\\n/' "$_ccert" | tr -d '\n')
  37. string_cca=$(sed 's/$/\\n/' "$_cca" | tr -d '\n')
  38. string_key=$(sed 's/$/\\n/' "$_ckey" | tr -d '\n')
  39. _request_body="{\"certificate\":\"$string_ccert\",\"key\":\"$string_key\",\"ca_certificates\":\"$string_cca\"}"
  40. _debug _request_body "$_request_body"
  41. _debug Netlify_ACCESS_TOKEN "$Netlify_ACCESS_TOKEN"
  42. export _H1="Authorization: Bearer $Netlify_ACCESS_TOKEN"
  43. _response=$(_post "$_request_body" "https://api.netlify.com/api/v1/sites/$Netlify_SITE_ID/ssl" "" "POST" "application/json")
  44. if _contains "$_response" "\"error\""; then
  45. _err "Error in deploying $_cdomain certificate to Netlify."
  46. _err "$_response"
  47. return 1
  48. fi
  49. _debug response "$_response"
  50. _info "Domain $_cdomain certificate successfully deployed to Netlify."
  51. return 0
  52. }