Skip to content

Commit 953288b

Browse files
authored
chore: new release ci (#27)
1 parent 57b02ee commit 953288b

File tree

2 files changed

+167
-16
lines changed

2 files changed

+167
-16
lines changed

.github/workflows/pre-release.yaml

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
name: Pre-release Build and Publish
1+
name: Pre-release Build and PR
2+
run-name: Pre-release ${{ github.ref_name }}
23

34
on:
45
push:
56
tags:
67
- '*-pre-release'
78

9+
permissions:
10+
contents: write
11+
pull-requests: write
12+
813
jobs:
914
build-and-release:
1015
runs-on: macos-latest
@@ -34,6 +39,12 @@ jobs:
3439
CHECKSUM=$(openssl dgst -sha256 loroFFI.xcframework.zip | awk '{print $2}')
3540
echo "checksum=$CHECKSUM" >> $GITHUB_OUTPUT
3641
42+
- name: Upload XCFramework artifact
43+
uses: actions/upload-artifact@v4
44+
with:
45+
name: loroFFI.xcframework.zip
46+
path: loroFFI.xcframework.zip
47+
3748
- name: Update Package.swift
3849
run: |
3950
VERSION=${{ steps.get_version.outputs.version }}
@@ -50,25 +61,31 @@ jobs:
5061
-e "s|\"https://github.com/loro-dev/loro-swift.git\", from: \"[0-9.]*\"|\"https://github.com/loro-dev/loro-swift.git\", from: \"${VERSION}\"|" \
5162
README.md
5263
53-
- name: Commit and push changes
64+
- name: Commit and push changes to pre-release branch
5465
run: |
5566
VERSION=${{ steps.get_version.outputs.version }}
5667
git config --local user.email "github-actions[bot]@users.noreply.github.com"
5768
git config --local user.name "github-actions[bot]"
69+
# Ensure local pre-release branch points to current commit
70+
git fetch origin pre-release:pre-release || true
71+
git checkout -B pre-release "$GITHUB_SHA"
5872
git add Package.swift README.md Sources/*
59-
git commit -m "chore: update version to ${VERSION}"
60-
git tag -a "${VERSION}" -m "Release version ${VERSION}"
61-
git push origin HEAD:main
62-
git push origin "${VERSION}"
73+
git commit -m "chore: update version to ${VERSION}" || echo "No changes to commit"
74+
git push origin pre-release
6375
64-
- name: Create Release
65-
uses: softprops/action-gh-release@v1
66-
with:
67-
tag_name: ${{ steps.get_version.outputs.version }}
68-
name: Release ${{ steps.get_version.outputs.version }}
69-
files: loroFFI.xcframework.zip
70-
draft: false
71-
prerelease: false
72-
generate_release_notes: true
76+
- name: Create or update Pull Request to main
7377
env:
74-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
79+
run: |
80+
VERSION=${{ steps.get_version.outputs.version }}
81+
CHECKSUM=${{ steps.sha256.outputs.checksum }}
82+
PR_NUMBER=$(gh pr list --base main --head pre-release --state open --json number --jq '.[0].number')
83+
if [ -z "$PR_NUMBER" ]; then
84+
gh pr create \
85+
--base main \
86+
--head pre-release \
87+
--title "Release ${VERSION}" \
88+
--body "This PR updates Package.swift and README.md to version ${VERSION}.\n\nSHA256 checksum: ${CHECKSUM}.\n\nThe XCFramework artifact has been uploaded to this workflow run."
89+
else
90+
echo "PR #$PR_NUMBER already exists."
91+
fi
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)