upload-github-release-asset.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env bash
  2. #
  3. # Uploads one or more files to a github release
  4. #
  5. # Prerequisites
  6. # 1) GITHUB_TOKEN defined in the environment
  7. # 2) TAG defined in the environment
  8. #
  9. set -e
  10. if [[ -z $1 ]]; then
  11. echo No files specified
  12. exit 1
  13. fi
  14. if [[ -z $GITHUB_TOKEN ]]; then
  15. echo Error: GITHUB_TOKEN not defined
  16. exit 1
  17. fi
  18. if [[ -z $CI_TAG ]]; then
  19. echo Error: CI_TAG not defined
  20. exit 1
  21. fi
  22. # Force CI_REPO_SLUG since sometimes
  23. # BUILDKITE_TRIGGERED_FROM_BUILD_PIPELINE_SLUG is not set correctly, causing the
  24. # artifact upload to fail
  25. CI_REPO_SLUG=anza-xyz/agave
  26. #if [[ -z $CI_REPO_SLUG ]]; then
  27. # echo Error: CI_REPO_SLUG not defined
  28. # exit 1
  29. #fi
  30. releaseId=$( \
  31. curl -s "https://api.github.com/repos/$CI_REPO_SLUG/releases/tags/$CI_TAG" \
  32. | grep -m 1 \"id\": \
  33. | sed -ne 's/^[^0-9]*\([0-9]*\),$/\1/p' \
  34. )
  35. echo "Github release id for $CI_TAG is $releaseId"
  36. for file in "$@"; do
  37. echo "--- Uploading $file to tag $CI_TAG of $CI_REPO_SLUG"
  38. curl \
  39. --verbose \
  40. --data-binary @"$file" \
  41. -H "Authorization: token $GITHUB_TOKEN" \
  42. -H "Content-Type: application/octet-stream" \
  43. "https://uploads.github.com/repos/$CI_REPO_SLUG/releases/$releaseId/assets?name=$(basename "$file")"
  44. echo
  45. done