-
Notifications
You must be signed in to change notification settings - Fork 0
Add workflow to update develop branch from main #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
62f4e46
feat(develop-update): add workflow to update develop branch from main
The0mikkel 886ccfd
Add permissions to develop-update workflow and README
The0mikkel ff7f698
feat(develop-update): enhance auto-merge logic and add warnings for n…
The0mikkel e7bb5de
Enhance clarity for develop update exit handling
The0mikkel f26e2c3
Merge branch 'main' into develop
The0mikkel 6f0a371
Correct pull request base and head references in workflow
The0mikkel 52fbbeb
Correct multiple logic errors in develop-update
The0mikkel bc652cc
Merge branch 'develop' of https://github.com/ctfpilot/ci into develop
The0mikkel 39d115a
Update formatting and documentation for develop-update
The0mikkel aeb986b
Simplify auto-merge for develop-update
The0mikkel 61081ff
Fix minor issues and fomatting in develop-update
The0mikkel e3373a9
Correct docs link to develop-update
The0mikkel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| # This workflow opens a PR between main and develop branches to keep develop up to date. | ||
| name: "Update Develop Branch" | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| workflow_call: | ||
| inputs: | ||
| repository: | ||
| description: "Allowed repository for workflow to run in. Example `ctfpilot/hello-world`." | ||
| required: true | ||
| type: string | ||
| auto_merge: | ||
| description: "Whether to automatically merge the PR after creating it." | ||
| required: false | ||
| type: boolean | ||
| default: true | ||
| pr_description: | ||
| description: "Additional description to add to the PR body." | ||
| required: false | ||
| type: string | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| issues: write | ||
|
|
||
| jobs: | ||
| update-develop: | ||
| name: "Update Develop Branch" | ||
| runs-on: ubuntu-latest | ||
| if: github.repository == ( inputs.repository || 'ctfpilot/ci') && github.ref == 'refs/heads/main' | ||
| steps: | ||
| - name: "Checkout" | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: main | ||
|
|
||
| # Ensure diff between main and develop | ||
| - name: "Check if there is a diff between main and develop" | ||
| id: check_diff | ||
| run: | | ||
| git fetch origin develop | ||
| DIFF=$(git diff origin/develop..main) | ||
| if [ -z "$DIFF" ]; then | ||
| echo "No differences found between main and develop." | ||
| echo "diff=false" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "Differences found between main and develop." | ||
| echo "diff=true" >> $GITHUB_OUTPUT | ||
| fi | ||
| - name: "Check if existing PR exists" | ||
| if: steps.check_diff.outputs.diff == 'true' | ||
| id: check_pr | ||
| uses: actions/github-script@v6 | ||
| with: | ||
| script: | | ||
| const { data: pullRequests } = await github.pulls.list({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| head: 'main', | ||
| base: 'develop', | ||
| state: 'open' | ||
| }); | ||
| if (pullRequests.length > 0) { | ||
| return 'true'; | ||
| } else { | ||
| return 'false'; | ||
| } | ||
| result-encoding: string | ||
|
|
||
| # Ensure labels exist | ||
| - name: 'Ensure "develop-update" label is created' | ||
| if: steps.check_pr.outputs.result == 'false' | ||
| uses: actions/github-script@v6 | ||
| with: | ||
| script: | | ||
| try { | ||
| await github.issues.getLabel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| name: 'develop-update' | ||
| }); | ||
| } catch (error) { | ||
| if (error.status === 404) { | ||
| await github.issues.createLabel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| name: 'develop-update', | ||
| color: '0E8A16', | ||
| description: 'Indicates that this PR updates the develop branch to match the latest version of main.' | ||
| }); | ||
| } else { | ||
| throw error; | ||
| } | ||
| } | ||
| - name: 'Ensure "ci" label is created' | ||
| if: steps.check_pr.outputs.result == 'false' | ||
| uses: actions/github-script@v6 | ||
| with: | ||
| script: | | ||
| try { | ||
| await github.issues.getLabel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| name: 'ci' | ||
| }); | ||
| } catch (error) { | ||
| if (error.status === 404) { | ||
| await github.issues.createLabel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| name: 'ci', | ||
| color: 'EDEDED', | ||
| description: 'Indicates that this PR is related to continuous integration.' | ||
| }); | ||
| } else { | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| # PR Creation | ||
| - name: "Create Pull Request to update develop branch, and merge it" | ||
| id: create_pr | ||
| if: steps.check_diff.outputs.diff == 'true' && steps.check_pr.outputs.result == 'false' | ||
| run: | | ||
| URL=$(gh pr create -B develop -H main --title 'CI: Update develop to match main' --body 'Merge main into develop to update the develop branch to the latest version\n\n${{ inputs.pr_description || '' }}' --label develop-update --label ci) | ||
| echo "URL=$URL" >> $GITHUB_OUTPUT | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| # Auto merge handling | ||
| - name: "Check if latest commit was a merge commit from develop" | ||
| if: steps.create_pr.outputs.URL != '' && inputs.auto_merge == true | ||
| id: check_merge_source | ||
| run: | | ||
| # Check if latest commit is a merge commit | ||
| if git rev-parse --verify HEAD^2 &>/dev/null; then | ||
| echo "Latest commit is a merge commit" | ||
| # Get PR number from merge commit message | ||
| PR_NUMBER=$(git log -1 --pretty=%B | grep -oP 'Merge pull request #\K[0-9]+' || echo "") | ||
| if [ -n "$PR_NUMBER" ]; then | ||
| echo "Found PR number: $PR_NUMBER" | ||
| # Use gh CLI to check PR head branch | ||
| HEAD_BRANCH=$(gh pr view "$PR_NUMBER" --json headRefName -q .headRefName || echo "") | ||
| echo "PR head branch: $HEAD_BRANCH" | ||
| if [ "$HEAD_BRANCH" = "develop" ]; then | ||
| echo "latest_from_develop=true" >> $GITHUB_OUTPUT | ||
| echo "✓ Latest commit merged from develop branch PR" | ||
| else | ||
| echo "latest_from_develop=false" >> $GITHUB_OUTPUT | ||
| echo "head_branch=$HEAD_BRANCH" >> $GITHUB_OUTPUT | ||
| echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT | ||
| echo "⚠ Latest commit merged from '$HEAD_BRANCH' branch (not develop)" | ||
| fi | ||
| else | ||
| echo "latest_from_develop=unknown" >> $GITHUB_OUTPUT | ||
| echo "Could not determine PR number from merge commit" | ||
| fi | ||
| else | ||
| echo "latest_from_develop=not_merge" >> $GITHUB_OUTPUT | ||
| echo "Latest commit is not a merge commit" | ||
| fi | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| - name: "Create warning for non-develop merge" | ||
| if: steps.check_merge_source.outputs.latest_from_develop == 'false' | ||
| run: | | ||
| echo "::warning::Latest merge commit on main was from branch '${{ steps.check_merge_source.outputs.head_branch }}' (PR #${{ steps.check_merge_source.outputs.pr_number }}), not from develop. Auto-merge will be skipped to allow manual review." | ||
| - name: "Comment on PR about skipped auto-merge" | ||
| if: steps.check_merge_source.outputs.latest_from_develop == 'false' | ||
| run: | | ||
| gh pr comment ${{ steps.create_pr.outputs.URL }} "⚠️ Auto-merge skipped: Latest merge commit on main was from branch '${{ steps.check_merge_source.outputs.head_branch }}' (PR #${{ steps.check_merge_source.outputs.pr_number }}), not from develop. Please review and merge manually." | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| - name: "Auto-merge Pull Request" | ||
| if: steps.check_merge_source.outputs.latest_from_develop == 'true' | ||
| run: | | ||
| gh pr merge "${{ steps.create_pr.outputs.URL }}" -t "chore(ci): Auto update develop to match main [skip ci]" -b "This was done automatically by the CI pipeline" --merge | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.