Skip to content

HDDS-11632. Publish images to GitHub container registry #102

HDDS-11632. Publish images to GitHub container registry

HDDS-11632. Publish images to GitHub container registry #102

Workflow file for this run

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: build
on:
pull_request:
types: [opened, ready_for_review, synchronize]
push:
env:
IMAGE_NAME: ozone-runner
VERSION: ${{ github.sha }}
jobs:
build:
name: build and deploy
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Calculate image name in owner/repo format
id: names
run: |
owner="$(echo ${{ github.repository_owner }} | tr '[A-Z]' '[a-z]')"
echo "image_id=$owner/$IMAGE_NAME" >> $GITHUB_OUTPUT
- name: Check if image exists
id: pull
run: |
success=false
if docker pull "ghcr.io/$IMAGE_ID:$VERSION"; then
success=true
fi
echo "success=$success" >> $GITHUB_OUTPUT
env:
IMAGE_ID: ${{ steps.names.outputs.image_id }}
continue-on-error: true
# Login to repository, if needed
- name: Login to ghcr.io
if: ${{ github.event_name == 'push' }}
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# If image does not exist, build it from source
- name: Checkout source
if: ${{ steps.pull.outputs.success != 'true' }}
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Build image
if: ${{ steps.pull.outputs.success != 'true' }}
run: |
DOCKER_BUILDKIT=1 docker build --tag "ghcr.io/$IMAGE_ID:$VERSION" --label "runnumber=$GITHUB_RUN_ID" .
env:
IMAGE_ID: ${{ steps.names.outputs.image_id }}
# Push the image, unless running for a pull request
- name: Push image by SHA
if: ${{ steps.pull.outputs.success != 'true' && github.event_name == 'push' }}
run: |
docker push "ghcr.io/$IMAGE_ID:$VERSION"
env:
IMAGE_ID: ${{ steps.names.outputs.image_id }}
- name: Apply tag
if: ${{ github.event_name == 'push' && github.ref_type == 'tag' }}
run: |
docker tag "ghcr.io/$IMAGE_ID:$VERSION" "ghcr.io/$IMAGE_ID:$TAG"
docker push "ghcr.io/$IMAGE_ID:$TAG"
env:
IMAGE_ID: ${{ steps.names.outputs.image_id }}
TAG: ${{ github.ref_name }}
- name: Logout
if: always()
run: |
docker logout