Skip to content

Commit

Permalink
Create docker-build.yml (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbristow authored Nov 22, 2024
1 parent 3467444 commit 23fab0b
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Docker Build and Push

on:
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy to'
required: true
type: choice
options:
- dev
- staging
tag_suffix:
description: 'Additional tag suffix (optional)'
required: false
type: string

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Generate tags
id: tags
run: |
BRANCH_NAME=${GITHUB_REF#refs/heads/}
SHA_SHORT=$(git rev-parse --short HEAD)
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
# Base tags
TAGS="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.event.inputs.environment }}"
TAGS="${TAGS},${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${BRANCH_NAME}-${SHA_SHORT}"
TAGS="${TAGS},${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${BRANCH_NAME}-${{ github.event.inputs.environment }}"
# Add timestamp tag
TAGS="${TAGS},${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${BRANCH_NAME}-${TIMESTAMP}"
# Add custom suffix if provided
if [ -n "${{ github.event.inputs.tag_suffix }}" ]; then
TAGS="${TAGS},${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.event.inputs.environment }}-${{ github.event.inputs.tag_suffix }}"
fi
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile.prod
push: true
tags: ${{ steps.tags.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Print image tags
run: |
echo "Image was built and pushed with the following tags:"
echo "${{ steps.tags.outputs.tags }}" | tr ',' '\n'

0 comments on commit 23fab0b

Please sign in to comment.