Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
ci(GHA): modify Github Actions to use docker bake
Browse files Browse the repository at this point in the history
Signed-off-by: András Jáky <[email protected]>
  • Loading branch information
akijakya committed Jan 22, 2024
1 parent 1e90ec5 commit dacb6dd
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 91 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ jobs:
use_release_repository: false
post_image_tags: true

e2e:
needs: build
name: End-to-End
uses: ./.github/workflows/reusable-end-to-end-testing.yml
with:
image_tag: ${{ format('pr{0}-{1}', github.event.pull_request.number, github.sha) }}
use_release_repository: false
# e2e:
# needs: build
# name: End-to-End
# uses: ./.github/workflows/reusable-end-to-end-testing.yml
# with:
# image_tag: ${{ format('pr{0}-{1}', github.event.pull_request.number, github.sha) }}
# use_release_repository: false

success:
needs:
- build
- e2e
# - e2e
- helm-docs-verification
name: Success
runs-on: ubuntu-latest
Expand Down
256 changes: 173 additions & 83 deletions .github/workflows/reusable-build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ name: Build & Push
on:
workflow_call:
inputs:
registry_name:
required: false
type: string
description: 'Registry name used for container image names. Default is `ghcr.io/openclarity`.'
default: ghcr.io/openclarity
image_tag:
required: true
type: string
Expand All @@ -17,6 +22,11 @@ on:
type: boolean
description: 'If set to true the image is pushed to the release repository otherwise it is pushed to the development.'
default: false
bake-group:
required: false
type: string
description: 'Name of the Docker Bake group of targets'
default: 'e2e'
post_image_tags:
required: false
type: boolean
Expand All @@ -25,10 +35,14 @@ on:
Only works if the event type is `pull_request`.
jobs:
timestamp:
build-vars:
runs-on: ubuntu-latest
outputs:
timestamp: ${{ steps.timestamp.outputs.timestamp }}
registry: ${{ steps.registry.outputs.registry }}
suffix: ${{ steps.suffix.outputs.suffix }}
targets: ${{ steps.targets.outputs.targets }}
image-name-cache-prefix: ${{ steps.prefix.outputs.prefix }}
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -39,99 +53,175 @@ jobs:
##
## Set timestamp variable
##
echo "timestamp=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> "$GITHUB_OUTPUT"
images:
uses: ./.github/workflows/reusable-image-names.yml
with:
use_release_repository: ${{ inputs.use_release_repository }}
- name: Set registry
id: registry
run: |
##
## Determine the image name registry
##
vmclarity-apiserver:
needs:
- images
- timestamp
uses: ./.github/workflows/build-and-push-component.yaml
with:
dockerfile: Dockerfile.apiserver
image_name: ${{ needs.images.outputs.apiserver-image }}
image_tag: ${{ inputs.image_tag }}
push: ${{ inputs.push }}
timestamp: ${{ needs.timestamp.outputs.timestamp }}

vmclarity-orchestrator:
needs:
- images
- timestamp
uses: ./.github/workflows/build-and-push-component.yaml
with:
dockerfile: Dockerfile.orchestrator
image_name: ${{ needs.images.outputs.orchestrator-image }}
image_tag: ${{ inputs.image_tag }}
push: ${{ inputs.push }}
timestamp: ${{ needs.timestamp.outputs.timestamp }}

vmclarity-ui-backend:
needs:
- images
- timestamp
uses: ./.github/workflows/build-and-push-component.yaml
with:
dockerfile: Dockerfile.uibackend
image_name: ${{ needs.images.outputs.ui-backend-image }}
image_tag: ${{ inputs.image_tag }}
push: ${{ inputs.push }}
timestamp: ${{ needs.timestamp.outputs.timestamp }}

vmclarity-ui:
needs:
- images
- timestamp
uses: ./.github/workflows/build-and-push-component.yaml
with:
dockerfile: Dockerfile.ui
image_name: ${{ needs.images.outputs.ui-image }}
image_tag: ${{ inputs.image_tag }}
push: ${{ inputs.push }}
timestamp: ${{ needs.timestamp.outputs.timestamp }}

vmclarity-cli:
needs:
- images
- timestamp
uses: ./.github/workflows/build-and-push-component.yaml
with:
dockerfile: Dockerfile.cli
image_name: ${{ needs.images.outputs.cli-image }}
image_tag: ${{ inputs.image_tag }}
push: ${{ inputs.push }}
timestamp: ${{ needs.timestamp.outputs.timestamp }}

post-images:
if: github.event_name == 'pull_request' && inputs.post_image_tags
# Remove trailing slash characters(s)
# shellcheck disable=SC2001
echo "registry=$(sed -e 's@/*$@@' <<< ${{ inputs.registry_name }})" >> "$GITHUB_OUTPUT"
- name: Set suffix
id: suffix
run: |
##
## Determine the image name suffix based on the release type
##
# Set image name suffix
suffix=-dev
if [ "${{ inputs.use_release_repository }}" == "true" ]; then
suffix=
fi
echo "suffix=${suffix}" >> "$GITHUB_OUTPUT"
- name: List targets
id: targets
uses: docker/bake-action/subaction/list-targets@v4
with:
target: ${{ inputs.bake-group }}

- name: Set image name cache prefix
id: prefix
run: |
echo "prefix=image-name-cache-{{ github.run_id }}" >> "$GITHUB_OUTPUT"
build-and-push:
runs-on: ubuntu-latest
needs:
- images
- vmclarity-apiserver
- vmclarity-orchestrator
- vmclarity-ui-backend
- vmclarity-ui
- vmclarity-cli
- build-vars
strategy:
fail-fast: false
matrix:
target: ${{ fromJson(needs.build-vars.outputs.targets) }}
# outputs:
# images: ${{ toJson(steps.images.outputs) }}
steps:
- name: Post comment with image tags
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.ref }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
id: bake
uses: docker/bake-action@v4
with:
targets: ${{ matrix.target }}
push: ${{ inputs.push }}
set: |
*.platform=["linux/amd64","linux/arm64"]
env:
DOCKER_TAG: ${{ inputs.image_tag }}
DOCKER_REGISTRY: ${{ needs.build-vars.outputs.registry }}
SUFFIX: ${{ needs.build-vars.outputs.suffix }}
VERSION: ${{ inputs.image_tag }}
BUILD_TIMESTAMP: ${{ needs.build-vars.outputs.timestamp }}
COMMIT_HASH: ${{ github.sha }}

- name: Get image name
run: |
echo "image-name=$(jq -r '.[].\"image.name\"' <<< ${{ toJson(steps.bake.outputs.metadata) }})" >> "$GITHUB_ENV"
- name: Update comment with image name
uses: marocchino/sticky-pull-request-comment@v2
with:
hide_and_recreate: true
hide_classify: "OUTDATED"
skip_unchanged: true
header: image-tags
append: true
message: |
Hey!
Your images are ready:
* `${{ format('{0}:{1}', needs.images.outputs.apiserver-image, inputs.image_tag) }}`
* `${{ format('{0}:{1}', needs.images.outputs.orchestrator-image, inputs.image_tag) }}`
* `${{ format('{0}:{1}', needs.images.outputs.ui-backend-image, inputs.image_tag) }}`
* `${{ format('{0}:{1}', needs.images.outputs.ui-image, inputs.image_tag) }}`
* `${{ format('{0}:{1}', needs.images.outputs.cli-image, inputs.image_tag) }}`
Your image for ${{ matrix.target }} is ready: ${{ env.image-name }}
# - name: Export image name
# if: inputs.push
# id: digest
# run: |
# mkdir -p /tmp/images
# image="${{ fromJSON(steps.bake.outputs.metadata)[]['image.name'] }}"
# touch "/tmp/images/${digest}"
#
# echo "image=${digest}" >> "$GITHUB_OUTPUT"
#
# - name: Upload image names
# if: inputs.push
# uses: actions/upload-artifact@v4
# with:
# name: ${{ needs.build-vars.outputs.image-name-cache-prefix }}-${{ steps.digest.outputs.digest }}
# path: /tmp/images/*
# if-no-files-found: error
# retention-days: 1
# compression-level: 0

# - name: Add image name to output
# id: images
# run: |
# export TARGET=${{ matrix.target }}
# echo "$TARGET=${{ fromJSON(steps.bake.outputs.metadata)['$TARGET']['image.name'] }}" >> "$GITHUB_OUTPUT"
# echo "::set-output name=${{ matrix.target }}::${{ fromJSON(steps.bake.outputs.metadata)['${{ matrix.target }}']['image.name'] }}"
# echo "$(jq -r '.[].\"image.name\"' <<< ${{ toJson(steps.bake.outputs.metadata) }})" >> $GITHUB_OUTPUT
# echo "${{ fromJSON(steps.bake.outputs.metadata)['${{ matrix.target }}']['image.name'] }}" >> $GITHUB_OUTPUT

# post-images:
# if: github.event_name == 'pull_request' && inputs.post_image_tags
# runs-on: ubuntu-latest
# needs:
# - build-vars
# - build-and-push
# steps:
# - name: Download image names
# uses: actions/download-artifact@v4
# with:
# pattern: ${{ needs.build-vars.outputs.image-name-cache-prefix }}-*
# merge-multiple: true
# path: /tmp/images
#
# - name: Get image names
# id: images
# working_directory: /tmp/images
# run: |
# cat *
#
# - name: Post comment with image tags
# uses: marocchino/sticky-pull-request-comment@v2
# with:
# hide_and_recreate: true
# hide_classify: "OUTDATED"
# skip_unchanged: true
# header: image-tags
# append: true
# message: |
# Hey!
#
# bake output metadata:
# ${{ steps.images.outputs }}

# Your images are ready:
# * `$(jq -r '.apiserver."image.name"' <<< ${{ toJson(needs.bake.outputs.metadata) }})`
# * `$(jq -r '.orchestrator."image.name"' <<< ${{ toJson(needs.bake.outputs.metadata) }})`
# * `$(jq -r '.ui-backend."image.name"' <<< ${{ toJson(needs.bake.outputs.metadata) }})`
# * `$(jq -r '.ui."image.name"' <<< ${{ toJson(needs.bake.outputs.metadata) }})`
# * `$(jq -r '.cli."image.name"' <<< ${{ toJson(needs.bake.outputs.metadata) }})`

0 comments on commit dacb6dd

Please sign in to comment.