Merge pull request #2 from finout-io/Workflow #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Create PR to finout-onboarding-internal Repository on Merge to main | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
create-pr: | |
runs-on: ubuntu-latest | |
env: | |
ORG: ${{ github.repository_owner }} | |
TARGET_REPO: 'finout-onboarding-internal' | |
NEW_BRANCH: "merge-branch-${{ github.run_number }}" | |
DEFAULT_BRANCH: 'main' | |
GIT_USER_NAME: 'GitHub Action' | |
GIT_USER_EMAIL: '[email protected]' | |
steps: | |
- name: Check if GH_TOKEN is set | |
run: | | |
if [ -z "${{ secrets.GH_TOKEN }}" ]; then | |
echo "GH_TOKEN is not set. Exiting workflow." | |
exit 1 | |
fi | |
timeout-minutes: 1 | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
timeout-minutes: 2 | |
- name: Log in with GitHub CLI | |
run: echo "${{ secrets.GH_TOKEN }}" | gh auth login --with-token | |
timeout-minutes: 2 | |
- name: Get last merged PR | |
id: last-merged-pr | |
run: | | |
PR_NUMBER=$(gh pr list --state merged --base main --limit 1 --json number --jq '.[0].number') | |
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV | |
MERGER_LOGIN=$(gh pr view $PR_NUMBER --json mergedBy --jq '.mergedBy.login') | |
echo "MERGER_LOGIN=$MERGER_LOGIN" >> $GITHUB_ENV | |
MERGER_EMAIL=$(gh api -X GET /users/$MERGER_LOGIN --jq '.email') | |
echo "MERGER_EMAIL=$MERGER_EMAIL" >> $GITHUB_ENV | |
LAST_COMMIT_MESSAGE=$(gh api repos/${{ env.ORG }}/${{ github.event.repository.name }}/commits/main --jq '.commit.message' -H "Authorization: token ${{ secrets.GH_TOKEN }}") | |
echo "LAST_COMMIT_MESSAGE=$(echo $LAST_COMMIT_MESSAGE | sed 's/"/\\"/g')" >> $GITHUB_ENV | |
timeout-minutes: 2 | |
- name: Sync files to a temporary directory | |
run: | | |
# Create a temporary directory | |
mkdir temp_dir | |
# Sync the source repository contents to the temporary directory | |
rsync -av --exclude=temp_dir --exclude=.git --exclude=.github/workflows/ --exclude=.circleci/ . temp_dir/ | |
- name: Create Pull Request in finout-onboarding-internal Repository | |
run: | | |
if [ -z "$MERGER_EMAIL" ]; then | |
echo "Merger's email not found. Using default." | |
git config --global user.name "${{ env.GIT_USER_NAME }}" | |
git config --global user.email "${{ env.GIT_USER_EMAIL }}" | |
else | |
git config --global user.name "$MERGER_LOGIN" | |
git config --global user.email "$MERGER_EMAIL" | |
fi | |
# Get the title and body of the merged PR | |
PR_TITLE="$(gh pr view $PR_NUMBER --json title --jq '.title')" | |
PR_BODY="$(gh pr view $PR_NUMBER --json body --jq '.body')" | |
# Create a new branch in the target repository | |
git clone "https://${{ secrets.GH_TOKEN }}@github.com/${{ env.ORG }}/${{ env.TARGET_REPO }}" target_repo | |
cd target_repo | |
git checkout -b "${{ env.NEW_BRANCH }}" | |
# Delete all files to prepare for copying the new state | |
git rm -rf . | |
# Sync the contents from the temporary directory | |
rsync -av --exclude=.git --exclude=.github/workflows/ --exclude=.circleci/ ../temp_dir/ . | |
# Add all files to the staging area and commit them | |
git add . | |
git commit -m "$LAST_COMMIT_MESSAGE" | |
# Push the new branch to the target repository | |
git push --set-upstream origin "${{ env.NEW_BRANCH }}" | |
# Create the new PR in the target repository | |
gh pr create --repo "${{ env.ORG }}/${{ env.TARGET_REPO }}" --title "${PR_TITLE}" --body "${PR_BODY}" --head "${{ env.ORG }}:${{ env.NEW_BRANCH }}" --base "${{ env.DEFAULT_BRANCH }}" | |
timeout-minutes: 5 | |
env: | |
GH_TOKEN: ${{ secrets.GH_TOKEN }} |