Skip to content

Commit

Permalink
Merge pull request #1065 from PhenoApps/git_action_updates
Browse files Browse the repository at this point in the history
GitHub Workflow Updates
  • Loading branch information
trife authored Oct 30, 2024
2 parents b7bc2a4 + b6d8028 commit 00f85a2
Show file tree
Hide file tree
Showing 6 changed files with 712 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/artifacts-cleanup.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
on: delete

name: do-artifacts-cleanup

on: delete

jobs:
delete-archived-apk:
if: github.event.ref_type == 'branch'
Expand Down
145 changes: 108 additions & 37 deletions .github/workflows/github-release.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
name: do-github-release

on:
schedule:
- cron: "0 20 * * *"
# Run every Monday at 20:00 (8:00 PM)
- cron: "0 20 * * 1"
workflow_dispatch:

name: do-github-release

jobs:

check-app-changes:
runs-on: ubuntu-latest
outputs:
app_changed: ${{ steps.check_app_changes.outputs.app_changed }}
changelog_additions: ${{ steps.check_changelog.outputs.changelog_additions }}
version_changed: ${{ steps.check_version.outputs.version_changed }}
steps:

- name: Checkout repo
uses: actions/checkout@v2
with:
with:
token: ${{secrets.ACTIONS_PAT}}
fetch-depth: 0

- name: Check if app directory changed
id: check_app_changes
- name: Determine last release tag and commit
id: set_commit_vars
run: |
LAST_RELEASE_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
echo "LAST_RELEASE_TAG was $LAST_RELEASE_TAG"
LAST_RELEASE_COMMIT=$(git rev-list -n 1 $LAST_RELEASE_TAG)
echo "LAST_RELEASE_COMMIT was $LAST_RELEASE_COMMIT"
echo "LAST_RELEASE_TAG=$LAST_RELEASE_TAG" >> $GITHUB_ENV
echo "LAST_RELEASE_COMMIT=$LAST_RELEASE_COMMIT" >> $GITHUB_ENV
- name: Check if app directory changed
id: check_app_changes
run: |
changed_files=$(git diff-tree --no-commit-id --name-only $LAST_RELEASE_COMMIT $GITHUB_SHA | grep '^app' || echo "none")
echo "Changed app files: $changed_files"
Expand All @@ -37,10 +48,65 @@ jobs:
echo "app_changed=false" >> "$GITHUB_OUTPUT"
fi
- name: Check CHANGELOG.md changes
id: check_changelog
run: |
# Extract the contents of the unreleased section
unreleased_section=$(sed -n '/## \[Unreleased\]/,/^## /p' CHANGELOG.md | sed '1,2d' | sed '$d')
if [ -n "$unreleased_section" ]; then
changelog_additions=$(echo "$unreleased_section" | sed 's/^- /✔ /g')
# Use multiline escape sequences
changelog_escaped="${changelog_additions//'%'/'%25'}"
changelog_escaped="${changelog_escaped//$'\n'/'%0A'}"
changelog_escaped="${changelog_escaped//$'\r'/'%0D'}"
echo "changelog_additions=$changelog_escaped" >> $GITHUB_OUTPUT
echo "changelog_additions: $changelog_escaped" # Also log release notes
else
echo "No changes found in the Unreleased section."
echo "changelog_additions=none" >> $GITHUB_OUTPUT
exit 1
fi
- name: Check if version.properties changed
id: check_version
run: |
version_changed=$(git diff --name-only $LAST_RELEASE_COMMIT $GITHUB_SHA | grep 'version.properties' || echo "none")
echo "version_changed: $version_changed"
if [ "$version_changed" != "none" ]; then
echo "version_changed=true" >> "$GITHUB_OUTPUT"
if [ -f version.properties ]; then
source version.properties
else
echo "version.properties file not found. Exiting."
exit 1
fi
OLD_VERSION=$(git show $LAST_RELEASE_COMMIT:version.properties | grep 'version' | awk -F= '{print $2}')
echo "OLD_VERSION: $OLD_VERSION"
NEW_VERSION=$majorVersion.$minorVersion.$patchVersion
echo "NEW_VERSION: $NEW_VERSION"
# Ensure NEW_VERSION and OLD_VERSION are valid before comparing
if [[ -z "$NEW_VERSION" || -z "$OLD_VERSION" ]]; then
echo "Either NEW_VERSION or OLD_VERSION is empty. Exiting."
exit 1
fi
if [[ "$NEW_VERSION" == "$OLD_VERSION" || "$NEW_VERSION" < "$OLD_VERSION" ]]; then
echo "New version is not higher than the old version. Exiting."
exit 1
else
# New version is higher than the old version
echo "VERSION=$NEW_VERSION" >> $GITHUB_ENV
fi
else
echo "version_changed=false" >> "$GITHUB_OUTPUT"
fi
build-and-release:
runs-on: ubuntu-latest
needs: check-app-changes
if: ${{ needs.check-app-changes.outputs.app_changed == 'true' }}
if: ${{ needs['check-app-changes'].outputs.app_changed == 'true' }}
steps:

- name: Checkout repo
Expand All @@ -63,7 +129,8 @@ jobs:
# Uses semantic commits to automate version bumping.
# No scope or "fix:" = PATCH, "feat:" or "minor:" = MINOR, "BREAKING CHANGE:", "major:", or fix/feat with appended "!" = MAJOR
# Additional details: https://www.conventionalcommits.org/en/v1.0.0/
- name: Increment version
- name: Increment version and update changelog
if: needs.check-app-changes.outputs.version_changed == 'false'
run: |
#!/bin/bash
COMMIT_MSG=$(git log -1 --pretty=format:"%b" || git log -1 --pretty=format:"%B")
Expand All @@ -88,21 +155,32 @@ jobs:
echo "patchVersion=$patchVersion" >> version.properties
VERSION=$majorVersion.$minorVersion.$patchVersion
RELEASE=$majorVersion.$minorVersion
echo "COMMIT_MSG=$COMMIT_MSG" >> $GITHUB_ENV
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "RELEASE=$RELEASE" >> $GITHUB_ENV
echo "BUMP_TYPE=$BUMP_TYPE" >> $GITHUB_ENV
# Update the Unreleased section in CHANGELOG.md
today=$(date +'%Y-%m-%d')
sed -i "s/## \[Unreleased\]/## \[v$VERSION\] - $today/" CHANGELOG.md
# Insert a new Unreleased section
sed -i '/## \[v'$VERSION'\]/i ## [Unreleased]\n\n### Added\n\n### Changed\n\n### Fixed\n' CHANGELOG.md
# Add the link to the release at the end of CHANGELOG.md
printf "\n[v$VERSION]: https://github.com/PhenoApps/Field-Book/releases/tag/v$VERSION\n" >> CHANGELOG.md
- name: Commit version changes
if: needs.check-app-changes.outputs.version_changed == 'false'
uses: EndBug/add-and-commit@v7
with:
add: 'version.properties'
message: Bump ${{ env.BUMP_TYPE }}
add: |
version.properties
CHANGELOG.md
message: Bump ${{ env.BUMP_TYPE }} and update changelog for v${{ env.VERSION }}
author_email: git-action-bot@example.com
author_name: Git Action Bot

- name: Push changes
if: needs.check-app-changes.outputs.version_changed == 'false'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -129,39 +207,32 @@ jobs:
name: Signed APK
path: app/build/outputs/

- name: Get matching release
uses: cardinalby/git-get-release-action@1.2.4
id: matching_release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
releaseName: v${{ env.RELEASE }}
doNotFailIfNotFound: 'true'

- name: Delete matching release if exists
if: steps.matching_release.outputs.id != ''
run: |
curl -X DELETE -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/releases/${{ steps.matching_release.outputs.id}}"
# Updates the latest release if just a new patch, drafts a new prerelease if major or minor version has changed
- name: Make github release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
release_name: v${{ env.RELEASE }}
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.RELEASE }}.apk
overwrite: ${{ env.BUMP_TYPE == 'patchVersion' }}
prerelease: ${{ env.BUMP_TYPE != 'patchVersion' }}
body: |
${{ steps.matching_release.outputs.body }}
${{ env.COMMIT_MSG }}
asset_name: Field-Book-v${{ env.VERSION }}.apk
body: ${{ needs.check-app-changes.outputs.changelog_additions }}

- name: Check date for Google Play upload
id: date_check
run: |
CURRENT_DATE=$(date +%m%d)
echo "CURRENT_DATE: $CURRENT_DATE"
if [ "$CURRENT_DATE" -ge 0415 ] && [ "$CURRENT_DATE" -le 0915 ]; then
echo "UPLOAD_TO_PLAY_STORE=false" >> $GITHUB_ENV
else
echo "UPLOAD_TO_PLAY_STORE=true" >> $GITHUB_ENV
fi
- name: Release APK to Play Store
if: env.UPLOAD_TO_PLAY_STORE == 'true'
uses: r0adkll/upload-google-play@v1
with:
serviceAccountJsonPlainText: ${{ secrets.SERVICE_ACCOUNT_JSON }}
packageName: com.fieldbook.tracker
releaseFiles: app/build/outputs/apk/release/app-release-unsigned-signed.apk
track: alpha
track: alpha
6 changes: 3 additions & 3 deletions .github/workflows/pr-prerelease.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name: do-pr-prerelease

on:
workflow_dispatch:
#pull_request:
Expand All @@ -6,8 +8,6 @@ on:
# paths:
# - 'app/**'

name: do-pr-prerelease

jobs:
build-and-upload:
if: github.event.pull_request.draft == false
Expand Down Expand Up @@ -72,4 +72,4 @@ jobs:
repo-token: ${{ secrets.GITHUB_TOKEN }}
artifacts-branch: artifacts
artifacts: |
app/build/outputs/apk/release/${{ env.APK_NUMBER }}-${{ env.BRANCH }}-Field-Book.apk
app/build/outputs/apk/release/${{ env.APK_NUMBER }}-${{ env.BRANCH }}-Field-Book.apk
94 changes: 94 additions & 0 deletions .github/workflows/update-changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: update-changelog

on:
push:
branches:
- main

jobs:
update-changelog:
runs-on: ubuntu-latest

env:
GH_TOKEN: ${{ secrets.ACTIONS_PAT }}

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.ACTIONS_PAT }}
fetch-depth: 0

- name: Extract changelog entry from PR
id: extract_changelog
run: |
# Get the latest commit SHA
commit_sha=$(git log -1 --format="%H")
echo "Commit SHA: $commit_sha"
# Find the pull request associated with the commit
pr_number=$(gh api -X GET "repos/${{ github.repository }}/commits/$commit_sha/pulls" --jq '.[0].number')
# Exit gracefully if no PR is found
if [ -z "$pr_number" ]; then
echo "No associated PR found for commit $commit_sha."
exit 0
fi
# Get the PR body and extract release note
pr_body=$(gh api -X GET "repos/${{ github.repository }}/pulls/$pr_number" --jq '.body' | sed 's/\r//g')
changelog_entry=$(echo "$pr_body" | awk 'BEGIN { found=0 } /```release-note/ { found=1; next } /```/ { found=0 } found { print }' | sed '/^$/d')
# Exit if no changelog entry found
if [ -z "$changelog_entry" ]; then
echo "No changelog entry found."
exit 0
fi
# Detect change type (CHANGE, FEATURE, BUGFIX, ENHANCEMENT)
change_type=$(echo "$pr_body" | grep -oP '(?<=- \[x\] `)\w+')
case "$change_type" in
"FEATURE"|"ENHANCEMENT")
section="### Added"
;;
"CHANGE")
section="### Changed"
;;
"BUGFIX")
section="### Fixed"
;;
"NONE")
echo "No notable changelog entry needed."
exit 0
;;
*)
echo "Unknown change type: $change_type"
exit 1
;;
esac
# Store the extracted information in environment variables
pr_url="https://github.com/${{ github.repository }}/pull/$pr_number"
echo "changelog=${changelog_entry} ($pr_url)" >> $GITHUB_ENV
echo "section=$section" >> $GITHUB_ENV
echo "pr_number=$pr_number" >> $GITHUB_ENV
- name: Add entry to changelog
if: env.pr_number != ''
run: |
# Insert the changelog entry under the first matching section in Unreleased (avoids / delimeter to not conflict with url)
sed -i "0,/${{ env.section }}/s@${{ env.section }}@${{ env.section }}\n- ${{ env.changelog }}@" CHANGELOG.md
# Show the updated changelog
cat CHANGELOG.md
- name: Commit and push changelog update
if: env.pr_number != ''
uses: EndBug/add-and-commit@v7
with:
add: 'CHANGELOG.md'
message: "Update CHANGELOG.md with entry from PR #${{ env.pr_number }}"
author_email: git-action-bot@example.com
author_name: Git Action Bot
token: ${{ secrets.GITHUB_TOKEN }}
push: true
22 changes: 22 additions & 0 deletions .github/workflows/update-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: update-docs

on:
push:
branches:
- main
paths:
- 'docs/**'

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
publish_dir: ./docs
github_token: ${{ secrets.ACTIONS_PAT }}
Loading

0 comments on commit 00f85a2

Please sign in to comment.