ci: fix #12
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: Docker Environment Manager | |
on: | |
push: | |
branches: [ "dev" ] | |
pull_request: | |
branches: [ "dev" ] | |
workflow_dispatch: | |
permissions: | |
contents: read | |
packages: write | |
jobs: | |
check-rebuild-needed: | |
runs-on: ubuntu-latest | |
outputs: | |
should_build: ${{ steps.check-date.outputs.should_build }} | |
steps: | |
- name: Get latest image info | |
id: check-date | |
run: | | |
# Get repository name in lowercase | |
REPO_NAME=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]') | |
# 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/r-env/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: 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: 'r-env' | |
package-type: 'container' | |
min-versions-to-keep: 10 | |
delete-only-untagged-versions: false | |
build-and-push: | |
needs: [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: Set container name | |
id: set-name | |
run: | | |
LOWER_NAME=$(echo "${{ github.repository }}" | awk '{print tolower($0)}') | |
echo "name=${LOWER_NAME}" >> $GITHUB_OUTPUT | |
- 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: ghcr.io/${{ steps.set-name.outputs.name }}/r-env | |
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=ghcr.io/${{ steps.set-name.outputs.name }}/r-env:latest | |
cache-to: type=inline |