feat: adding retention policy #187
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Container Images | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
release: | |
types: [published] | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
attestations: write | |
id-token: write | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
tags: | | |
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }} | |
type=ref,event=tag | |
type=raw,value=stable,enable=${{ startsWith(github.ref, 'refs/tags/') }} | |
type=ref,event=pr | |
- name: Show tags | |
run: | | |
echo ${{ steps.meta.outputs.tags }} | |
- name: Extract version from tag | |
if: startsWith(github.ref, 'refs/tags/') | |
run: echo "VERSION=${GITHUB_REF##*/}" >> $GITHUB_ENV | |
- name: Log into GitHub Container Registry | |
if: github.event_name != 'pull_request' | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push slim | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./Dockerfile.slim | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta.outputs.tags }}-slim | |
labels: ${{ steps.meta.outputs.labels }} | |
build-args: | | |
BUILD_DATE=${{ github.event.repository.updated_at }} | |
VCS_REF=${{ github.sha }} | |
VERSION=${{ env.VERSION }} | |
- name: Build and push | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
build-args: | | |
SLIM_IMAGE=${{ steps.meta.outputs.tags }}-slim | |
- name: Retain last two minor versions | |
run: | | |
latest_two_minors=$(git tag -l | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | awk -F. '{print $1"."$2}' | uniq | tail -n 2) | |
keep_tags="" | |
for minor in $latest_two_minors; do | |
latest_patch=$(git tag -l "${minor}.*" | sort -V | tail -n 1) | |
keep_tags="$keep_tags $latest_patch" | |
done | |
echo "keep_tags=$(echo $keep_tags)" | |
- name: Run container retention policy | |
if: github.ref == 'refs/heads/main' | |
uses: snok/container-retention-policy@v3 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
keep-tag-names: | | |
stable | |
latest | |
${{ env.keep_tags }} | |