Update docker-publish.yml #181
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 | |
# 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 "::set-output name=latest_version::$latest_version" | |
echo "::set-output name=should_build::$(( time_difference < 24 ))" | |
- name: Check Manual Trigger | |
if: github.event_name == 'workflow_dispatch' | |
run: | | |
echo "Manual trigger detected. Forcing build." | |
echo "::set-output name=should_build::1" | |
- name: Checkout Repository | |
if: steps.check_release_time.outputs.should_build == '1' | |
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 == '1' | |
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 | |
# 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 == '1' | |
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 | |
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 == '1' | |
id: meta | |
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 | |
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 == '1' | |
id: build-and-push | |
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 | |
with: | |
context: . | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max |