33# SPDX-License-Identifier: Apache-2.0
44# SPDX-ArtifactOfProjectHomePage: https://github.com/vegardit/docker-openldap
55#
6- # https://docs.github.com/en/actions/using -workflows/workflow-syntax-for-github-actions
6+ # https://docs.github.com/en/actions/writing -workflows/workflow-syntax-for-github-actions
77name : Build
88
99on :
2626 - ' .github/*.yml'
2727 - ' .github/workflows/stale.yml'
2828 schedule :
29- # https://docs.github.com/en/actions/using -workflows/events-that-trigger-workflows
29+ # https://docs.github.com/en/actions/writing -workflows/choosing-when-your-workflow-runs /events-that-trigger-workflows
3030 - cron : ' 0 17 * * 3'
3131 workflow_dispatch :
32- # https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with- workflow_dispatch/
32+ # https://docs. github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows# workflow_dispatch
3333
3434defaults :
3535 run :
3636 shell : bash
3737
3838env :
39- DOCKER_IMAGE_REPO : vegardit/ openldap
39+ DOCKER_REPO_NAME : openldap
4040 TRIVY_CACHE_DIR : ~/.trivy/cache
4141
4242jobs :
4343
4444 # ##########################################################
4545 build :
4646 # ##########################################################
47-
4847 runs-on : ubuntu-latest # https://github.com/actions/runner-images#available-images
4948 timeout-minutes : 10
5049
50+ permissions :
51+ packages : write
52+
5153 steps :
5254 - name : " Show: GitHub context"
5355 env :
@@ -99,11 +101,13 @@ jobs:
99101
100102
101103 - name : " Determine if docker images shall be published"
104+ id : docker_push_actions
102105 run : |
103106 # ACT -> https://nektosact.com/usage/index.html#skipping-steps
104107 set -x
105108 if [[ $GITHUB_REF_NAME == 'main' && $GITHUB_EVENT_NAME != 'pull_request' && -z "$ACT" ]]; then
106109 echo "DOCKER_PUSH_GHCR=true" >> "$GITHUB_ENV"
110+ echo "DOCKER_PUSH_GHCR=true" >> $GITHUB_OUTPUT
107111 if [[ -n "${{ secrets.DOCKER_HUB_USERNAME }}" ]]; then
108112 echo "DOCKER_PUSH=true" >> "$GITHUB_ENV"
109113 fi
@@ -124,28 +128,41 @@ jobs:
124128 with :
125129 registry : ghcr.io
126130 username : ${{ github.actor }}
127- password : ${{ github.token }}
131+ password : ${{ secrets.GITHUB_TOKEN }}
128132
129133
130134 - name : Build docker image
131135 env :
132- TRIVY_GITHUB_TOKEN : ${{ github.token }}
136+ DOCKER_IMAGE_REPO : ${{ github.repository_owner }}/${{ env.DOCKER_REPO_NAME }}
137+ TRIVY_GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
133138 run : bash build-image.sh
134139
140+ outputs :
141+ DOCKER_PUSH_GHCR : ${{ steps.docker_push_actions.outputs.DOCKER_PUSH_GHCR }}
142+
143+
144+ # ##########################################################
145+ delete-untagged-images :
146+ # ##########################################################
147+ runs-on : ubuntu-latest # https://github.com/actions/runner-images#available-images
148+ timeout-minutes : 5
149+ needs : [build]
150+ if : ${{ needs.build.outputs.DOCKER_PUSH_GHCR }}
151+
152+ concurrency :
153+ group : ${{ github.workflow }}
154+ cancel-in-progress : false
155+
156+ permissions :
157+ packages : write
135158
159+ steps :
136160 - name : Delete untagged images
137- uses : actions/github-script@v7
138- if : ${{ env.DOCKER_PUSH_GHCR }}
139- continue-on-error : true
161+ uses : dataaxiom/ghcr-cleanup-action@v1
140162 with :
141- github-token : ${{ secrets.GHA_DELETE_PACKAGES }}
142- script : |
143- const imageName = /[^/]*$/.exec(process.env.DOCKER_IMAGE_REPO)[0]
144- const basePath = `/orgs/${{ github.repository_owner }}/packages/container/${imageName}/versions`
145- for (version of (await github.request(`GET ${basePath}`, { per_page: 100 })).data) {
146- if (version.metadata.container.tags.length == 0) {
147- console.log(`deleting ${version.name}...`)
148- const delResponse = await github.request(`DELETE ${basePath}/${version.id}`)
149- console.log(`status: ${delResponse.status}`)
150- }
151- }
163+ package : ${{ env.DOCKER_REPO_NAME }}
164+ delete-untagged : true
165+ delete-partial-images : true
166+ delete-ghost-images : true
167+ delete-orphaned-images : true
168+ validate : true
0 commit comments