Skip to content

Commit

Permalink
Merge pull request #1137 from PhenoApps/git_workflow_improvements
Browse files Browse the repository at this point in the history
refine workflow structure and fix release commit sequence
  • Loading branch information
trife authored Jan 17, 2025
2 parents 7f331d6 + a78de4e commit 0a46575
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 131 deletions.
33 changes: 22 additions & 11 deletions .github/workflows/github-release.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
name: do-github-release

# This workflow automates the app release process and supports multiple triggers:
# - Manual dispatch: Releases unconditionally with a specified version bump.
# - Dispatch from `process-pr-merge`: Releases unconditionally using the version bump from `process-pr-merge`.
# - Weekly schedule: Runs every Monday at 3:00 PM EST and performs a patch release if the app directory has changed since the last release.

# Key steps:
# - Optionally checks for changes in `/app` and determines the release type (major, minor, or patch).
# - Extracts unreleased notes from `CHANGELOG.md`, updates it, and syncs `changelog.xml`.
# - Bumps the app version, builds, and signs the APK.
# - Commits changes and creates a GitHub release with the APK and changelog details.

on:
schedule:
# Run every Monday at 20:00 (8:00 PM)
# Run every Monday at 3:00 PM EST
- cron: "0 20 * * 1"
workflow_dispatch:
inputs:
Expand Down Expand Up @@ -181,16 +192,6 @@ jobs:
name: Signed APK
path: app/build/outputs/

- name: Make GitHub Release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
release_name: "v${{ env.VERSION }}"
tag: "${{ env.VERSION }}"
file: app/build/outputs/apk/release/app-release-unsigned-signed.apk
asset_name: "Field-Book-v${{ env.VERSION }}.apk"
body: ${{ steps.process-changelog.outputs.changelog_additions }}

- name: Commit Version and Changelog Changes
if: ${{ success() }}
uses: EndBug/add-and-commit@v7
Expand All @@ -204,6 +205,16 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}

- name: Make GitHub Release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
release_name: "v${{ env.VERSION }}"
tag: "${{ env.VERSION }}"
file: app/build/outputs/apk/release/app-release-unsigned-signed.apk
asset_name: "Field-Book-v${{ env.VERSION }}.apk"
body: ${{ steps.process-changelog.outputs.changelog_additions }}

#- name: Check date for Google Play upload
# id: date_check
Expand Down
153 changes: 153 additions & 0 deletions .github/workflows/process-pr-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
name: process-pr-merge

# This workflow processes commits pushed to the main branch to:
# 1. Determine if the commit is associated with a PR, and if not exit gracefully.
# 2. Extract relevant details (change type, release note, and bump type) from the PR body
# and use them to update the changelog if applicable (change type is not "OTHER").
# 3. Trigger a release action if warranted by the bump type (MAJOR, MINOR), or skip if WAIT is selected.

on:
push:
branches:
- main

jobs:
determine-pr:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.ACTIONS_PAT }}
outputs:
pr_number: ${{ steps.check-pr.outputs.pr_number }}
pr_body: ${{ steps.check-pr.outputs.pr_body }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.ACTIONS_PAT }}
fetch-depth: 0

- name: Check if this is a PR commit
id: check-pr
run: |
commit_sha=$(git log -1 --format="%H")
echo "Commit SHA: $commit_sha"
pr_number=$(gh api -X GET "repos/${{ github.repository }}/commits/$commit_sha/pulls" --jq '.[0].number')
if [ -z "$pr_number" ]; then
echo "No PR associated with commit $commit_sha."
echo "pr_number=" >> $GITHUB_OUTPUT
exit 0
fi
pr_body=$(gh api -X GET "repos/${{ github.repository }}/pulls/$pr_number" --jq '.body' | sed 's/\r//g')
echo "PR Number: $pr_number"
echo "PR Body: $pr_body"
echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT
echo "pr_body=${pr_body}" >> $GITHUB_OUTPUT
update-changelog:
runs-on: ubuntu-latest
needs: determine-pr
outputs:
bump_type: ${{ steps.extract-bump.outputs.bump_type }}
if: needs.determine-pr.outputs.pr_number != ''
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.ACTIONS_PAT }}
fetch-depth: 0

- name: Extract change type and release note
id: extract-details
run: |
pr_body="${{ needs.determine-pr.outputs.pr_body }}"
echo "Extracting details from PR body..."
release_note=$(echo "$pr_body" | awk 'BEGIN { found=0 } /```release-note/ { found=1; next } /```/ { found=0 } found { print }' | sed '/^$/d')
echo "Release Note: $release_note"
change_type=$(echo "$pr_body" | grep -oP '(?<=- \[x\] `)[A-Z]+(?=`)' | head -n 1)
echo "Change Type: $change_type"
if [ -z "$release_note" ]; then
echo "No release note found."
exit 1
fi
case "$change_type" in
"ADDITION")
section="### Added"
;;
"CHANGE")
section="### Changed"
;;
"FIX")
section="### Fixed"
;;
"OTHER")
echo "Change type is 'OTHER'. Skipping changelog update."
exit 0
;;
*)
echo "Invalid change type: $change_type."
exit 1
;;
esac
echo "release_note=${release_note}" >> $GITHUB_OUTPUT
echo "section=${section}" >> $GITHUB_OUTPUT
- name: Update changelog
run: |
echo "Updating changelog under section: ${{ steps.extract-details.outputs.section }}"
sed -i "0,/${{ steps.extract-details.outputs.section }}/s@${{ steps.extract-details.outputs.section }}@${{ steps.extract-details.outputs.section }}\n- ${{ steps.extract-details.outputs.release_note }}@" CHANGELOG.md
cat CHANGELOG.md
- name: Commit and push changelog update
uses: EndBug/add-and-commit@v7
with:
add: 'CHANGELOG.md'
message: "Update CHANGELOG.md with release note from PR #${{ needs.determine-pr.outputs.pr_number }}"
author_email: [email protected]
author_name: Git Action Bot
token: ${{ secrets.GITHUB_TOKEN }}
push: true

- name: Extract bump type
id: extract-bump
run: |
pr_body="${{ needs.determine-pr.outputs.pr_body }}"
echo "Extracting bump type from PR body..."
major_checked=$(echo "$pr_body" | grep -q '\[x\] \*\*`MAJOR`' && echo "true" || echo "false")
minor_checked=$(echo "$pr_body" | grep -q '\[x\] \*\*`MINOR`' && echo "true" || echo "false")
wait_checked=$(echo "$pr_body" | grep -q '\[x\] \*\*`WAIT`' && echo "true" || echo "false")
if [ "$major_checked" == "true" ]; then
echo "Detected bump type: MAJOR"
echo "bump_type=major" >> $GITHUB_OUTPUT
elif [ "$minor_checked" == "true" ]; then
echo "Detected bump type: MINOR"
echo "bump_type=minor" >> $GITHUB_OUTPUT
elif [ "$wait_checked" == "true" ]; then
echo "WAIT was checked. No release required."
echo "bump_type=" >> $GITHUB_OUTPUT
else
echo "No valid bump type specified. Skipping release."
echo "bump_type=" >> $GITHUB_OUTPUT
fi
dispatch-release:
runs-on: ubuntu-latest
needs: update-changelog
if: needs.update-changelog.outputs.bump_type != ''
steps:
- name: Dispatch GitHub Release Action
run: |
echo "Dispatching release action with bump type: ${{ needs.update-changelog.outputs.bump_type }}"
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.everest-preview+json" \
https://api.github.com/repos/${{ github.repository }}/dispatches \
-d '{"event_type": "trigger-release", "client_payload": {"bump_type": "${{ needs.update-changelog.outputs.bump_type }}"}}'
120 changes: 0 additions & 120 deletions .github/workflows/update-changelog.yml

This file was deleted.

0 comments on commit 0a46575

Please sign in to comment.