Docker Auto Build and Push #439
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 Auto Build and Push | |
on: | |
schedule: | |
- cron: '0 0 * * *' # Runs at 00:00 UTC daily | |
push: | |
branches: | |
- main | |
workflow_dispatch: | |
permissions: | |
contents: write | |
env: | |
BITPING_UPDATE_JSON: ${{ secrets.BITPING_UPDATE_JSON }} | |
IMAGE_NAME: 'mrcolorrain/bitping' | |
TAG: 'latest' | |
MAX_DELAY: 60 # Maximum delay in seconds | |
VERSION_FILE_PATH: '.githubworkflows.current_version' # Path to the version file | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Randomized Delay | |
run: | | |
sleep $((RANDOM % $MAX_DELAY)) | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Check for updates in Bitpingd | |
id: check-update | |
run: | | |
update_json=$(curl --silent ${{ env.BITPING_UPDATE_JSON }}) | |
last_version=$(echo "$update_json" | grep '"version":' | sed -E 's/.*"([^"]+)".*/\1/') | |
echo "Latest version: $last_version" | |
echo "version=$last_version" >> $GITHUB_OUTPUT | |
- name: Compare with previous version | |
id: compare-version | |
run: | | |
mkdir -p $(dirname ${{ env.VERSION_FILE_PATH }}) | |
if [ -f ${{ env.VERSION_FILE_PATH }} ]; then | |
previous_ver=$(cat ${{ env.VERSION_FILE_PATH }}) | |
else | |
echo 'none' > ${{ env.VERSION_FILE_PATH }} | |
previous_ver='none' | |
fi | |
echo "Previous version: $previous_ver" | |
if [ "$previous_ver" != "${{ steps.check-update.outputs.version }}" ]; then | |
echo "New version available. Triggering build..." | |
echo "${{ steps.check-update.outputs.version }}" > ${{ env.VERSION_FILE_PATH }} | |
echo "trigger_build=true" >> $GITHUB_OUTPUT | |
else | |
echo "No new version. Skipping build." | |
echo "trigger_build=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Setup QEMU | |
if: steps.compare-version.outputs.trigger_build == 'true' | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
if: steps.compare-version.outputs.trigger_build == 'true' | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to DockerHub | |
if: steps.compare-version.outputs.trigger_build == 'true' | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and Push Docker Image | |
if: steps.compare-version.outputs.trigger_build == 'true' | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
platforms: linux/amd64,linux/arm64,linux/arm/v7 | |
tags: ${{ env.IMAGE_NAME}}:${{ env.TAG }} | |
push: true | |
cache-from: type=gha | |
cache-to: type=gha | |
- name: Commit and push updates version file | |
if: steps.compare-version.outputs.trigger_build == 'true' | |
run: | | |
git config --global user.name 'GitHub Actions' | |
git config --global user.email 'mrcolorr@users.noreply.github.com' | |
git add ${{ env.VERSION_FILE_PATH }} | |
git commit -m "Update version file to ${{ steps.check-update.outputs.version }}" | |
git push | |
- name: Create and push tag | |
if: steps.compare-version.outputs.trigger_build == 'true' | |
run: | | |
git config --global user.name 'GitHub Actions' | |
git config --global user.email 'mrcolorr@users.noreply.github.com' | |
git tag -a ${{ steps.check-update.outputs.version }} -m "Update to ${{ steps.check-update.outputs.version }}" | |
git push origin ${{ steps.check-update.outputs.version }} |