Merge production deployment #158
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: "0 8 * * *" # 8am UTC | |
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 | |
} |