Skip to content

create-merge-release-into-dev-pr #14

create-merge-release-into-dev-pr

create-merge-release-into-dev-pr #14

name: create-merge-release-into-dev-pr
on:
workflow_dispatch:
env:
BASE_BRANCH: test-dev
permissions: {}
jobs:
setup:
runs-on: ubuntu-latest
outputs:
latest_release_branch: ${{ steps.find_latest_release.outputs.branch }}
steps:
- id: find_latest_release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
BRANCH=$(curl -H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/$GITHUB_REPOSITORY/branches?protected=true | \
jq -r '.[].name' | grep '^release/' | sort --version-sort | tail -1
)
if [ "$BRANCH" = "" ]; then
echo "Invalid release branch found: $BRANCH"
exit 1
fi
echo "branch=${BRANCH}" >> $GITHUB_OUTPUT
merge-or-create-pr:
permissions:
contents: write # for git push
pull-requests: write # for creating pull request
runs-on: ubuntu-latest
needs: setup
timeout-minutes: 5
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ env.BASE_BRANCH }} # TODO: DELETE ME
- name: Try merging
run: |
# Fetching the dev and release branches without fetching their complete history
# First fetch branches themselves excluding commits reachable by the other branch
git fetch --shallow-exclude "$BASE_BRANCH" origin "$RELEASE_BRANCH"
git fetch --shallow-exclude "$RELEASE_BRANCH" origin "$BASE_BRANCH"
# Resolve bug in git https://stackoverflow.com/a/63879454/96823
git repack -d
# Get one commit more to connect the history of dev and release branches
git fetch --deepen 1 origin "$BASE_BRANCH" "$RELEASE_BRANCH"
# Create branch without checkout just to have shorter automatic commit message
git branch "$RELEASE_BRANCH" origin/"$RELEASE_BRANCH"
# Required config
git config user.name "OpenProject Actions CI"
git config user.email "[email protected]"
if git diff --exit-code --quiet ..."$RELEASE_BRANCH"; then
echo "Nothing to merge"
else
if git merge --no-edit --no-ff "$RELEASE_BRANCH"; then
git push "$BASE_BRANCH"
else
gh pr create \
--base "$BASE_BRANCH" \
--head "$RELEASE_BRANCH" \
--title "Merge $RELEASE_BRANCH into $BASE_BRANCH" \
--body 'Created by GitHub action'
end
end
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_BRANCH: ${{ needs.setup.outputs.latest_release_branch }}