Skip to content

Commit

Permalink
feat: delete keep-latest (#352)
Browse files Browse the repository at this point in the history
* feat: delete keep-latest

* ci: delete deployments on main
  • Loading branch information
andykenward authored May 6, 2024
1 parent 64f4736 commit 1c3239a
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-snails-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"github-actions-cloudflare-pages": minor
---

feat: delete workflow keep-latest number of deployments.
10 changes: 10 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
github-environment: ${{ inputs.environment || 'preview' }}
working-directory: example
- id: 'cloudflare-pages-delete'
uses: ./delete
# if main branch
if: github.ref == 'refs/heads/main'
with:
cloudflare-api-token: ${{secrets.CLOUDFLARE_API_TOKEN}}
github-token: ${{ secrets.GITHUB_TOKEN }}
# Only required for legacy action deployments.
github-environment: ${{ inputs.environment || 'preview' }}
keep-latest: 2
4 changes: 4 additions & 0 deletions delete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ github-token:
github-environment:
description: 'GitHub environment to delete deployments from. Leave undefined to delete all deployments referencing the current branch or pull_request.'
required: false
keep-latest:
description: 'How many deployments to keep. Default is 0.'
default: '0'
required: false
```
## Examples
Expand Down
4 changes: 4 additions & 0 deletions delete/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ inputs:
github-environment:
description: 'GitHub environment to delete deployments from. If not defined any deployments link to the branch/pr are deleted.'
required: false
keep-latest:
description: 'How many deployments to keep. Default is 0.'
default: '0'
required: false

runs:
using: node20
Expand Down
10 changes: 5 additions & 5 deletions dist/delete/index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/delete/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/deploy/index.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion input-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ export const INPUT_KEY_DIRECTORY = 'directory'
export const INPUT_KEY_GITHUB_ENVIRONMENT = 'github-environment'
export const INPUT_KEY_GITHUB_TOKEN = 'github-token'
export const INPUT_KEY_WORKING_DIRECTORY = 'working-directory'
export const INPUT_KEYS_KEEP_LATEST = 'keep-latest'

export const INPUT_KEYS_REQUIRED = [
INPUT_KEY_CLOUDFLARE_ACCOUNT_ID,
INPUT_KEY_CLOUDFLARE_API_TOKEN,
INPUT_KEY_CLOUDFLARE_PROJECT_NAME,
INPUT_KEY_DIRECTORY,
INPUT_KEY_GITHUB_ENVIRONMENT,
INPUT_KEY_GITHUB_TOKEN
INPUT_KEY_GITHUB_TOKEN,
INPUT_KEYS_KEEP_LATEST
] as const
26 changes: 26 additions & 0 deletions src/delete/inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {getInput} from '@actions/core'

import {INPUT_KEYS_KEEP_LATEST} from '@/input-keys'

export interface Inputs {
/** How many deployments to keep. */
keepLatest: number
}

const getInputs = (): Inputs => {
return {
keepLatest: Number(
getInput(INPUT_KEYS_KEEP_LATEST, {required: false, trimWhitespace: true})
)
}
}

type UseInputs = ReturnType<typeof getInputs>

let _inputs: UseInputs

export const useInputs = (): UseInputs => {
return process.env.NODE_ENV === 'test'
? getInputs()
: _inputs ?? (_inputs = getInputs())
}
13 changes: 10 additions & 3 deletions src/delete/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ import {debug, info, setFailed, summary} from '@actions/core'
import {batchDelete} from '@/common/batch-delete.js'
import {getGitHubDeployments} from '@/common/github/deployment/get.js'

import {useInputs} from './inputs.js'

const PREFIX = `delete -`

export async function run() {
const deployments = await getGitHubDeployments()
let deployments = await getGitHubDeployments()

const {keepLatest} = useInputs()
if (deployments.length > 0 && keepLatest) {
info(`${PREFIX} Keeping latest ${keepLatest} deployments`)
deployments = deployments.slice(keepLatest)
}
if (deployments.length === 0) {
info(`${PREFIX} No deployments found to delete`)
info(`${PREFIX} No deployments to delete`)

await summary
.addHeading('andykenward/github-actions-cloudflare-pages')
.addBreak()
.addTable([['No deployments found to delete']])
.addTable([['No deployments to delete']])
.write()
return
}
Expand Down

0 comments on commit 1c3239a

Please sign in to comment.