Skip to content

issue #63: build and push docker container to github registry #2

issue #63: build and push docker container to github registry

issue #63: build and push docker container to github registry #2

name: Reusable workflow to build and push docker container to github 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: 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: Build and push (latest)
id: docker_build_latest
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 }}
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_latest.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 }}