Skip to content

Auto GLPI Update and Docker Image CI #58

Auto GLPI Update and Docker Image CI

Auto GLPI Update and Docker Image CI #58

name: Auto GLPI Update and Docker Image CI
on:
schedule:
# Runs at 03:00 UTC every Monday
- cron: "0 3 * * 1"
workflow_dispatch: # Allows manual triggering
inputs:
force_rebuild:
description: 'Force Docker Image Rebuild (even if version matches)'
required: false
default: false
type: boolean
# Keep push/tag triggers for manual/external triggers
push:
branches:
- 'main'
tags:
- "*.*.*"
paths-ignore:
- 'README.md'
- 'LICENSE'
- 'README_FR.md'
permissions:
contents: write # To commit changes, create tags, and create releases
jobs:
check-update-commit:
runs-on: ubuntu-latest
outputs:
update_needed: ${{ steps.version_check.outputs.update_needed }}
new_version: ${{ steps.latest_glpi_release.outputs.version }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get current version from Dockerfile
id: current_version_dockerfile
run: |
# Extract version (handles quotes, spaces, or malformed lines)
CURRENT_VERSION_IN_FILE=$(sed -nE 's/.*VERSION_GLPI[ ="\t]+([0-9]+\.[0-9]+\.[0-9]+).*/\1/p' Dockerfile | head -n 1)
if [ -z "$CURRENT_VERSION_IN_FILE" ]; then
echo "Warning: Could not extract current version from Dockerfile."
echo "version=null" >> $GITHUB_OUTPUT
else
echo "Current version in Dockerfile: $CURRENT_VERSION_IN_FILE"
echo "version=$CURRENT_VERSION_IN_FILE" >> $GITHUB_OUTPUT
fi
- name: Get latest GLPI release version
id: latest_glpi_release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Fetch releases, filter, sort by version, take top one
NEW_GLPI_VERSION=$(curl -sL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
"https://api.github.com/repos/glpi-project/glpi/releases" \
| jq -r '.[] | select(.prerelease==false and .draft==false) | .tag_name' \
| sort -V \
| tail -n 1)
echo "Latest GLPI version from upstream: $NEW_GLPI_VERSION"
if [ -z "$NEW_GLPI_VERSION" ] || [ "$NEW_GLPI_VERSION" == "null" ]; then
echo "Error: Could not fetch valid version from upstream. Aborting."
exit 1
fi
echo "version=$NEW_GLPI_VERSION" >> $GITHUB_OUTPUT
- name: Compare versions
id: version_check
run: |
CURRENT_VER="${{ steps.current_version_dockerfile.outputs.version }}"
LATEST_UPSTREAM_VER="${{ steps.latest_glpi_release.outputs.version }}"
echo "Comparing Current: $CURRENT_VER vs Upstream: $LATEST_UPSTREAM_VER"
# If current is null, we MUST update to fix the build
if [ "$CURRENT_VER" == "null" ]; then
echo "Current version is unknown/null. Forcing update."
echo "update_needed=true" >> $GITHUB_OUTPUT
exit 0
fi
if dpkg --compare-versions "$LATEST_UPSTREAM_VER" gt "$CURRENT_VER"; then
echo "New GLPI version available."
echo "update_needed=true" >> $GITHUB_OUTPUT
else
echo "Current version is up-to-date."
echo "update_needed=false" >> $GITHUB_OUTPUT
fi
- name: Update files if new version is available
if: steps.version_check.outputs.update_needed == 'true'
run: |
NEW_VER="${{ steps.latest_glpi_release.outputs.version }}"
echo "Forcing update to version: $NEW_VER"
# Aggressive replacements (fix "null" or mismatching formats)
sed -i "s/^ENV VERSION_GLPI.*/ENV VERSION_GLPI=\"${NEW_VER}\"/" Dockerfile
sed -i "s|image: triatk/glpi-standalone:.*|image: triatk/glpi-standalone:${NEW_VER}|g" docker-compose.yml
sed -i "s|- VERSION_GLPI=.*|- VERSION_GLPI=${NEW_VER}|g" docker-compose.yml
sed -i "s/:=.*}/:=${NEW_VER}}/" glpi-start.sh
CURRENT_VER="${{ steps.current_version_dockerfile.outputs.version }}"
if [ "$CURRENT_VER" != "null" ]; then
sed -i "s|${CURRENT_VER}|${NEW_VER}|g" README.md README_FR.md || true
fi
- name: Commit and push changes
if: steps.version_check.outputs.update_needed == 'true'
run: |
NEW_VER="${{ steps.latest_glpi_release.outputs.version }}"
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
if git diff --exit-code; then
echo "No changes detected. Skipping commit."
else
git add Dockerfile docker-compose.yml glpi-start.sh README.md README_FR.md
git commit -m "Bump GLPI to ${NEW_VER} (Automated)"
git push origin HEAD
fi
- name: Create Git tag
if: steps.version_check.outputs.update_needed == 'true'
run: |
NEW_VER="${{ steps.latest_glpi_release.outputs.version }}"
# Only tag if tag doesn't exist
if git rev-parse "$NEW_VER" >/dev/null 2>&1; then
echo "Tag $NEW_VER already exists. Skipping."
else
git tag "$NEW_VER" -m "Release GLPI Standalone ${NEW_VER}"
git push origin "$NEW_VER"
fi
- name: Create GitHub Release
if: steps.version_check.outputs.update_needed == 'true'
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.latest_glpi_release.outputs.version }}
name: GLPI Standalone ${{ steps.latest_glpi_release.outputs.version }}
body: "Automated release. Upstream: https://github.com/glpi-project/glpi/releases/tag/${{ steps.latest_glpi_release.outputs.version }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
docker:
runs-on: ubuntu-latest
needs: check-update-commit
# Run if:
# 1. Manual push/tag
# 2. Update was needed (new version found)
# 3. Force rebuild was selected manually
if: |
(github.event_name == 'push' || github.event_name == 'create') ||
(needs.check-update-commit.outputs.update_needed == 'true') ||
(inputs.force_rebuild == true)
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Docker meta
id: meta_glpi-standalone
uses: docker/metadata-action@v5
with:
images: |
triatk/glpi-standalone
flavor: |
latest=true
tags: |
# Use the upstream version calculated in the previous job if this is an automated update OR a forced rebuild
type=raw,value=${{ needs.check-update-commit.outputs.new_version }}
# Standard tags
type=schedule
type=ref,event=branch
type=ref,event=pr
type=pep440,pattern={{version}}
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
- name: Build and push
id: docker_build
uses: docker/build-push-action@v6
with:
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta_glpi-standalone.outputs.tags }}
labels: ${{ steps.meta_glpi-standalone.outputs.labels }}