-
Notifications
You must be signed in to change notification settings - Fork 99
95 lines (86 loc) · 3.33 KB
/
2-pre-release.yaml
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
name: 2-Publish container image to GitHub Packages
on:
push:
branches:
- dev
workflow_dispatch:
env:
REGISTRY: ghcr.io
PACKAGE_NAME: carbon-aware-sdk
IMAGE_NAME: ${{ github.repository }}
NUM_KEEP_IMAGES: 3
concurrency:
group: publish
cancel-in-progress: true
jobs:
publish-container-image:
if: github.repository == 'Green-Software-Foundation/carbon-aware-sdk' || vars.ENABLE_PRERELEASE_WORKFLOW == 'true'
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to the Container registry
uses: docker/[email protected]
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/[email protected]
with:
tags: |
type=raw,value=pre
type=sha
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
uses: docker/[email protected]
with:
context: "{{defaultContext}}:src"
file: CarbonAware.WebApi/src/Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
provenance: false
- name: List older container images to remove
id: list-older-images
run: |
$image_lower = "${{ env.IMAGE_NAME }}".ToLower()
# Retrieve information of all of container images
$versions = gh api orgs/${{ github.repository_owner }}/packages/container/${{ env.PACKAGE_NAME }}/versions | ConvertFrom-Json
$digests = New-Object System.Collections.Generic.HashSet[string]
# Pick up digest of tagged images which should be removed
$versions |
Where-Object {$_.metadata.container.tags.Where({$_.StartsWith("sha-")}, "First").Count -gt 0} |
Sort-Object -Property created_at -Descending |
Select-Object -Skip ${{ env.NUM_KEEP_IMAGES }} |
ForEach-Object {[void]$digests.add($_.name)}
# Pick up digest of children tagged images which should be removed
$loop_set = New-Object System.Collections.Generic.HashSet[string]($digests)
$loop_set.foreach{
docker manifest inspect "${{ env.REGISTRY }}/$image_lower@$_" |
Out-String |
ConvertFrom-Json |
Select-Object -ExpandProperty "manifests" |
ForEach-Object {[void]$digests.Add($_.digest)}
}
# Retrieve IDs to be removed
$ids = $versions |
Where-Object {$digests.Contains($_.name)} |
Select-Object -ExpandProperty id
$remove_images = $ids -join ","
"REMOVE_IMAGES=$remove_images" >> $env:GITHUB_OUTPUT
env:
GH_TOKEN: ${{ github.token }}
shell: pwsh
- name: Remove stale packages
uses: actions/delete-package-versions@v4
with:
package-name: carbon-aware-sdk
package-type: container
package-version-ids: ${{ steps.list-older-images.outputs.REMOVE_IMAGES }}