Merge production deployment #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
# Make sure `Settings > Actions > General > Workflow permissions > Read and write permissions` and `Allow GitHub Actions to create and approve pull requests` are enabled | |
name: Merge production deployment | |
on: | |
schedule: | |
- cron: "30 0 * * *" | |
workflow_dispatch: | |
jobs: | |
merge_pull_request: | |
runs-on: ubuntu-22.04 | |
# Related | |
# - https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs | |
# - https://docs.github.com/en/rest/authentication/permissions-required-for-github-apps | |
permissions: | |
contents: write # Required to merge a pull request | |
steps: | |
- uses: actions/github-script@v7.0.1 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
# Related: | |
# - https://octokit.github.io/rest.js | |
# - https://docs.github.com/en/rest/pulls/pulls | |
script: | | |
console.info("Fetching existing pull requests") | |
const existingPullRequests = await github.rest.pulls.list({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
head: "develop", | |
base: "main", | |
state: "open", | |
}) | |
if (existingPullRequests.data.length <= 0) { | |
console.info(`No pull request found`) | |
return | |
} | |
const pullRequest = await github.rest.pulls.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: existingPullRequests.data[0].number, | |
}) | |
// https://github.com/orgs/community/discussions/24504 | |
// https://github.com/octokit/octokit.net/issues/1763 | |
if (pullRequest.data.mergeable_state !== "clean") { | |
throw new Error(`Pull request is not mergeable: ${pullRequest.data.mergeable_state}`) | |
} | |
console.info(`Merging pull request: ${pullRequest.data.html_url}`) | |
try { | |
const mergedPullRequest = await github.rest.pulls.merge({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: pullRequest.data.number, | |
merge_method: "merge", | |
}) | |
console.info(`Pull request merged`) | |
} catch (error) { | |
throw error | |
} |