Skip to content

Instantly share code, notes, and snippets.

@syossan27
Last active December 11, 2021 16:25
Show Gist options
  • Save syossan27/2326f51865b9c7f5e3cea3a1263d174b to your computer and use it in GitHub Desktop.
Save syossan27/2326f51865b9c7f5e3cea3a1263d174b to your computer and use it in GitHub Desktop.
# PRがマージ/クローズされたらデプロイした開発環境の使用状況を空に変更
name: Change ENV by Pull Request close
on:
pull_request_target:
# closedしか指定していませんが、マージ時も対象となります
types: [closed]
jobs:
change_env:
runs-on: ubuntu-latest
steps:
- name: PRのデプロイコメントの取得
uses: actions/github-script@v4
id: comment
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let page_num = 1
let latest_deploy_comment = ""
# デプロイコメントを見つけるためにループ
while (true) {
# ループ毎に1ページづつコメントリストを取得
const result = await github.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
per_page: 100,
page: page_num
})
page_num++ # 次のページへ
# 取得するコメントがなければループを抜ける
if (result.data.length === 0) {
break
}
# コメントリストの中からデプロイコメントがあるか探索
for (let i = 0; i < result.data.length; i++) {
const comment = result.data[i].body
if (comment.indexOf('deploy dev_') != -1) {
latest_deploy_comment = comment
}
}
}
return latest_deploy_comment
- name: Google Cloud SDKのセットアップ
uses: google-github-actions/[email protected]
with:
project_id: ${{ secrets.PROJECT_ID }}
service_account_key: ${{ secrets.GCP_SA_KEY }}
export_default_credentials: true
- name: 使用状況の変更
run: |
# 開発環境の番号(1~3)を取得
comment=${{ steps.comment.outputs.result }}
env_number=${comment#deploy dev_}
# 取得できなければ終了
if [ -z "$env_number" ]; then
exit
fi
# 取得した番号をもとに切り替え処理
touch ${env_number}.txt
echo ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} > ${env_number}.txt # 未使用状態ではリポジトリに遷移するようにする
gsutil -h "Cache-Control: no-store" -m cp -r ./${env_number}.txt gs://example-bucket/app-1/
gsutil -h "Cache-Control: no-store" -m cp -r gs://example-bucket/red.png gs://example-bucket/app-1/${env_number}.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment