name: Build and Push Docker Image
on:
push:
branches: [main]
tags: ['v*']
paths:
- 'src/**'
- 'Cargo.toml'
- 'Cargo.lock'
- 'Dockerfile'
- '.github/workflows/docker-build.yml'
pull_request:
branches: [main]
paths:
- 'src/**'
- 'Cargo.toml'
- 'Cargo.lock'
- 'Dockerfile'
- '.github/workflows/docker-build.yml'
env:
REGISTRY: ghcr.io
jobs:
build:
name: Build and Push
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/phalus-sh/phalus
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
load: ${{ github.event_name == 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=phalus
cache-to: type=gha,mode=max,scope=phalus
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@v3.4.0
with:
cosign-release: 'v2.2.3'
- name: Sign image with cosign
if: github.event_name != 'pull_request'
run: |
images="${{ steps.meta.outputs.tags }}"
for image in ${images//,/ }; do
echo "Signing image: $image"
cosign sign --yes "$image"
done
env:
COSIGN_EXPERIMENTAL: "1"