Build and Push Dev Docker Image #944
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: Build and Push Dev Docker Image | |
on: | |
schedule: | |
- cron: '0 */2 * * *' # Run every 2 hours | |
workflow_dispatch: # Allow manual triggering | |
push: | |
paths-ignore: # Prevent triggering on updates to .last_dev_build | |
- '.last_build' | |
- '.last_dev_build' | |
- '.last_prerelease_build' | |
- 'dockerfile' | |
- 'dockerfile.pre' | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
SOURCE_REPO: TNTwise/REAL-Video-Enhancer | |
jobs: | |
build-and-push-dev: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
packages: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Get last commit info from source repo | |
id: last_commit | |
run: | | |
DEFAULT_BRANCH=$(curl -s https://api.github.com/repos/${{ env.SOURCE_REPO }} | jq '.default_branch' | tr -d \") | |
LAST_COMMIT_INFO=$(curl -s "https://api.github.com/repos/${{ env.SOURCE_REPO }}/commits/$DEFAULT_BRANCH") | |
LAST_COMMIT_TIME=$(echo $LAST_COMMIT_INFO | jq -r .commit.author.date) | |
LAST_COMMIT_TIMESTAMP=$(date -d "$LAST_COMMIT_TIME" +%s) | |
CURRENT_TIME=$(date +%s) | |
TIME_DIFF=$((CURRENT_TIME - LAST_COMMIT_TIMESTAMP)) | |
if [ $TIME_DIFF -ge 14400 ]; then # 14400 seconds = 4 hours | |
echo "build=true" >> $GITHUB_OUTPUT | |
else | |
echo "build=false" >> $GITHUB_OUTPUT | |
fi | |
echo "LAST_COMMIT_HASH=$(echo $LAST_COMMIT_INFO | jq -r .sha)" >> $GITHUB_OUTPUT | |
- name: Determine if build should proceed | |
id: check | |
run: | | |
# Define individual conditions for easier readability | |
MANUAL_TRIGGER=$([[ "${{ github.event_name }}" == "workflow_dispatch" ]] && echo true || echo false) | |
NEW_COMMIT_PUSHED=$([[ "${{ github.event_name }}" == "push" && "${{ github.actor }}" != "github-actions" ]] && echo true || echo false) | |
SOURCE_REPO_OLD=$([[ "${{ steps.last_commit.outputs.build }}" == "true" ]] && echo true || echo false) | |
# Check if any condition is met to proceed with the build | |
if [[ $MANUAL_TRIGGER == "true" || $NEW_COMMIT_PUSHED == "true" || $SOURCE_REPO_OLD == "true" ]]; then | |
echo "update=true" >> $GITHUB_OUTPUT | |
else | |
echo "update=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Prepare Docker metadata | |
id: meta | |
if: steps.check.outputs.update == 'true' | |
run: | | |
echo "IMAGE_ID=$(echo ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | tr '[A-Z]' '[a-z]')" >> $GITHUB_OUTPUT | |
- name: Log in to the Container registry | |
if: steps.check.outputs.update == 'true' | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push Docker image | |
if: steps.check.outputs.update == 'true' | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.meta.outputs.IMAGE_ID }}:dev | |
file: ./dockerfile.dev | |
- name: Update last build info | |
if: steps.check.outputs.update == 'true' | |
run: | | |
# Function to update the .last_dev_build file | |
update_last_build_file() { | |
echo "$1" > .last_dev_build | |
git config user.name github-actions | |
git config user.email [email protected] | |
git add .last_dev_build | |
git commit -m "Update last dev build info" | |
git push | |
} | |
# Get the new commit hash from the output | |
NEW_COMMIT_HASH="${{ steps.last_commit.outputs.LAST_COMMIT_HASH }}" | |
# Check if .last_dev_build exists | |
if [[ -f .last_dev_build ]]; then | |
# Compare with the old commit hash stored in .last_dev_build | |
OLD_COMMIT_HASH=$(cat .last_dev_build) | |
if [[ "$NEW_COMMIT_HASH" != "$OLD_COMMIT_HASH" ]]; then | |
update_last_build_file "$NEW_COMMIT_HASH" | |
else | |
echo "No new commit, skipping the commit step." | |
fi | |
else | |
# If the file doesn't exist, create it and commit | |
update_last_build_file "$NEW_COMMIT_HASH" | |
fi |