Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 83 additions & 39 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,65 +1,109 @@
name: Build and Push to Docker Hub
name: Build and Push Docker Image

# Trigger the workflow on push to main branch or when creating a tag (release)
on:
push:
branches:
- main
- master
- dev
paths:
- 'Dockerfile'
- 'go.mod'
- 'go.sum'
- 'main.go'
- 'Makefile'
- '.github/workflows/**'
- 'api/**'
- 'config/**'
- 'database/**'
- 'models/**'
- 'services/**'
- 'ui/**'
- '.env.example'
- '.gitignore'
- 'LICENSE'
- 'README.md'
- 'docker-compose.yml'
tags:
- 'v*'
workflow_dispatch: # Allows manual trigger from GitHub UI
workflow_dispatch:

env:
DOCKERHUB_IMAGE_NAME: hhftechnology/middleware-manager

jobs:
build-and-push:
name: Build and Push Docker image
runs-on: ubuntu-latest

steps:
- name: Check out the repo
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for proper versioning

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23.x' # Match your go.mod version
cache: true

- name: Verify dependencies
run: go mod verify


- name: Debug trigger
run: |
echo "Event: ${{ github.event_name }}"
echo "Ref: ${{ github.ref }}"
echo "Ref name: ${{ github.ref_name }}"
echo "Branch: ${{ github.ref_type == 'branch' && github.ref_name || 'not a branch' }}"

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

# Get current date for image tags
- name: Get current date
id: date
run: echo "date=$(date +'%Y%m%d')" >> $GITHUB_OUTPUT

- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ secrets.DOCKERHUB_USERNAME }}/middleware-manager
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=ref,event=branch
type=sha,format=short
latest

- name: Build and push Docker image
# Prepare tags based on branch and version
- name: Prepare Docker tags
id: docker_tags
run: |
TAGS=""

# Add branch-specific tags
if [[ "${{ github.ref_name }}" == "main" || "${{ github.ref_name }}" == "master" ]]; then
# For main/master branch, add latest tag
TAGS="$TAGS ${{ env.DOCKERHUB_IMAGE_NAME }}:latest"
elif [[ "${{ github.ref_name }}" == "dev" ]]; then
# For dev branch
TAGS="$TAGS ${{ env.DOCKERHUB_IMAGE_NAME }}:dev"
elif [[ "${{ github.ref_type }}" == "branch" ]]; then
# For other branches
TAGS="$TAGS ${{ env.DOCKERHUB_IMAGE_NAME }}:${{ github.ref_name }}"
fi

# Add sha tag for all branches
if [[ "${{ github.ref_type }}" == "branch" ]]; then
TAGS="$TAGS,${{ env.DOCKERHUB_IMAGE_NAME }}:sha-${GITHUB_SHA::7}"
fi

# Add version tag for tagged releases
if [[ "${{ github.ref_type }}" == "tag" && "${{ github.ref }}" == refs/tags/v* ]]; then
VERSION="${{ github.ref_name }}"

# Add full version tag
TAGS="$TAGS,${{ env.DOCKERHUB_IMAGE_NAME }}:$VERSION"
fi

# Add date tag for all builds
TAGS="$TAGS,${{ env.DOCKERHUB_IMAGE_NAME }}:${{ steps.date.outputs.date }}"

# Remove leading space or comma if present
TAGS=$(echo "$TAGS" | sed 's/^[ ,]*//')

echo "tags=$TAGS" >> $GITHUB_OUTPUT
echo "Docker tags: $TAGS"

- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile # Points to your Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
tags: ${{ steps.docker_tags.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
cache-to: type=gha,mode=max
Loading