-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8f7a6a
commit 9761962
Showing
1 changed file
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
# build-push-docker-image.yml: builds a Docker container image and pushes it | ||
# to the Agent Artificial repository in GitHub Container Registry (GHCR) | ||
--- | ||
on: | ||
workflow_call: | ||
inputs: | ||
aws_region: | ||
required: false | ||
type: string | ||
default: us-west-1 | ||
description: AWS region for CLI commands (such as ECR login) | ||
build_args: | ||
required: false | ||
type: string | ||
default: "" | ||
description: newline-delimited list of Docker build args (as "key=value") | ||
context_dir: | ||
required: false | ||
type: string | ||
default: "." | ||
description: context directory which is passed to docker build | ||
docker_file: | ||
required: false | ||
type: string | ||
default: Dockerfile | ||
description: override Dockerfile name | ||
image_prefix: | ||
required: false | ||
type: string | ||
default: "" | ||
description: prefix for image name | ||
image_registry: | ||
required: false | ||
type: string | ||
default: ghcr.io/${{ github.repository_owner }} | ||
description: Docker/OCI registry to host the image (hostname and optional path) | ||
image_tag: | ||
required: true | ||
type: string | ||
description: image tag (e.g. build date, commit, version, variant) | ||
microservice: | ||
required: true | ||
type: string | ||
description: name of the application | ||
microservice_env: | ||
required: true | ||
type: string | ||
description: name of the environment (e.g. dev, test, prod) | ||
microservice_path: | ||
required: true | ||
type: string | ||
description: path within the caller's Git repo which contains the Dockerfile | ||
push: | ||
required: false | ||
type: boolean | ||
default: true | ||
description: whether to push the built image to GHCR (true) or simply build it (false) | ||
branch: | ||
required: false | ||
type: string | ||
default: main | ||
description: the branch deploy | ||
outputs: | ||
image_name: | ||
value: ${{ jobs.build_push_docker_image.outputs.image_name }} | ||
description: the full name of the image, including registry but excluding tag | ||
# secrets: | ||
# aws_access_key: | ||
# required: false | ||
# description: AWS access key ID for CLI commands | ||
# aws_secret_key: | ||
# required: false | ||
# description: AWS secret access key for CLI commands | ||
|
||
jobs: | ||
Debug: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Debug information | ||
run: | | ||
echo "=== Variables ===" | ||
echo "Variable: ${{ inputs.build_args }}" | ||
echo "Variable: ${{ inputs.context_dir }}" | ||
echo "Variable: ${{ inputs.docker_file }}" | ||
echo "Variable: ${{ inputs.image_prefix }}" | ||
echo "Variable: ${{ inputs.microservice }}" | ||
echo "Variable: ${{ inputs.microservice_env }}" | ||
build_push_docker_image: | ||
name: Build and Push Docker Image | ||
runs-on: ubuntu-latest | ||
env: | ||
build_args: ${{ inputs.build_args }} | ||
context_dir: ${{ inputs.context_dir }} | ||
docker_file: ${{ inputs.docker_file }} | ||
#image_name: ${{ inputs.image_registry }}/${{ inputs.image_prefix }}${{ inputs.microservice }}-${{ inputs.microservice_env }} | ||
image_tag: ${{ inputs.image_tag }} | ||
microservice: ${{ inputs.microservice }} | ||
microservice_path: ${{ inputs.microservice_path }} | ||
microservice_env: ${{ inputs.microservice_env }} | ||
push: ${{ inputs.push }} | ||
outputs: | ||
image_name: ${{ env.image_name }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ inputs.branch }} | ||
#submodules: 'true' | ||
|
||
- name: Set registry_host and convert image_name to lower | ||
env: | ||
image_registry: ${{ inputs.image_registry }} | ||
image_prefix: ${{inputs.image_prefix}} | ||
microservice: ${{inputs.microservice}} | ||
microservice_env: ${{inputs.microservice_env}} | ||
run: | | ||
echo "Grab just the docker registry host" | ||
registry_host="${image_registry%%/*}" | ||
echo "registry_host=${registry_host}" | tee -a $GITHUB_ENV | ||
echo "Converting the GitHub org to lower case (required for GHCR)" | ||
#echo "image_registry=$(echo $image_registry | tr '[:upper:]' '[:lower:]')" | tee -a $GITHUB_ENV | ||
echo "image_name=$(echo $image_registry | tr '[:upper:]' '[:lower:]')/${image_prefix}${microservice}-${microservice_env}" | tee -a $GITHUB_ENV | ||
- name: debug | ||
run: | | ||
echo "=== Variables ===" | ||
echo registry_host: ${{env.registry_host}} | ||
echo image_name: ${{env.image_name}} | ||
echo image_tag: ${{env.image_tag}} | ||
echo complete image name: ${{ env.image_name }}:${{ env.image_tag }} | ||
- name: Install Docker via Buildx | ||
id: buildx | ||
uses: docker/setup-buildx-action@v2 | ||
with: | ||
buildkitd-flags: --debug | ||
#driver-opts: image=moby/buildkit:v0.10.6 | ||
|
||
# - name: Login to ECR | ||
# if: inputs.push && contains(env.registry_host, '.dkr.ecr.') | ||
# uses: docker/login-action@v2 | ||
# with: | ||
# registry: ${{ env.registry_host }} | ||
# username: ${{ secrets.aws_access_key }} | ||
# password: ${{ secrets.aws_secret_key }} | ||
|
||
- name: Login to GHCR | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Build image and push to registry | ||
uses: docker/build-push-action@v5 | ||
with: | ||
build-args: ${{ env.build_args }} | ||
context: ${{ env.context_dir }} | ||
file: ${{ env.microservice_path }}//${{ env.docker_file }} | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
no-cache: true | ||
pull: true | ||
push: ${{ env.push }} | ||
tags: "${{ env.image_name }}:latest,${{ env.image_name }}:${{ env.image_tag }}" | ||
|