Getting pull request messages that merged into master branch

86 views Asked by At

I have a GitHub Actions workflow in place that is triggered whenever a PR is closed and merged. This action automatically generates an APK and attaches it to a new GitHub release with a specific tag.

However, I also want to automatically copy the body/message of the PR into the GitHub release's message body. This is to maintain a record of what was introduced or changed in that release.

Current Workflow

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 }}"

The error that I got

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.
1

There are 1 answers

0
nogabemist On

Actually this works

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 }}"