Auto GLPI Update and Docker Image CI #29
This file contains hidden or 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: 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 | |
| # Also keep the push/tag triggers from Workflow 2 to allow manual/external triggers to build the image independently | |
| 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: # Merged and renamed from 'Auto Update GLPI Version' | |
| runs-on: ubuntu-latest | |
| # Export outputs for the downstream 'docker' job to use | |
| 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 # Fetch all history for proper tag operations | |
| - name: Get current version from Dockerfile | |
| id: current_version_dockerfile | |
| run: | | |
| CURRENT_VERSION_IN_FILE=$(grep 'ENV VERSION_GLPI=' Dockerfile | cut -d'"' -f2) | |
| echo "Current version in Dockerfile: $CURRENT_VERSION_IN_FILE" | |
| if [ -z "$CURRENT_VERSION_IN_FILE" ]; then | |
| echo "Error: Could not extract current version from Dockerfile." | |
| exit 1 | |
| fi | |
| echo "version=$CURRENT_VERSION_IN_FILE" >> $GITHUB_OUTPUT | |
| - name: Get latest GLPI release version | |
| id: latest_glpi_release | |
| run: | | |
| # Requires 'jq' to be available on the runner (default for ubuntu-latest) | |
| NEW_GLPI_VERSION=$(curl -sL https://api.github.com/repos/glpi-project/glpi/releases/latest | jq -r .tag_name) | |
| echo "Latest GLPI version from upstream (glpi-project/glpi): $NEW_GLPI_VERSION" | |
| if [ -z "$NEW_GLPI_VERSION" ]; then | |
| echo "Error: Could not fetch latest GLPI release version from upstream." | |
| exit 1 | |
| fi | |
| echo "version=$NEW_GLPI_VERSION" >> $GITHUB_OUTPUT | |
| - name: Compare versions and proceed if new version is available | |
| id: version_check | |
| run: | | |
| CURRENT_VER="${{ steps.current_version_dockerfile.outputs.version }}" | |
| LATEST_UPSTREAM_VER="${{ steps.latest_glpi_release.outputs.version }}" | |
| echo "Comparing Current in Dockerfile: $CURRENT_VER with Latest Upstream GLPI: $LATEST_UPSTREAM_VER" | |
| if dpkg --compare-versions "$LATEST_UPSTREAM_VER" gt "$CURRENT_VER"; then | |
| echo "New GLPI version available: $LATEST_UPSTREAM_VER" | |
| echo "update_needed=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "Current version $CURRENT_VER is up-to-date or newer than upstream $LATEST_UPSTREAM_VER. No update needed." | |
| echo "update_needed=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Update files if new version is available | |
| if: steps.version_check.outputs.update_needed == 'true' | |
| run: | | |
| CURRENT_VER_IN_FILES="${{ steps.current_version_dockerfile.outputs.version }}" | |
| NEW_UPSTREAM_GLPI_VER="${{ steps.latest_glpi_release.outputs.version }}" | |
| echo "Updating files from $CURRENT_VER_IN_FILES to $NEW_UPSTREAM_GLPI_VER..." | |
| # Update all necessary files: Dockerfile, docker-compose.yml, glpi-start.sh, README.md, README_FR.md | |
| # 1. Update Dockerfile | |
| sed -i "s/ENV VERSION_GLPI=\"${CURRENT_VER_IN_FILES}\"/ENV VERSION_GLPI=\"${NEW_UPSTREAM_GLPI_VER}\"/" Dockerfile | |
| # 2. Update docker-compose.yml | |
| sed -i "s|image: triatk/glpi-standalone:${CURRENT_VER_IN_FILES}|image: triatk/glpi-standalone:${NEW_UPSTREAM_GLPI_VER}|g" docker-compose.yml | |
| sed -i "s/- VERSION_GLPI=${CURRENT_VER_IN_FILES}/- VERSION_GLPI=${NEW_UPSTREAM_GLPI_VER}/g" docker-compose.yml | |
| # 3. Update glpi-start.sh | |
| sed -i "s/:=${CURRENT_VER_IN_FILES}}/:=${NEW_UPSTREAM_GLPI_VER}}/" glpi-start.sh | |
| # 4 & 5. Update README files (simplified for brevity, assumes original sed commands work) | |
| sed -i "s|${CURRENT_VER_IN_FILES}|${NEW_UPSTREAM_GLPI_VER}|g" README.md README_FR.md || true | |
| - name: Commit and push changes | |
| if: steps.version_check.outputs.update_needed == 'true' | |
| run: | | |
| NEW_UPSTREAM_GLPI_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" | |
| # Check for changes before committing | |
| if git diff --exit-code; then | |
| echo "No changes detected after version bump. Skipping commit." | |
| else | |
| git add Dockerfile docker-compose.yml glpi-start.sh README.md README_FR.md | |
| git commit -m "Bump GLPI to ${NEW_UPSTREAM_GLPI_VER} (Automated)" | |
| BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) | |
| git push origin "$BRANCH_NAME" | |
| echo "Changes pushed to branch $BRANCH_NAME." | |
| fi | |
| - name: Create Git tag | |
| if: steps.version_check.outputs.update_needed == 'true' | |
| run: | | |
| NEW_REPO_TAG_NAME="${{ steps.latest_glpi_release.outputs.version }}" | |
| git tag "$NEW_REPO_TAG_NAME" -m "Release GLPI Standalone based on GLPI ${NEW_REPO_TAG_NAME}" | |
| git push origin "$NEW_REPO_TAG_NAME" | |
| echo "Tag $NEW_REPO_TAG_NAME pushed." | |
| - 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 for GLPI Standalone. | |
| This image incorporates GLPI version ${{ steps.latest_glpi_release.outputs.version }}. | |
| Upstream GLPI release notes: https://github.com/glpi-project/glpi/releases/tag/${{ steps.latest_glpi_release.outputs.version }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: No update performed | |
| if: steps.version_check.outputs.update_needed == 'false' | |
| run: echo "No update was performed as the current version is up-to-date or newer." | |
| docker: # Original Workflow 2 job, now dependent on the update | |
| runs-on: ubuntu-latest | |
| needs: check-update-commit # Ensure this job runs after the version check | |
| # The Docker build job will run in two cases: | |
| # 1. If the 'check-update-commit' job found a new version and pushed changes/tag. | |
| # 2. If the workflow was manually triggered by push/tag (github.event_name is 'push' or 'create' which is tag push). | |
| if: | | |
| (github.event_name == 'push' || github.event_name == 'create') || | |
| (needs.check-update-commit.outputs.update_needed == 'true') | |
| steps: | |
| - name: Checkout repository (to get the updated files and new tag) | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Get tags for Docker meta action | |
| - 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 new version tag directly if it was an automated update | |
| type=raw,value=${{ needs.check-update-commit.outputs.new_version }},enable=${{ needs.check-update-commit.outputs.update_needed == 'true' }} | |
| # Fallback to original tagging logic for push/tag triggers | |
| 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 }} |