-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
このコミットで追加したワークフローは以下の動作をします。 PRがopen(またはreopen)された場合: https://github.com/GoConPreview/にリポジトリを作成して、 GitHub PagesにHugoの生成物をデプロイする。 PRにコミットがpushされた場合: open時に作成したリポジトリにHugoの生成物をデプロイする。 PRがmerge(またはclose)された場合: open時に作成したリポジトリを削除する。 https://medium.com/geekculture/when-youre-working-on-a-static-site-and-github-pages-feels-like-the-perfect-hosting-solution-a41c37f4e326 の実装を参考にしました。
- Loading branch information
Showing
5 changed files
with
141 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Preview Repository Name | ||
description: Get preview repository name | ||
runs: | ||
using: composite | ||
steps: | ||
- name: Set repository owner name | ||
id: owner | ||
run: echo "::set-output name=result::GoConPreview" | ||
shell: bash | ||
|
||
- name: Set repository name | ||
uses: actions/github-script@v5 | ||
id: repo | ||
with: | ||
script: | | ||
function sanitize(str) { | ||
return str.toString().replace(/[^\w-]/g, ""); | ||
} | ||
const repoName = context.repo.repo; | ||
const prNumber = context.payload.number; | ||
const branchName = context.payload.pull_request.head.ref; | ||
return `${sanitize(repoName)}-pr-${sanitize(prNumber)}-${sanitize(branchName)}`; | ||
result-encoding: string | ||
outputs: | ||
owner: | ||
description: Repository owner name | ||
value: ${{ steps.owner.outputs.result }} | ||
name: | ||
description: Repository name | ||
value: ${{ steps.repo.outputs.result }} | ||
full_name: | ||
description: Repository full name ($owner/$name) | ||
value: ${{ steps.owner.outputs.result }}/${{ steps.repo.outputs.result }} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name: Install Hugo | ||
description: Install Hugo | ||
runs: | ||
using: composite | ||
steps: | ||
- uses: peaceiris/actions-hugo@v2 | ||
with: | ||
extended: true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Delete preview repository | ||
|
||
on: | ||
pull_request: | ||
types: [closed] | ||
|
||
jobs: | ||
delete-preview: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Get preview repo name | ||
uses: ./.github/actions/get_preview_repo_name | ||
id: preview_repo | ||
|
||
- name: Get GitHub token | ||
id: get_token | ||
uses: getsentry/action-github-app-token@v1 | ||
with: | ||
app_id: ${{ secrets.PREVIEW_APP_ID }} | ||
private_key: ${{ secrets.PREVIEW_APP_PRIVATE_KEY }} | ||
|
||
- name: Delete preview repo | ||
uses: octokit/request-action@v2.x | ||
with: | ||
route: DELETE /repos/{repo_name} | ||
repo_name: ${{ steps.preview_repo.outputs.full_name }} | ||
env: | ||
GITHUB_TOKEN: ${{ steps.get_token.outputs.token }} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
name: Deploy PR to preview repository | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
|
||
jobs: | ||
deploy-preview: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
submodules: true | ||
|
||
- name: Get preview repo name | ||
uses: ./.github/actions/get_preview_repo_name | ||
id: preview_repo | ||
|
||
- name: Install Hugo | ||
uses: ./.github/actions/install_hugo | ||
|
||
- name: Build pages | ||
run: hugo -v --baseURL="$baseURL" | ||
env: | ||
baseURL: https://${{ steps.preview_repo.outputs.owner }}.github.io/${{ steps.preview_repo.outputs.name }} | ||
|
||
- name: Get GitHub token | ||
id: get_token | ||
uses: getsentry/action-github-app-token@v1 | ||
with: | ||
app_id: ${{ secrets.PREVIEW_APP_ID }} | ||
private_key: ${{ secrets.PREVIEW_APP_PRIVATE_KEY }} | ||
|
||
- name: Check if new repository is needed | ||
id: new_repo | ||
# 新しいリポジトリを作る必要があれば'true'、なければ'false'をresultに出力する | ||
run: echo "::set-output name=result::$new_repo" | ||
env: | ||
new_repo: ${{ github.event.action == 'opened' || github.event.action == 'reopened' }} | ||
|
||
- name: Create preview repo | ||
if: steps.new_repo.outputs.result == 'true' | ||
uses: octokit/request-action@v2.x | ||
with: | ||
route: POST /orgs/{org}/repos | ||
org: ${{ steps.preview_repo.outputs.owner }} | ||
name: ${{ steps.preview_repo.outputs.name }} | ||
env: | ||
GITHUB_TOKEN: ${{ steps.get_token.outputs.token }} | ||
|
||
- name: Deploy | ||
uses: peaceiris/actions-gh-pages@v3 | ||
with: | ||
personal_token: ${{ steps.get_token.outputs.token }} | ||
external_repository: ${{ steps.preview_repo.outputs.full_name }} | ||
publish_dir: public | ||
|
||
- name: Enable GitHub Pages | ||
if: steps.new_repo.outputs.result == 'true' | ||
uses: octokit/request-action@v2.x | ||
with: | ||
route: POST /repos/{repo_name}/pages | ||
repo_name: ${{ steps.preview_repo.outputs.full_name }} | ||
data: '{"source":{"branch":"gh-pages"}}' | ||
env: | ||
GITHUB_TOKEN: ${{ steps.get_token.outputs.token }} |
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