Build and Push to Docker Hub #588
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 to Docker Hub | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- main | |
schedule: | |
- cron: '0 */6 * * *' | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
build_and_push: | |
runs-on: ubuntu-latest | |
env: | |
DOCKERHUB_REPO: jrasanen/bookwyrm | |
steps: | |
- name: Fetch the latest Bookwyrm tag | |
id: bookwyrm_tag | |
run: | | |
LATEST_TAG=$(curl -s "https://api.github.com/repos/bookwyrm-social/bookwyrm/tags" | jq -r '.[0].name') | |
echo "Latest tag is $LATEST_TAG" | |
echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT | |
- name: Check if the tag exists on Docker Hub | |
id: dockerhub_check | |
run: | | |
TAG=${{ steps.bookwyrm_tag.outputs.latest_tag }} | |
TAG_EXISTS=$(curl -s -f "https://hub.docker.com/v2/repositories/${DOCKERHUB_REPO}/tags/${TAG}/" > /dev/null && echo "0" || echo "1") | |
echo "tag_exists=$TAG_EXISTS" >> $GITHUB_OUTPUT | |
if [ "$TAG_EXISTS" == "0" ]; then | |
echo "Tag $TAG exists on Docker Hub. Exiting workflow." | |
exit 0 | |
fi | |
- name: Check out your repository | |
if: steps.dockerhub_check.outputs.tag_exists != '0' | |
uses: actions/checkout@v4 | |
- name: Check out Bookwyrm repository at the latest tag | |
if: steps.dockerhub_check.outputs.tag_exists != '0' | |
uses: actions/checkout@v4 | |
with: | |
repository: bookwyrm-social/bookwyrm | |
ref: ${{ steps.bookwyrm_tag.outputs.latest_tag }} | |
path: "bookwyrm" | |
- name: Copy our Dockerfile into Bookwyrm context | |
if: steps.dockerhub_check.outputs.tag_exists != '0' | |
run: cp Dockerfile bookwyrm/Dockerfile | |
- name: Copy our settings.py to bookwyrm/settings.py | |
if: steps.dockerhub_check.outputs.tag_exists != '0' | |
run: cp settings.py bookwyrm/bookwyrm/settings.py | |
- name: Set up Docker Buildx | |
if: steps.dockerhub_check.outputs.tag_exists != '0' | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to Docker Hub | |
if: steps.dockerhub_check.outputs.tag_exists != '0' | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Build and Push | |
if: steps.dockerhub_check.outputs.tag_exists != '0' | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./bookwyrm | |
file: ./bookwyrm/Dockerfile | |
push: true | |
tags: | | |
${{ env.DOCKERHUB_REPO }}:${{ steps.bookwyrm_tag.outputs.latest_tag }} | |
${{ env.DOCKERHUB_REPO }}:latest | |
platforms: linux/amd64,linux/arm64 |