Skip to content

Instantly share code, notes, and snippets.

@makotoshimazu
Created September 21, 2022 11:30
Show Gist options
  • Save makotoshimazu/7d0e2ab1301760521b1cd9bdded4be7b to your computer and use it in GitHub Desktop.
Save makotoshimazu/7d0e2ab1301760521b1cd9bdded4be7b to your computer and use it in GitHub Desktop.
on:
workflow_call:
inputs:
deploy_target:
description: 'Deploy target'
required: true
type: string
default: 'production'
secrets:
GH_TOKEN:
required: true
jobs:
create-and-merge-deploy-pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: "trigger deploy trial when diff exists and status check passed"
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
DEPLOY_TARGET: ${{ inputs.deploy_target }}
run: |
# Check out branches so that you don't have to speculate the remote name.
git checkout main
git checkout ${DEPLOY_TARGET}
does_diff_exist=$( \
git diff --quiet --exit-code \
${DEPLOY_TARGET} main ; \
echo $? )
if [[ "${does_diff_exist}" == "0" ]]; then
echo "'main' and '${DEPLOY_TARGET}' are identical. Quit the job."
exit 0
elif [[ "${does_diff_exist}" != "1" ]]; then
echo 'git diff failed unexpectedly.'
exit 1
fi
# Add some logic to check if the latest CI status is success.
# Create a PR
pr_url=$( \
gh pr create \
--base ${DEPLOY_TARGET} \
--head main --label "auto deploy" \
--title "Deploy to ${DEPLOY_TARGET}" \
--body "Auto deploy" \
--no-maintainer-edit )
echo "Created a PR: ${pr_url}"
if [[ -z "${pr_url}" ]]; then
echo "Failed to create PR unexpectedly."
exit 1
fi
# And finally, merge it!
# NOTE: use `--admin` to bypass the review requirement.
gh pr merge ${pr_url} --admin --merge
name: Manual Deploy
on:
# Manual trigger from the "Actions" tab on the GitHub UI.
workflow_dispatch:
inputs:
deploy_target:
description: 'Deploy target'
required: true
default: 'production'
type: choice
options:
- production
- any-other-deploy-target-branches
jobs:
deploy:
uses: ./.github/workflows/deploy-pr.yml
with:
deploy_target: ${{ github.event.inputs.deploy_target }}
secrets:
GH_TOKEN: ${{ secrets.GH_TOKEN_YOUR_BOT }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment