Skip to content

Instantly share code, notes, and snippets.

@justedlev
Created October 4, 2024 08:05
Show Gist options
  • Save justedlev/c7226814239518f96dc4dc8f672cd1e1 to your computer and use it in GitHub Desktop.
Save justedlev/c7226814239518f96dc4dc8f672cd1e1 to your computer and use it in GitHub Desktop.
Bild and Push to Docker Hub with GitHub Actions
name: Publish Docker image
on:
release:
types: [ published ]
tags: [ "v*.*.*" ]
workflow_dispatch:
env:
REGISTRY: docker.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
name: Build the Docker image
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build the Docker image
run: docker compose build --no-cache --force-rm
test:
name: Test Docker image
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test Docker image
run: docker compose up -d
push_to_registry:
name: Push Docker image to Docker Hub
needs: [ build, test ]
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract metadata (tags, labels) for Docker
id: metadata
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=semver,pattern={{version}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: "{{defaultContext}}"
push: true
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.metadata.outputs.labels }}
@justedlev
Copy link
Author

justedlev commented Oct 4, 2024

📋 About

In order to automate the routine work of building and pushing a new version of the docker image,
I used a github action that will do this for me with each new release that I created.

⚙️ Configuration

First we need to configure our github repo, more details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment