CD - Build and push Docker images #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CD - Build and push Docker images | |
on: [workflow_dispatch] | |
jobs: | |
build-and-push-image: | |
strategy: | |
matrix: | |
directory: [backend, frontend] | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
env: | |
REGISTRY: ghcr.io | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Save date for docker image tagging | |
id: date | |
run: echo "::set-output name=date::$(date +'%Y%m%d%H%M%S')" | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v4.6.0 | |
with: | |
images: ${{ env.REGISTRY }}/${{ github.repository }}/${{ matrix.directory }} | |
flavor: | | |
latest=true | |
tags: | | |
${{ steps.date.outputs.date }} | |
- name: Build and push production image | |
if: matrix.directory == 'backend' | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
builder: ${{ steps.buildx.outputs.name }} | |
file: ./backend/Dockerfile | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
- name: Build and push production image | |
if: matrix.directory == 'frontend' | |
uses: docker/build-push-action@v5 | |
with: | |
context: ./frontend | |
builder: ${{ steps.buildx.outputs.name }} | |
file: ./frontend/Dockerfile | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max |