Skip to content

Add workflow to build docker image on github #5

Add workflow to build docker image on github

Add workflow to build docker image on github #5

name: Container Build and Push
on:
push:
branches:
- "v2c"
pull_request:
branches:
- "v2c"
workflow_dispatch:
env:
GITHUB_REGISTRY: ghcr.io
DOCKERHUB_REGISTRY: docker.io
DOCKERHUB_IMAGE: chairemobilite/${{ github.event.repository.name }}
GITHUB_IMAGE: chairemobilite/trRouting
jobs:
# PR validation job - builds and pushes to GitHub registry only
pr-validation:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
actions: write
security-events: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Generate Docker tags for PR
id: pr-meta
uses: docker/metadata-action@v5
with:
images: ${{ env.GITHUB_REGISTRY }}/${{ env.GITHUB_IMAGE }}
tags: |
type=ref,event=pr
- name: Build and push PR image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.pr-meta.outputs.tags }}
labels: ${{ steps.pr-meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
# Branch push job - builds and pushes to both registries
branch-push:
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Generate Docker tags for branch
id: branch-meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.DOCKERHUB_REGISTRY }}/${{ env.DOCKERHUB_IMAGE }}
${{ env.GITHUB_REGISTRY }}/${{ env.GITHUB_IMAGE }}
tags: |
# Tag as latest since v2c is the main branch
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/v2c' }}
# Include the branch name as a tag
type=ref,event=branch
- name: Build and push branch images
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.branch-meta.outputs.tags }}
labels: ${{ steps.branch-meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max