Skip to content

Build and Push Docker Image #9

Build and Push Docker Image

Build and Push Docker Image #9

name: Build and Push Docker Image
on:
push:
tags: # Trigger the workflow only when a tag is pushed
- '*' # Matches all tags (e.g., v1.0.0, 1.0.0, etc.)
workflow_dispatch: # Allows the workflow to be triggered manually
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
# Checkout the repository code
- name: Checkout code
uses: actions/checkout@v4
# Set the image tag based on the trigger type
- name: Set Image Tag
id: vars
run: |
if [ "${{ github.event_name }}" == "push" ]; then
# For Git tag push, use the tag name
echo "TAG=${GITHUB_REF##*/}" >> $GITHUB_ENV
else
# For manual trigger, use the commit SHA as a suffix
SHORT_SHA=$(git rev-parse --short HEAD)
echo "TAG=manual-${SHORT_SHA}" >> $GITHUB_ENV
fi
# Log in to Docker Hub
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Build the Docker image with the Git tag as version
- name: Build Base Docker image
run: |
docker build -t eckii24/dev-base:${{ env.TAG }} -t eckii24/dev-base:latest -f ./.config/setup-scripts/base.Dockerfile .
docker build -t eckii24/dev-dotnet:${{ env.TAG }} -t eckii24/dev-dotnet:latest -f ./.config/setup-scripts/dotnet.Dockerfile .
docker build -t eckii24/dev-php:${{ env.TAG }} -t eckii24/dev-php:latest -f ./.config/setup-scripts/php.Dockerfile .
# Push the Docker image to Docker Hub
- name: Push Base Docker image
run: |
docker push eckii24/dev-base:${{ env.TAG }}
docker push eckii24/dev-base:latest
docker push eckii24/dev-dotnet:${{ env.TAG }}
docker push eckii24/dev-dotnet:latest
docker push eckii24/dev-php:${{ env.TAG }}
docker push eckii24/dev-php:latest