我有一个 GitHub Actions 工作流程,每当 PR 关闭和合并时就会触发该工作流程。此操作会自动生成 APK 并将其附加到带有特定标签的新 GitHub 版本。
但是,我还想自动将 PR 的正文/消息复制到 GitHub 版本的消息正文中。这是为了维护该版本中引入或更改的内容的记录。
当前工作流程
name: Build and Release
on:
pull_request:
branches:
- master
types:
- closed
jobs:
build-and-release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0 # Fetch all tags
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '20'
- name: Determine New Version and Tag
id: version
run: |
echo $(git describe --tags --abbrev=0)
CURRENT_VERSION=$(git describe --tags --abbrev=0)
echo "$CURRENT_VERSION"
MAJOR_VERSION=$(echo $CURRENT_VERSION | cut -d. -f1)
MINOR_VERSION=$(echo $CURRENT_VERSION | cut -d. -f2)
PATCH_VERSION=$(echo $CURRENT_VERSION | cut -d. -f3)
echo "$MAJOR_VERSION"
echo "$MINOR_VERSION"
echo "$PATCH_VERSION"
NEW_PATCH_VERSION=$((PATCH_VERSION+1))
NEW_VERSION="$MAJOR_VERSION.$MINOR_VERSION.$NEW_PATCH_VERSION"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "GitHub Actions"
git tag "$NEW_VERSION"
git push origin "$NEW_VERSION"
- name: Extract PR Body
id: extract-pr-body
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_ID=$(echo $GITHUB_REF | sed -E 's/refs\/pull\/([0-9]+)\/merge/\1/')
PR_BODY=$(gh pr view $PR_ID --json body -q .body)
PR_BODY_ESCAPED=$(echo "$PR_BODY" | sed ':a;N;$!ba;s/\n/\ /g') # Convert to single string with newline escapes
echo "PR_BODY_ESCAPED=$PR_BODY_ESCAPED" >> $GITHUB_ENV
- name: Install Dependencies
run: npm install
- name: Build Android Release
run: |
mkdir -p android/app/src/main/assets/
npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
cd android
rm -r app/src/main/res/drawable-*
./gradlew assembleRelease
- name: Rename APK
run: |
mv android/app/build/outputs/apk/release/app-release.apk MyAPP-${{ env.NEW_VERSION }}.apk
- name: Create GitHub Release and Upload APK
run: |
gh auth login --with-token <<< ${{ secrets.GITHUB_TOKEN }}
gh release create ${{ env.NEW_VERSION }} \
MyAPP-${{ env.NEW_VERSION }}.apk \
--title "${{ env.NEW_VERSION }}" \
--notes "Release notes for ${{ env.NEW_VERSION }}: <br /> <br /> ${{ env.PR_BODY_ESCAPED }}"
我遇到的错误
Run PR_ID=$(echo $GITHUB_REF | sed -E 's/refs\/pull\/([0-9]+)\/merge/\1/')
no pull requests found for branch "refs/heads/master"
Error: Process completed with exit code 1.
实际上这有效
name: Build and Release
on:
pull_request:
branches:
- master
types:
- closed
jobs:
build-and-release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0 # Fetch all tags
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '20'
- name: Determine New Version and Tag
id: version
run: |
echo $(git describe --tags --abbrev=0)
CURRENT_VERSION=$(git describe --tags --abbrev=0)
echo "$CURRENT_VERSION"
MAJOR_VERSION=$(echo $CURRENT_VERSION | cut -d. -f1)
MINOR_VERSION=$(echo $CURRENT_VERSION | cut -d. -f2)
PATCH_VERSION=$(echo $CURRENT_VERSION | cut -d. -f3)
echo "$MAJOR_VERSION"
echo "$MINOR_VERSION"
echo "$PATCH_VERSION"
NEW_PATCH_VERSION=$((PATCH_VERSION+1))
NEW_VERSION="$MAJOR_VERSION.$MINOR_VERSION.$NEW_PATCH_VERSION"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "GitHub Actions"
git tag "$NEW_VERSION"
git push origin "$NEW_VERSION"
- name: Extract PR Body
id: extract-pr-body
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_ID=${{ github.event.pull_request.number }}
PR_BODY=$(gh pr view $PR_ID --json body -q .body)
PR_BODY_ESCAPED=$(echo "$PR_BODY" | sed ':a;N;$!ba;s/\n/\ /g') # Convert to single string with newline escapes
echo "PR_BODY_ESCAPED=$PR_BODY_ESCAPED" >> $GITHUB_ENV
- name: Install Dependencies
run: npm install
- name: Build Android Release
run: |
mkdir -p android/app/src/main/assets/
npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
cd android
rm -r app/src/main/res/drawable-*
./gradlew assembleRelease
- name: Rename APK
run: |
mv android/app/build/outputs/apk/release/app-release.apk Synapse-${{ env.NEW_VERSION }}.apk
- name: Create GitHub Release and Upload APK
run: |
gh auth login --with-token <<< ${{ secrets.GITHUB_TOKEN }}
gh release create ${{ env.NEW_VERSION }} \
Synapse-${{ env.NEW_VERSION }}.apk \
--title "${{ env.NEW_VERSION }}" \
--notes "Release notes for ${{ env.NEW_VERSION }}: <br /> <br /> ${{ env.PR_BODY_ESCAPED }}"