|
1 | 1 | name: "Preview environment garbage collection"
|
2 | 2 | on:
|
3 | 3 | workflow_dispatch:
|
| 4 | + schedule: |
| 5 | + - cron: "0 */4 * * *" |
4 | 6 | jobs:
|
5 |
| - gc: |
6 |
| - name: "Delete unused preview environments" |
| 7 | + stale: |
| 8 | + name: "Find stale preview environments" |
7 | 9 | runs-on: [self-hosted]
|
| 10 | + container: |
| 11 | + image: eu.gcr.io/gitpod-core-dev/dev/dev-environment:mads-leeway-v0.7.3.4 |
| 12 | + outputs: |
| 13 | + names: ${{ steps.set-matrix.outputs.names }} |
| 14 | + count: ${{ steps.set-matrix.outputs.count }} |
8 | 15 | steps:
|
9 | 16 | - uses: actions/checkout@v3
|
10 |
| - - name: Garbage collect |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + - name: Compute matrix |
| 20 | + id: set-matrix |
| 21 | + shell: bash |
| 22 | + env: |
| 23 | + PREVIEW_ENV_DEV_SA_KEY: ${{ secrets.GCP_CREDENTIALS }} |
11 | 24 | run: |
|
12 |
| - echo "Not implemented yet 🧹" |
| 25 | + set -euo pipefail |
| 26 | +
|
| 27 | + export LEEWAY_WORKSPACE_ROOT="$(pwd)" |
| 28 | + export HOME="/home/gitpod" |
| 29 | + export PREVIEW_ENV_DEV_SA_KEY_PATH="/home/gitpod/.config/gcloud/preview-environment-dev-sa.json" |
| 30 | + # Used by 'previewctl list stale' |
| 31 | + export GOOGLE_APPLICATION_CREDENTIALS="/home/gitpod/.config/gcloud/preview-environment-dev-sa.json" |
| 32 | +
|
| 33 | + echo "${PREVIEW_ENV_DEV_SA_KEY}" > "${PREVIEW_ENV_DEV_SA_KEY_PATH}" |
| 34 | + gcloud auth activate-service-account --key-file "${PREVIEW_ENV_DEV_SA_KEY_PATH}" |
| 35 | +
|
| 36 | + leeway run dev/preview/previewctl:install |
| 37 | +
|
| 38 | + previewctl get-credentials --gcp-service-account "${PREVIEW_ENV_DEV_SA_KEY_PATH}" |
| 39 | + previewctl list stale | jq --null-input --raw-input --compact-output '[inputs | select(length>0)]' > /tmp/stale-json |
| 40 | + echo "names=$(cat /tmp/stale-json)" >> $GITHUB_OUTPUT |
| 41 | + echo "count=$(jq '. | length' /tmp/stale-json)" >> $GITHUB_OUTPUT |
| 42 | +
|
| 43 | + delete: |
| 44 | + name: "Delete preview environment" |
| 45 | + needs: [stale] |
| 46 | + runs-on: [self-hosted] |
| 47 | + if: ${{ needs.stale.outputs.count > 0 }} |
| 48 | + strategy: |
| 49 | + matrix: |
| 50 | + name: ${{ fromJSON(needs.stale.outputs.names) }} |
| 51 | + steps: |
| 52 | + - uses: actions/checkout@v3 |
| 53 | + - name: Delete preview environment ${{ matrix.name }} |
| 54 | + uses: ./.github/actions/delete-preview |
| 55 | + with: |
| 56 | + name: ${{ matrix.name }} |
| 57 | + sa_key: ${{ secrets.GCP_CREDENTIALS }} |
0 commit comments