Skip to content

Docker

Docker #247

name: Docker
on:
schedule:
- cron: '20 12 * * *'
push:
branches: [ "main" ]
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "main" ]
workflow_dispatch:
env:
REGISTRY: docker.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Check Latest Release
if: github.event_name != 'workflow_dispatch'
id: check_release_time
run: |
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')
published_at_seconds=$(date -d "$published_at" +%s)
current_time=$(date +%s)
time_difference=$(( (current_time - published_at_seconds) / 3600 ))
if [ "$time_difference" -lt 24 ]; then
echo "SHOULD_BUILD=true" >> $GITHUB_OUTPUT
else
echo "SHOULD_BUILD=false" >> $GITHUB_OUTPUT
fi
- name: Check Manual Trigger
if: github.event_name == 'workflow_dispatch'
id: check_manual_trigger
run: |
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'
uses: actions/checkout@v3
- 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@v3
- 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@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract Docker Metadata
if: steps.check_release_time.outputs.SHOULD_BUILD == 'true' || steps.check_manual_trigger.outputs.SHOULD_BUILD == 'true'
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker Image
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@v5
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