Skip to content

ci: fix

ci: fix #13

name: Docker Environment Manager
on:
push:
branches: [ "dev" ]
pull_request:
branches: [ "dev" ]
workflow_dispatch:
permissions:
contents: read
packages: write
jobs:
setup:
runs-on: ubuntu-latest
outputs:
container_name: ${{ steps.set-name.outputs.name }}
steps:
- name: Set container name
id: set-name
run: |
LOWER_NAME=$(echo "${{ github.repository }}" | awk '{print tolower($0)}')
echo "name=ghcr.io/${LOWER_NAME}/r-env" >> $GITHUB_OUTPUT
check-rebuild-needed:
needs: setup
runs-on: ubuntu-latest
outputs:
should_build: ${{ steps.check-date.outputs.should_build }}
steps:
- name: Get latest image info
id: check-date
run: |
# Extract package name from container name
PACKAGE_NAME=$(echo "${{ needs.setup.outputs.container_name }}" | sed 's|ghcr.io/||')
# Query GHCR API for latest image
RESPONSE=$(curl -s \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${{ github.token }}" \
"https://api.github.com/users/${{ github.repository_owner }}/packages/container/${PACKAGE_NAME#*/}/versions")
# Check if response is empty or invalid
if ! echo "$RESPONSE" | jq . > /dev/null 2>&1; then
echo "should_build=true" >> $GITHUB_OUTPUT
echo "Invalid API response, will build new image"
exit 0
fi
# Get creation date of latest version (if exists)
LATEST_DATE=$(echo "$RESPONSE" | jq -r 'if type=="array" and length>0 then .[0].created_at else empty end')
if [ -z "$LATEST_DATE" ]; then
# No image exists, definitely build
echo "should_build=true" >> $GITHUB_OUTPUT
echo "No existing image found, will build new one"
exit 0
fi
# Convert dates to timestamps
LATEST_TS=$(date -d "$LATEST_DATE" +%s)
CURRENT_TS=$(date +%s)
# Calculate difference in days
DAYS_DIFF=$(( ($CURRENT_TS - $LATEST_TS) / 86400 ))
if [ $DAYS_DIFF -ge 7 ]; then
echo "should_build=true" >> $GITHUB_OUTPUT
echo "Latest image is $DAYS_DIFF days old, will rebuild"
else
echo "should_build=false" >> $GITHUB_OUTPUT
echo "Latest image is only $DAYS_DIFF days old, skipping rebuild"
fi
cleanup-images:
needs: [setup, check-rebuild-needed]
if: needs.check-rebuild-needed.outputs.should_build == 'true'
runs-on: ubuntu-latest
steps:
- name: Delete old images
uses: actions/delete-package-versions@v4
with:
package-name: '${{ github.repository_owner }}/r-env'
package-type: 'container'
min-versions-to-keep: 10
delete-only-untagged-versions: false
build-and-push:
needs: [setup, check-rebuild-needed, cleanup-images]
if: needs.check-rebuild-needed.outputs.should_build == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: dev
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ needs.setup.outputs.container_name }}
tags: |
type=raw,value={{date 'YYYYMMDD'}}
type=raw,value=latest
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./.github/docker/testing-environment.Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ needs.setup.outputs.container_name }}:latest
cache-to: type=inline