draft_release_protoc_artifactbundle.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/bash
  2. # This script generates an artifactbundle for protoc. This artifactbundle
  3. # is used by the Swift package manger. The script is run by a GitHub action
  4. # to create protoc-vXXX releases with artifactbundles.
  5. set -eux
  6. AUTH="Authorization: token $GITHUB_TOKEN"
  7. # Fetch the latest stable release from protocolbuffers/protobuf
  8. upstream_response=$(curl -sH "$AUTH" "https://api.github.com/repos/protocolbuffers/protobuf/releases/latest")
  9. TAG=$(echo "$upstream_response" | grep -m 1 '"tag_name":' | cut -d '"' -f 4)
  10. # Remove 'v' prefix if present
  11. TAG="${TAG#v}"
  12. if [[ ! "$TAG" =~ ^[0-9]+\.[0-9]+$ ]]; then
  13. echo "Error: $TAG does not match the expected pattern"
  14. exit 1
  15. fi
  16. echo "Latest upstream protoc version: $TAG"
  17. # Check for the latest protoc-vXXX release in this swift-protobuf repo
  18. swift_protobuf_response=$(curl -sH "$AUTH" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/releases")
  19. CURRENT_PROTOC_TAG=$(echo "$swift_protobuf_response" | jq -r '.[] | select(.tag_name | startswith("protoc-v")) | .tag_name' | head -n 1)
  20. if [ -z "$CURRENT_PROTOC_TAG" ] || [ "$CURRENT_PROTOC_TAG" = "null" ]; then
  21. echo "No existing protoc-vXXX release found. This will be the initial release."
  22. CURRENT_PROTOC_VERSION=""
  23. else
  24. # Extract version from protoc-vX.Y format
  25. CURRENT_PROTOC_VERSION="${CURRENT_PROTOC_TAG#protoc-v}"
  26. echo "Current swift-protobuf protoc version: $CURRENT_PROTOC_VERSION"
  27. fi
  28. # Compare versions - if they match, no need to create a new release
  29. if [ "$CURRENT_PROTOC_VERSION" = "$TAG" ]; then
  30. echo "Protoc version $TAG is already released. No action needed."
  31. exit 0
  32. fi
  33. echo "Creating new protoc release: protoc-v$TAG"
  34. # Fetch all protoc release assets from protocolbuffers/protobuf
  35. curl -LJ --output protoc-$TAG-osx-x86_64.zip -H 'Accept: application/octet-stream' https://github.com/protocolbuffers/protobuf/releases/download/v$TAG/protoc-$TAG-osx-x86_64.zip
  36. curl -LJ --output protoc-$TAG-osx-aarch_64.zip -H 'Accept: application/octet-stream' https://github.com/protocolbuffers/protobuf/releases/download/v$TAG/protoc-$TAG-osx-aarch_64.zip
  37. curl -LJ --output protoc-$TAG-linux-aarch_64.zip -H 'Accept: application/octet-stream' https://github.com/protocolbuffers/protobuf/releases/download/v$TAG/protoc-$TAG-linux-aarch_64.zip
  38. curl -LJ --output protoc-$TAG-linux-x86_64.zip -H 'Accept: application/octet-stream' https://github.com/protocolbuffers/protobuf/releases/download/v$TAG/protoc-$TAG-linux-x86_64.zip
  39. curl -LJ --output protoc-$TAG-win64.zip -H 'Accept: application/octet-stream' https://github.com/protocolbuffers/protobuf/releases/download/v$TAG/protoc-$TAG-win64.zip
  40. # Fetch and validate license from protocolbuffers/protobuf
  41. curl -LJ --output LICENSE -H 'Accept: application/vnd.github.v3.raw' https://api.github.com/repos/protocolbuffers/protobuf/contents/LICENSE
  42. LICENSE_HASH=$(sha256sum LICENSE | cut -d ' ' -f 1)
  43. EXPECTED_HASH="6e5e117324afd944dcf67f36cf329843bc1a92229a8cd9bb573d7a83130fea7d"
  44. if [ "$LICENSE_HASH" != "$EXPECTED_HASH" ]; then
  45. echo "Error: License file has changed. Expected hash: $EXPECTED_HASH, Got: $LICENSE_HASH"
  46. exit 1
  47. fi
  48. # Unzip all assets
  49. mkdir protoc-$TAG.artifactbundle
  50. unzip -d protoc-$TAG.artifactbundle/protoc-$TAG-osx-x86_64 protoc-$TAG-osx-x86_64.zip
  51. unzip -d protoc-$TAG.artifactbundle/protoc-$TAG-osx-aarch_64 protoc-$TAG-osx-aarch_64.zip
  52. unzip -d protoc-$TAG.artifactbundle/protoc-$TAG-linux-aarch_64 protoc-$TAG-linux-aarch_64.zip
  53. unzip -d protoc-$TAG.artifactbundle/protoc-$TAG-linux-x86_64 protoc-$TAG-linux-x86_64.zip
  54. unzip -d protoc-$TAG.artifactbundle/protoc-$TAG-win64 protoc-$TAG-win64.zip
  55. # Copy license file into artifactbundle
  56. cp LICENSE protoc-$TAG.artifactbundle/
  57. # Create info.json for artifactbundle
  58. cat > protoc-$TAG.artifactbundle/info.json << EOF
  59. {
  60. "schemaVersion": "1.0",
  61. "artifacts": {
  62. "protoc": {
  63. "type": "executable",
  64. "version": "$TAG",
  65. "variants": [
  66. {
  67. "path": "protoc-$TAG-linux-x86_64/bin/protoc",
  68. "supportedTriples": ["x86_64-unknown-linux-gnu"]
  69. },
  70. {
  71. "path": "protoc-$TAG-linux-aarch_64/bin/protoc",
  72. "supportedTriples": ["aarch64-unknown-linux-gnu", "arm64-unknown-linux-gnu", "aarch64-unknown-linux", "arm64-unknown-linux"]
  73. },
  74. {
  75. "path": "protoc-$TAG-osx-x86_64/bin/protoc",
  76. "supportedTriples": ["x86_64-apple-macosx"]
  77. },
  78. {
  79. "path": "protoc-$TAG-osx-aarch_64/bin/protoc",
  80. "supportedTriples": ["arm64-apple-macosx"]
  81. },
  82. {
  83. "path": "protoc-$TAG-win64/bin/protoc.exe",
  84. "supportedTriples": ["x86_64-unknown-windows"]
  85. },
  86. ]
  87. }
  88. }
  89. }
  90. EOF
  91. # Zip artifactbundle
  92. zip -r protoc-$TAG.artifactbundle.zip protoc-$TAG.artifactbundle
  93. # Create a new draft release for protoc-vXXX
  94. echo "Creating draft release protoc-v$TAG"
  95. create_response=$(curl -sH "$AUTH" -X POST "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/releases" \
  96. -d "{
  97. \"tag_name\": \"protoc-artifactbundle-v$TAG\",
  98. \"name\": \"protoc v$TAG artifactbundle\",
  99. \"body\": \"Protoc artifactbundle for version $TAG\",
  100. \"draft\": true,
  101. \"prerelease\": false,
  102. \"make_latest\": \"false\"
  103. }")
  104. upload_url=$(echo "$create_response" | jq -r '.upload_url')
  105. release_id=$(echo "$create_response" | jq -r '.id')
  106. if [ -z "$upload_url" ] || [ "$upload_url" = "null" ] || [ -z "$release_id" ] || [ "$release_id" = "null" ]; then
  107. echo "Error: Failed to create draft release"
  108. echo "Response: $create_response"
  109. exit 1
  110. fi
  111. # Remove the {?name,label} template from upload_url
  112. upload_url=$(echo "$upload_url" | sed 's/{?name,label}//')
  113. echo "Created draft release with ID: $release_id"
  114. echo "Upload URL: $upload_url"
  115. # Upload asset
  116. echo "Uploading artifactbundle..."
  117. upload_response=$(curl --data-binary @protoc-$TAG.artifactbundle.zip -H "$AUTH" -H "Content-Type: application/octet-stream" "$upload_url?name=protoc-$TAG.artifactbundle.zip")
  118. echo "Upload completed successfully!"
  119. echo "Draft release protoc-v$TAG created with artifactbundle"