Skip to content

Replenish platform pools #29

Replenish platform pools

Replenish platform pools #29

name: "Replenish platform pools"
on:
workflow_call:
inputs:
gitRef:
description: "Commit Id from where the pools should be created"
required: false
default: "main"
type: string
poolMatrixDefPath:
description: "Path to the file describing the pools to create"
required: false
default: "config/poolMatrixdef.json"
type: string
clearPools:
description: "If checked, clear unused scratches in the pools"
required: true
default: false
type: boolean
secrets:
SF_DEVHUB_URL:
required: true
workflow_dispatch:
inputs:
gitRef:
description: "Commit Id from where the pools should be created"
required: false
default: "main"
poolMatrixDefPath:
description: "Path to the file describing the pools to create"
required: true
default: "config/poolMatrixdef.json"
clearPools:
description: "If checked, clear unused scratches in the pools"
required: true
default: "false"
jobs:
checkScratchLimitsThreshold:
name: "Check Scratch limits"
runs-on: ubuntu-latest
outputs:
canReplenishPool: ${{ steps.checkLimits.outputs.canReplenishPool }}
steps:
- name: Setup SF CLI
uses: navikt/sf-platform/.github/actions/installSfCli@main
with:
version: ${{ vars.SF_CLI_VERSION }}
- name: "Authenticate Dev Hub"
uses: navikt/sf-platform/.github/actions/authenticateOrg@main
with:
auth-url: ${{ secrets.SF_DEVHUB_URL }}
alias: devhub
- name: "Check scratch org limits"
id: checkLimits
run: |
printf 'get org limits...\n' >&2
sf org list limits --target-org devhub --json >> limits.json
remainingDailyScratchOrgs=$(jq '.result[] | select(.name=="DailyScratchOrgs").remaining' limits.json)
maxDailyScratchOrgs=$(jq '.result[] | select(.name=="DailyScratchOrgs").max' limits.json)
remainingActiveScratchOrgs=$(jq '.result[] | select(.name=="ActiveScratchOrgs").remaining' limits.json)
maxActiveScratchOrgs=$(jq '.result[] | select(.name=="ActiveScratchOrgs").max' limits.json)
rm limits.json
echo "canReplenishPool=true" >> "$GITHUB_OUTPUT"
printf 'Check remaining daily scratch orgs\n' >&2
if [ $remainingDailyScratchOrgs -lt ${{ vars.POOL_REMAINING_DAILY_SCRATCH_ORG_LIMIT }} ]
then
echo "::warning title=To few remaining daily scratch orgs::Max daily scatch orgs: $maxDailyScratchOrgs. Remaining daily scratch orgs: $remainingDailyScratchOrgs. Threshold: ${{ vars.POOL_REMAINING_DAILY_SCRATCH_ORG_LIMIT }}."
echo "canReplenishPool=" >> "$GITHUB_OUTPUT"
fi
printf 'Check remaining active scratch orgs\n' >&2
if [ $remainingActiveScratchOrgs -lt ${{ vars.POOL_REMAINING_ACTIVE_SCRATCH_ORG_LIMIT }} ]
then
echo "::warning title=To many active Scratch Orgs::Max active scratch orgs: $maxActiveScratchOrgs. Remaining scratch orgs: $remainingActiveScratchOrgs. Threshold: ${{ vars.POOL_REMAINING_ACTIVE_SCRATCH_ORG_LIMIT }}."
echo "canReplenishPool=" >> "$GITHUB_OUTPUT"
fi
prepareMatrix:
runs-on: ubuntu-latest
outputs:
matrixJson: ${{ steps.loadFile.outputs.matrixJson }}
steps:
- id: loadFile
run: |
content='cat ${{ inputs.poolMatrixDefPath }}'
echo "matrixJson=$content" >> "$GITHUB_OUTPUT"
createPool:
name: "Replenish Pool"
needs: [prepareMatrix, checkScratchLimitsThreshold]
if: needs.checkScratchLimitsThreshold.outputs.canReplenishPool
runs-on: ubuntu-latest
container: ghcr.io/flxbl-io/sfp:${{ vars.SFP_CONTAINER_VERSION }}
timeout-minutes: 720 #Set to Maximum Time out
strategy:
matrix: ${{ fromJson(needs.prepareMatrix.outputs.matrixJson) }}
permissions:
contents: read
packages: read
env:
POOL_TAG: ""
steps:
- uses: actions/checkout@v4
with:
ref: ${{ inputs.gitRef }}
fetch-depth: 0
- name: Set pool tag
run: |
pooltag=$(jq --raw-output '.tag' ${{ matrix.poolConfigPath }})
echo "POOL_TAG=$pooltag" >> $GITHUB_ENV
- name: Print Vaules
run: |
echo Pool Tag: "$POOL_TAG"
echo Pool Type: ${{ matrix.poolType }}
echo Pool Config Path: ${{ matrix.poolConfigPath }}
echo Pool Delete Job Type: ${{ matrix.deleteJobType }}
echo Clear unused scratch orgs: ${{ inputs.clearPools }}
- name: "Authenticate Dev Hub"
uses: navikt/sf-platform/.github/actions/authenticateOrg@main
with:
auth-url: ${{ secrets.SF_DEVHUB_URL }}
alias: devhub
- name: "Clear pool"
id: clearPool
if: ${{ github.event.schedule == '15 2 * * *' }}
uses: navikt/sf-platform/.github/actions/ciDeletePool@main
with:
devhub: "devhub"
poolTag: ${{ env.POOL_TAG }}
deleteJobType: ${{ matrix.deleteJobType }}
- name: "Create Pool"
uses: navikt/sf-platform/.github/actions/ciCreatePool@main
with:
devhub: "devhub"
poolConfigPath: ${{ matrix.poolConfigPath }}
nodeToken: ${{ secrets.GITHUB_TOKEN }}
- name: "Delete orphans"
uses: navikt/sf-platform/.github/actions/ciDeletePool@main
with:
devhub: "devhub"
deleteJobType: orphans