|
| 1 | +name: Create Release on Merge |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [closed] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + pull-requests: read |
| 10 | + actions: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + create-release: |
| 14 | + if: >- |
| 15 | + github.event.pull_request.merged == true && |
| 16 | + github.event.pull_request.base.ref == 'main' && |
| 17 | + github.event.pull_request.head.ref == 'pre-release' |
| 18 | + runs-on: ubuntu-latest |
| 19 | + env: |
| 20 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 21 | + steps: |
| 22 | + - name: Checkout repository |
| 23 | + uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Determine version from Package.swift |
| 28 | + id: get_version |
| 29 | + shell: bash |
| 30 | + run: | |
| 31 | + VERSION=$(grep -o 'releases/download/[^/]*/loroFFI\.xcframework\.zip' Package.swift | sed 's|releases/download/||' | sed 's|/loroFFI\.xcframework\.zip||' | head -n1) |
| 32 | + if [ -z "$VERSION" ]; then |
| 33 | + echo "Failed to determine version from Package.swift" >&2 |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 37 | +
|
| 38 | + - name: Determine merge commit |
| 39 | + id: get_sha |
| 40 | + shell: bash |
| 41 | + run: | |
| 42 | + SHA="${{ github.event.pull_request.merge_commit_sha }}" |
| 43 | + if [ -z "$SHA" ]; then |
| 44 | + echo "Merge commit sha is empty" >&2 |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + echo "sha=$SHA" >> $GITHUB_OUTPUT |
| 48 | +
|
| 49 | + - name: Find pre-release workflow run and download artifact |
| 50 | + shell: bash |
| 51 | + run: | |
| 52 | + VERSION=${{ steps.get_version.outputs.version }} |
| 53 | + TAG_NAME="${VERSION}-pre-release" |
| 54 | + |
| 55 | + echo "Looking for workflow run for tag: $TAG_NAME" |
| 56 | + |
| 57 | + # Get the commit SHA of the tag |
| 58 | + TAG_SHA="" |
| 59 | + if git rev-parse --verify "$TAG_NAME" >/dev/null 2>&1; then |
| 60 | + TAG_SHA=$(git rev-parse "$TAG_NAME") |
| 61 | + echo "Tag $TAG_NAME points to commit: $TAG_SHA" |
| 62 | + fi |
| 63 | + |
| 64 | + # Find workflow runs and check multiple criteria |
| 65 | + RUNS_JSON=$(gh run list \ |
| 66 | + --workflow "Pre-release Build and PR" \ |
| 67 | + --status success \ |
| 68 | + --limit 20 \ |
| 69 | + --json databaseId,headSha,displayTitle,createdAt) |
| 70 | + |
| 71 | + RUN_ID="" |
| 72 | + |
| 73 | + # Method 1: Try to match by tag SHA |
| 74 | + if [ -n "$TAG_SHA" ]; then |
| 75 | + RUN_ID=$(echo "$RUNS_JSON" | jq -r ".[] | select(.headSha == \"$TAG_SHA\") | .databaseId" | head -n1) |
| 76 | + if [ -n "$RUN_ID" ]; then |
| 77 | + echo "Found run by SHA match: $RUN_ID" |
| 78 | + fi |
| 79 | + fi |
| 80 | + |
| 81 | + # Method 2: Try to match by displayTitle |
| 82 | + if [ -z "$RUN_ID" ]; then |
| 83 | + RUN_ID=$(echo "$RUNS_JSON" | jq -r ".[] | select(.displayTitle == \"Pre-release $TAG_NAME\") | .databaseId" | head -n1) |
| 84 | + if [ -n "$RUN_ID" ]; then |
| 85 | + echo "Found run by displayTitle match: $RUN_ID" |
| 86 | + fi |
| 87 | + fi |
| 88 | + |
| 89 | + # Method 3: Use the most recent successful run as fallback |
| 90 | + if [ -z "$RUN_ID" ]; then |
| 91 | + RUN_ID=$(echo "$RUNS_JSON" | jq -r '.[0].databaseId') |
| 92 | + if [ -n "$RUN_ID" ]; then |
| 93 | + echo "Using most recent successful run as fallback: $RUN_ID" |
| 94 | + fi |
| 95 | + fi |
| 96 | + |
| 97 | + if [ -z "$RUN_ID" ]; then |
| 98 | + echo "Could not find any suitable workflow run" >&2 |
| 99 | + exit 1 |
| 100 | + fi |
| 101 | + |
| 102 | + gh run download "$RUN_ID" --name loroFFI.xcframework.zip --dir . |
| 103 | + # Normalize location if downloaded into a directory |
| 104 | + if [ -d loroFFI.xcframework.zip ] && [ -f loroFFI.xcframework.zip/loroFFI.xcframework.zip ]; then |
| 105 | + mv loroFFI.xcframework.zip/loroFFI.xcframework.zip . |
| 106 | + rm -rf loroFFI.xcframework.zip |
| 107 | + fi |
| 108 | + if [ ! -f loroFFI.xcframework.zip ]; then |
| 109 | + echo "Artifact loroFFI.xcframework.zip not found after download" >&2 |
| 110 | + exit 1 |
| 111 | + fi |
| 112 | +
|
| 113 | + - name: Create tag on merge commit |
| 114 | + shell: bash |
| 115 | + run: | |
| 116 | + VERSION=${{ steps.get_version.outputs.version }} |
| 117 | + SHA=${{ steps.get_sha.outputs.sha }} |
| 118 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 119 | + git config user.name "github-actions[bot]" |
| 120 | + # Create or move tag to the merge commit |
| 121 | + if git rev-parse "$VERSION" >/dev/null 2>&1; then |
| 122 | + git tag -d "$VERSION" |
| 123 | + fi |
| 124 | + git tag -a "$VERSION" -m "Release version $VERSION" "$SHA" |
| 125 | + git push --force origin "$VERSION" |
| 126 | +
|
| 127 | + - name: Create GitHub Release |
| 128 | + shell: bash |
| 129 | + run: | |
| 130 | + VERSION=${{ steps.get_version.outputs.version }} |
| 131 | + gh release create "$VERSION" loroFFI.xcframework.zip \ |
| 132 | + --title "Release $VERSION" \ |
| 133 | + --notes "Automated release for version $VERSION. Includes pre-built loroFFI.xcframework.zip." |
| 134 | +
|
0 commit comments