Skip to content

Clear cache and deploy site on Netlify #69

Clear cache and deploy site on Netlify

Clear cache and deploy site on Netlify #69

name: 'Clear cache and deploy site on Netlify'
on:
workflow_dispatch:
schedule:
- cron: '0 9,17 * * *' # 毎日9時と17時に実行する
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: main branch
# mainブランチで実行されている場合は、NetlifyのキャッシュをクリアしてデプロイするAPIを叩く
if: github.ref_name == 'main'
run: |
echo "Request main branch build and deploy without cache"
curl -X POST https://api.netlify.com/api/v1/sites/${{ secrets.NETLIFY_SITE_ID }}/builds \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${{ secrets.NETLIFY_AUTH_TOKEN }}" \
-d '{"clear_cache": true}'
- name: not in main branch
# mainブランチ以外では、そのブランチでの最新デプロイのIDを取得してから、
# そのデプロイをキャッシュなしでリトライするAPIを叩く
if: github.ref_name != 'main'
run: |
response=$(curl https://api.netlify.com/api/v1/sites/${{ secrets.NETLIFY_SITE_ID }}/deploys?per_page=1\&branch=${{ github.ref_name }} \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${{ secrets.NETLIFY_AUTH_TOKEN }}")
deploy_id=$(echo $response | jq -r '.[0].id')
if [ "$deploy_id" == 'null' ]; then
echo "deploy_id for ${{ github.ref_name }} branch not found";
exit;
fi
echo "Request retrying deploy without cache. branch_name: ${{ github.ref_name }} deploy_id: $deploy_id"
curl -X POST https://api.netlify.com/api/v1/deploys/$deploy_id/retry \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${{ secrets.NETLIFY_AUTH_TOKEN }}" \
-d '{"clear_cache": true}'