Skip to content

Commit

Permalink
fix: refactor image creation name processing
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Buchleitner <[email protected]>
  • Loading branch information
mabunixda committed Dec 6, 2024
1 parent 8ea2535 commit 4f75e23
Showing 1 changed file with 47 additions and 32 deletions.
79 changes: 47 additions & 32 deletions .github/workflows/container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ on:
required: false
type: string
default: ''
image_repo:
description: 'The repository of the image'
required: false
type: string
default: ${{ github.repository_owner }}

build_args:
description: 'The build arguments for the image'
required: false
Expand All @@ -56,6 +52,8 @@ on:

env:
MONDOO_CONFIG_BASE64: ${{ secrets.MONDOO_SERVICE_ACCOUNT }}
QUAY_USER: ${{ secrets.QUAY_USER }}
DOCKER_USER: ${{ secrets.DOCKER_USER }}

jobs:

Expand All @@ -81,41 +79,49 @@ jobs:
id: get_dir
run: |
echo "docker_directory=$(dirname ${{ inputs.dockerfile }})" >> $GITHUB_OUTPUT
env
- name: Prepare tagging
id: prep
run: |
IMAGE_NAME=$(basename ${{ github.repository }})
IMAGE_REPO=${{ inputs.image_repo }}
name="$(basename ${{ github.repository }})"
if [[ -n "${{ inputs.image_name }}" ]]; then
IMAGE_NAME="${{ inputs.image_name }}"
fi
VERSION="dev"
if [[ '${{ needs.pre-commit.outputs.version }}' != '' ]]; then
VERSION="${{ needs.pre-commit.outputs.version }}"
tmp_name="$( basename '${{ inputs.image_name }}')"
fi
if [ "${{ github.event_name }}" = "schedule" ]; then
VERSION="nightly"
repo=${{ github.repository_owner }}
if [[ -n "${{ inputs.image_name }}" ]] && [[ "${name}" != "${{ inputs.image_name}}" ]]; then
repo="$( dirname '${{ inputs.image_name }}')"
fi
GHCR_IMAGE="ghcr.io/${IMAGE_NAME}"
TAGS="${GHCR_IMAGE}:${VERSION}"
IMAGE_NAME="${repo}/${name}
if [[ -n "${{ secrets.QUAY_USER }}" ]]; then
QUAY_IMAGE="quay.io/${IMAGE_REPO}/${IMAGE_NAME}"
tags="${TAGS}:${QUAY_IMAGE}:${VERSION}"
fi
IMAGES=<<EOT
name=ghcr.io/${IMAGE_NAME},enable=true
name=quay.io/${IMAGE_NAME},enable=$( [[ -n "${{env.QUAY_USER}}" ]] && echo "true" || echo "false" )
name=docker.io/${IMAGE_NAME},enable=$( [[ -n "${{env.DOCKER_USER}}" ]] && echo "true" || echo "false" )
EOT
if [[ $VERSION =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
TAGS="$TAGS,${GHCR_IMAGE}:latest"
if [[ -n "${{ secrets.QUAY_USER }}" ]]; then
TAGS="$TAGS,${QUAY_IMAGE}:latest"
fi
fi
TAGS=<<EOT
type=semver,pattern={{version}},value=${{ needs.pre-commit.outputs.version }}
type=schedule,pattern=nightly
type=ref,enable=true,priority=600,prefix=pr-,suffix=,event=pr
EOT
echo "setting images ${IMAGES}"
echo "settings tag ${TAGS}"
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
echo "ghcr_tag=${GHCR_IMAGE}:${VERSION}" >> $GITHUB_OUTPUT
echo "quay_user=${{ secrets.QUAY_USER }}" >> $GITHUB_OUTPUT
echo "images=${IMAGES}" >> $GITHUB_OUTPUT
- name: Extract metadata (tags, labels) for Docker 👀
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ steps.prep.outputs.images }}
tags: |
${{ steps.prep.outputs.tags }}
- name: foo
run : |
echo "${{ steps.prep.outputs.json }}"
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand All @@ -128,20 +134,29 @@ jobs:
uses: docker/build-push-action@v5
with:
load: true
push: false
context: ${{ steps.get_dir.outputs.docker_directory }}
file: ${{ inputs.dockerfile }}
tags: ${{ steps.prep.outputs.tags }}
tags: ${{ steps.meta.outputs.tags }}
build-args: ${{ inputs.build_args }}

- name: Scan Docker Image
if: env.MONDOO_CONFIG_BASE64 != ''
uses: mondoohq/actions/[email protected]
with:
image: ${{ steps.prep.outputs.ghcr_tag }}
image: ${{ steps.meta.outputs.ghcr_tag }}
score-threshold: ${{ inputs.score }}

- name: Login to Dockerhub
if: needs.pre-commit.outputs.version != '' && env.DOCKER_USER != ''
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Login to Quay
if: needs.pre-commit.outputs.version != '' && steps.prep.outputs.quay_user != ''
if: needs.pre-commit.outputs.version != '' && env.QUAY_USER != ''
uses: docker/login-action@v3
with:
registry: quay.io
Expand Down

0 comments on commit 4f75e23

Please sign in to comment.