issue #63: build and push docker container to github registry #6
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
name: Reusable workflow to build and push docker container to github container registry | |
on: | |
workflow_call: | |
inputs: | |
container-name: | |
required: true | |
type: string | |
tag: | |
required: true | |
type: string | |
registry: | |
required: true | |
type: string | |
pull_request: | |
types: | |
- opened | |
- closed | |
- synchronize | |
jobs: | |
build-push-image: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check Out Repo | |
uses: actions/checkout@v4 | |
- name: Check if a Dockerfile is present at the root of the repo | |
id: check-file | |
run: | | |
if [ -f Dockerfile ]; then | |
echo "::set-output name=exists::true" | |
else | |
echo "::set-output name=exists::false" | |
fi | |
- name: Cancel the workflow if no Dockerfile is present | |
if: steps.check-file.outputs.exists == 'false' | |
uses: andymckay/cancel-action@0.2 | |
# needed since the run is not cancelled immediately | |
- name: Wait for run cancellation | |
if: steps.check-file.outputs.exists == 'false' | |
shell: bash | |
run: | | |
echo "No Dockerfile found, cancelling the workflow." | |
while true; do echo "Waiting for job to be cancelled"; sleep 5; done | |
- name: Set up Docker Buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to the github container registry (GCR) | |
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 | |
with: | |
registry: ${{ inputs.registry }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Cache Docker layers | |
uses: actions/cache@v3 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ github.sha }} | |
restore-keys: ${{ runner.os }}-buildx | |
- name: Extract branch name | |
shell: bash | |
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT | |
id: extract_branch_name | |
- name: Build and push (3 tags | commit number, pr number, branch name) | |
id: docker_build_and_tag | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
push: true | |
tags: | | |
${{ inputs.registry }}/${{ inputs.container-name }}:${{ github.event.number }} | |
${{ inputs.registry }}/${{ inputs.container-name }}:${{ inputs.tag }} | |
${{ inputs.registry }}/${{ inputs.container-name }}:${{ steps.extract_branch_name.outputs.branch }} | |
cache-from: type=local,src=/tmp/.buildx-cache | |
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new | |
- name: Refresh Cache | |
run: | | |
rm -rf /tmp/.buildx-cache | |
mv /tmp/.buildx-cache-new /tmp/.buildx-cache | |
- name: Image digest (latest) | |
run: echo ${{ steps.docker_build_and_tag.outputs.digest }} | |
remove-old-image: | |
runs-on: ubuntu-latest | |
needs: build-push-image | |
steps: | |
- name: Check Out Repo | |
uses: actions/checkout@v4 | |
- name: Set up Docker | |
uses: docker/setup-buildx-action@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v3 | |
with: | |
python-version: 3.8 | |
- name: Install Python dependencies | |
run: pip install requests | |
- name: Run Python Script | |
run: python scripts/remove-old-image.py | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
REGISTRY: ${{ inputs.registry }} | |
CONTAINER_NAME: ${{ inputs.container-name }} | |
PR_TAG: ${{ github.event.number }} | |
USER: ${{ github.actor }} | |
CURRENT_COMMIT: ${{ inputs.tag }} | |