Build and Push to Docker Hub #485
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/mycorrhiza | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Fetch the latest Mycorrhiza release tag | |
id: mycorrhiza_release | |
run: | | |
LATEST_RELEASE=$(curl -s "https://api.github.com/repos/bouncepaw/mycorrhiza/tags" | jq -r '.[0].name') | |
echo "Latest release tag is $LATEST_RELEASE" | |
echo "latest_release=$LATEST_RELEASE" >> $GITHUB_OUTPUT | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Check if the tag exists on Docker Hub | |
id: dockerhub_check | |
run: | | |
TAG=${{ steps.mycorrhiza_release.outputs.latest_release }} | |
TAG_EXISTS=$(curl -s -f "https://hub.docker.com/v2/repositories/${DOCKERHUB_REPO}/tags/${TAG}/" > /dev/null && echo "true" || echo "false") | |
echo "tag_exists=$TAG_EXISTS" >> $GITHUB_OUTPUT | |
if [ "$TAG_EXISTS" == "true" ]; then | |
echo "Tag $TAG exists on Docker Hub. Exiting workflow." | |
else | |
echo "Tag $TAG does not exist on Docker Hub. Proceeding with build." | |
fi | |
- name: Check out mycorrhiza repository at the latest tag | |
if: steps.dockerhub_check.outputs.tag_exists != 'true' | |
uses: actions/checkout@v4 | |
with: | |
repository: bouncepaw/mycorrhiza | |
ref: ${{ steps.mycorrhiza_release.outputs.latest_release }} | |
path: "mycorrhiza" | |
- name: Copy our Dockerfile into mycorrhiza context | |
if: steps.dockerhub_check.outputs.tag_exists != 'true' | |
run: cp Dockerfile mycorrhiza/Dockerfile | |
- name: Set up Docker Buildx | |
if: steps.dockerhub_check.outputs.tag_exists != 'true' | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to Docker Hub | |
if: steps.dockerhub_check.outputs.tag_exists != 'true' | |
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 != 'true' | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./mycorrhiza | |
file: ./mycorrhiza/Dockerfile | |
push: true | |
tags: | | |
${{ env.DOCKERHUB_REPO }}:${{ steps.mycorrhiza_release.outputs.latest_release }} | |
${{ env.DOCKERHUB_REPO }}:latest | |
platforms: linux/amd64,linux/arm64 |