Skip to content

Dockerfile: Build and Push Manually #32

Dockerfile: Build and Push Manually

Dockerfile: Build and Push Manually #32

name: Build and Push Dockerfiles manually
on:
workflow_dispatch:
inputs:
full-name:
type: string
description: "Dockerfile name with full path (e.g. 'this/folder/image/name.Dockerfile')"
required: true
tag:
type: string
description: "Docker image tag (e.g. 'latest', '1.0.0', 'pr-123'). 'manual-' will be prepended to the tag if it is not 'latest'."
required: true
jobs:
verify-arguments:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Assert that arguments are valid
run: |
if [[ "${{ github.event.inputs.full-name }}" == "" ]]; then
echo "Dockerfile name cannot be empty."
exit 1
fi
if [[ "${{ github.event.inputs.tag }}" == "" ]]; then
echo "Tag cannot be empty."
exit 1
fi
if [[ ! -f "${{ github.event.inputs.full-name }}" ]]; then
echo "Dockerfile '${{ github.event.inputs.full-name }}' does not exist."
exit 1
fi
build-and-push:
runs-on: ubuntu-latest
needs: verify-arguments
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_ID }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Set environment variables for Dockerfile name, directory and tag
id: set-env-vars
run: |
echo "file-name=$(basename '${{ github.event.inputs.full-name }}' .Dockerfile)" >> $GITHUB_ENV
echo "dir-name=$(dirname '${{ github.event.inputs.full-name }}')" >> $GITHUB_ENV
if [[ "${{ github.event.inputs.tag }}" == "latest" ]]; then
echo "tag-name=latest" >> $GITHUB_ENV
else
echo "tag-name=manual-${{ github.event.inputs.tag }}" >> $GITHUB_ENV
fi
- name: Build and Push Dockerfile
uses: docker/build-push-action@v3
with:
context: ${{ env.dir-name }}
file: ${{ github.event.inputs.full-name }}
push: true
tags: |
${{ secrets.DOCKER_HUB_ID }}/${{ env.file-name }}:${{ env.tag-name }}