Skip to content

Commit

Permalink
Update docker-publish.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
troytc authored Nov 7, 2024
1 parent 4031e99 commit 761cb5c
Showing 1 changed file with 19 additions and 45 deletions.
64 changes: 19 additions & 45 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -1,105 +1,79 @@
name: Docker

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

on:
schedule:
- cron: '20 12 * * *'
push:
branches: [ "main" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "main" ]
workflow_dispatch:

env:
# Use docker.io for Docker Hub if empty
REGISTRY: docker.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}


jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write

steps:
- name: Check Latest Release
if: github.event_name != 'workflow_dispatch'
id: check_release_time
run: |
# Get the latest release timestamp
latest_release=$(curl -s https://api.github.com/repos/jagrosh/MusicBot/releases/latest)
published_at=$(echo "$latest_release" | jq -r '.published_at')
latest_version=$(echo "$latest_release" | jq -r '.tag_name')
# Convert published_at to seconds since epoch
published_at_seconds=$(date -d "$published_at" +%s)

# Get the current time in seconds since epoch
current_time=$(date +%s)

# Calculate the time difference in hours
time_difference=$(( (current_time - published_at_seconds) / 3600 ))

echo "Latest release published at: $published_at (UTC)"
echo "Time difference: $time_difference hours"
echo "SHOULD_BUILD=$(( time_difference < 24 ))" >> "$GITHUB_OUTPUT"
if [ "$time_difference" -lt 24 ]; then
echo "SHOULD_BUILD=true" >> $GITHUB_OUTPUT
else
echo "SHOULD_BUILD=false" >> $GITHUB_OUTPUT
fi
- name: Check Manual Trigger
id: check_manual_trigger
if: github.event_name == 'workflow_dispatch'
id: check_manual_trigger
run: |
echo "Manual trigger detected. Forcing build."
echo "SHOULD_BUILD=true" >> "$GITHUB_OUTPUT"
echo "SHOULD_BUILD=true" >> $GITHUB_OUTPUT
- name: Checkout Repository
if: steps.check_release_time.outputs.SHOULD_BUILD == true || steps.check_manual_trigger.outputs.should_build == true
if: steps.check_release_time.outputs.SHOULD_BUILD == 'true' || steps.check_manual_trigger.outputs.SHOULD_BUILD == 'true'
uses: actions/checkout@v3

# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
if: steps.check_release_time.outputs.SHOULD_BUILD == true || steps.check_manual_trigger.outputs.should_build == true
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
if: steps.check_release_time.outputs.SHOULD_BUILD == 'true' || steps.check_manual_trigger.outputs.SHOULD_BUILD == 'true'
uses: docker/setup-buildx-action@v3

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log Into Registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request' && (steps.check_release_time.outputs.SHOULD_BUILD == true || steps.check_manual_trigger.outputs.should_build == true)
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
if: github.event_name != 'pull_request' && (steps.check_release_time.outputs.SHOULD_BUILD == 'true' || steps.check_manual_trigger.outputs.SHOULD_BUILD == 'true')
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker Metadata
if: steps.check_release_time.outputs.SHOULD_BUILD == true || steps.check_manual_trigger.outputs.should_build == true
if: steps.check_release_time.outputs.SHOULD_BUILD == 'true' || steps.check_manual_trigger.outputs.SHOULD_BUILD == 'true'
id: meta
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker Image
if: steps.check_release_time.outputs.SHOULD_BUILD == true || steps.check_manual_trigger.outputs.should_build == true
if: steps.check_release_time.outputs.SHOULD_BUILD == 'true' || steps.check_manual_trigger.outputs.SHOULD_BUILD == 'true'
id: build-and-push
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
Expand Down

0 comments on commit 761cb5c

Please sign in to comment.