-
Notifications
You must be signed in to change notification settings - Fork 0
125 lines (113 loc) · 4.95 KB
/
Copy pathcd.yml
File metadata and controls
125 lines (113 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
name: CD
# Runs only after the CI workflow finishes, and reuses the artifacts it built
# (no rebuild here). Preview on pushes to main, production on version tags.
on:
workflow_run:
workflows: ["CI"]
types:
- completed
concurrency:
group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch }}
cancel-in-progress: true
permissions:
contents: read
actions: read # download artifacts from the triggering CI run
deployments: write
jobs:
prepare:
name: Prepare
runs-on: ubuntu-latest
# Only after a SUCCESSFUL CI, and never for pull requests (deploy is for
# main -> preview and tags -> production only).
if: >-
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event != 'pull_request'
outputs:
deploy_type: ${{ steps.meta.outputs.deploy_type }}
version: ${{ steps.meta.outputs.version }}
steps:
- name: Download deploy metadata
uses: actions/download-artifact@v8
with:
name: "deploy-info"
path: "meta"
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Read metadata
id: meta
run: cat meta/deploy-info.env >> "$GITHUB_OUTPUT"
deploy:
name: Deploy to Netlify
runs-on: ubuntu-latest
needs: prepare
if: needs.prepare.outputs.deploy_type != 'none'
environment:
name: ${{ needs.prepare.outputs.deploy_type == 'production' && 'production' || 'preview' }}
url: ${{ steps.netlify.outputs.deploy-url }}
steps:
- name: Download build artifact
uses: actions/download-artifact@v8
with:
name: "dist"
path: "./dist"
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Deploy to Netlify
id: netlify
uses: nwtgck/actions-netlify@v4.0.0
with:
publish-dir: "./dist"
production-deploy: ${{ needs.prepare.outputs.deploy_type == 'production' }}
deploy-message: "Deploy (${{ needs.prepare.outputs.deploy_type }}) from CD — v${{ needs.prepare.outputs.version }}"
enable-pull-request-comment: false
enable-commit-comment: false
env:
NETLIFY_AUTH_TOKEN: "${{ secrets.NETLIFY_AUTH_TOKEN }}"
NETLIFY_SITE_ID: "${{ secrets.NETLIFY_SITE_ID }}"
timeout-minutes: 5
cleanup:
name: Prune old Netlify deploys
runs-on: ubuntu-latest
needs: [prepare, deploy]
# Runs after every deploy — preview or production. Skipped when nothing was
# deployed (the deploy job didn't run / didn't succeed).
if: ${{ always() && needs.deploy.result == 'success' }}
steps:
- name: Keep recent deploys plus a minimum of stable/preview, delete the rest
env:
NETLIFY_AUTH_TOKEN: "${{ secrets.NETLIFY_AUTH_TOKEN }}"
NETLIFY_SITE_ID: "${{ secrets.NETLIFY_SITE_ID }}"
KEEP_STABLE: "3" # minimum stable (production) deploys to always retain
KEEP_PREVIEW: "2" # minimum preview (draft) deploys to always retain
MAX_AGE_DAYS: "7" # additionally, retain every deploy younger than this
run: |
set -euo pipefail
api="https://api.netlify.com/api/v1"
auth="Authorization: Bearer ${NETLIFY_AUTH_TOKEN}"
cutoff="$(( $(date +%s) - MAX_AGE_DAYS * 24 * 3600 ))"
deploys="$(curl -fsS -H "$auth" "$api/sites/${NETLIFY_SITE_ID}/deploys?per_page=100")"
# Delete a deploy only if it is BOTH older than the cutoff AND beyond its group's
# minimum (newest $KEEP_STABLE stable / newest $KEEP_PREVIEW previews). So every
# deploy younger than $MAX_AGE_DAYS is kept, and each group keeps at least its
# minimum regardless of age.
ids="$(printf '%s' "$deploys" | jq -r \
--argjson keep_stable "$KEEP_STABLE" \
--argjson keep_preview "$KEEP_PREVIEW" \
--argjson cutoff "$cutoff" '
def isold: (.created_at | sub("\\.[0-9]+";"") | fromdateiso8601) < $cutoff;
( [ .[] | select(.draft == false and .state == "ready") ]
| sort_by(.created_at) | reverse | .[$keep_stable:] | .[] | select(isold) | .id ),
( [ .[] | select(.draft == true and .state == "ready") ]
| sort_by(.created_at) | reverse | .[$keep_preview:] | .[] | select(isold) | .id )
')"
if [ -z "$ids" ]; then echo "Nothing to prune."; exit 0; fi
while IFS= read -r id; do
[ -n "$id" ] || continue
# `|| echo 000` so a network-level curl failure doesn't trip errexit and abort the loop
code="$(curl -sS -o /dev/null -w '%{http_code}' -X DELETE -H "$auth" "$api/deploys/${id}" || echo 000)"
if [ "$code" = "200" ] || [ "$code" = "204" ]; then
echo "Deleted $id"
else
echo "::warning::Could not delete $id (HTTP $code)"
fi
done <<< "$ids"