Skip to content

Dockerfile: Build and Push Manually #41

Dockerfile: Build and Push Manually

Dockerfile: Build and Push Manually #41

name: "Dockerfile: Build and Push 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 [[ -z "${{ github.event.inputs.full-name }}" ]]; then
echo "Dockerfile name cannot be empty."
exit 1
fi
if [[ -z "${{ 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: .
file: ${{ github.event.inputs.full-name }}
push: true
tags: "${{ secrets.DOCKER_HUB_ID }}/${{ env.file-name }}:${{ env.tag-name }}"