Skip to content

Build API

Build API #6

Workflow file for this run

name: Build API
on:
workflow_dispatch:
push:
branches:
- "main"
paths:
- "api/**"
env:
REGISTRY: ghcr.io
IMAGE_NAME: api
DOCKERFILE_CONTEXT: "api" # Build's context is the set of files located in the specified PATH or URL (default Git context)
DOCKERFILE_TARGET: "prod" # Sets the target stage to build
DOCKERFILE_FILE: "Dockerfile" # Path to the Dockerfile. (default {context}/Dockerfile)
jobs:
build-and-push:
name: Build and publish image api to ghcr.io
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
## Authenticate to registry repo github
##
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
## Build and push Docker image
##
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ github.repository }}/${{ env.IMAGE_NAME }}
tags: type=sha
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ${{ env.DOCKERFILE_CONTEXT }}
file: ${{ env.DOCKERFILE_CONTEXT }}/${{ env.DOCKERFILE_FILE }}
target: ${{ env.DOCKERFILE_TARGET }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ github.repository }}/${{ env.IMAGE_NAME }}:buildcache
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ github.repository }}/${{ env.IMAGE_NAME }}:buildcache,mode=max