Files

64 lines
2.2 KiB
YAML

name: Publish Docker Image to Docker Hub
on:
push:
branches:
- 'main' # Or your default branch, e.g., master
tags:
- 'v*.*.*' # Push on version tags like v1.0.0
pull_request: # Also build on PRs to validate Dockerfile, but don't push
branches:
- 'main'
jobs:
docker_build_and_push:
runs-on: ubuntu-latest
permissions:
contents: read # Required to checkout the repository
# packages: write # Only needed if pushing to GitHub Packages (GHCR)
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
# Optional: For multi-platform builds (e.g., linux/arm64).
# If you only need linux/amd64, this can be removed, but it's good practice.
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
if: github.event_name != 'pull_request' # Don't log in for PRs
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ secrets.DOCKERHUB_USERNAME }}/rust-socksd
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha,format=short
flavor: |
latest=auto # Automatically tags 'latest' for default branch and semver tags
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: ${{ github.event_name != 'pull_request' }} # Only push if not a PR
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64 # Add other platforms like linux/arm64 if needed (QEMU is then required)
cache-from: type=gha # Enable GitHub Actions cache for Docker layers
cache-to: type=gha,mode=max # Enable writing to GitHub Actions cache