Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Add custom ref input to container build workflow #927

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions .github/workflows/container-image-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ permissions: {}
on:
workflow_call:
inputs:
ref:
required: false
description: "Git reference of the workflow's trigger"
type: string
image-name:
required: true
description: "Name of the image to build"
Expand Down Expand Up @@ -40,25 +44,41 @@ jobs:
contents: read

steps:
- name: Set GITHUB_REF and GITHUB_REFNAME to the inputs' ref, or default to github's ref
id: set_ref
run: |
REF="${{ inputs.ref || github.ref }}"
echo "Using GITHUB_REF: ${REF}"
REF_NAME="${REF#refs/heads}"
REF_NAME="${REF_NAME#refs/tags}"
echo "GITHUB_REF=${REF}" >> "${GITHUB_ENV}"
echo "GITHUB_REFNAME=${REF_NAME}" >> "${GITHUB_ENV}"

- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
ref: ${{ github.ref }}
ref: ${{ env.GITHUB_REF }}

- name: Set GITHUB_REFSHA to sha of the ref
id: set_refsha
run: |
REF_SHA=$(git rev-parse HEAD)
echo "GITHUB_REFSHA=${REF_SHA}" >> "${GITHUB_ENV}"

- name: Get current date
id: date
run: echo "current_date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
run: echo "current_date=$(date +'%Y-%m-%d')" >> "${GITHUB_OUTPUT}"

- name: Get image tags
id: image_tags
run: |
BASE_TAG=`echo "${{ github.ref_name }}" | sed 's/\//_/'`
IMAGE_TAGS="${BASE_TAG}, ${BASE_TAG}_${{ steps.date.outputs.current_date }}_${{ github.sha }}"
BASE_TAG=`echo "${{ env.GITHUB_REFNAME }}" | sed 's/\//_/'`
IMAGE_TAGS="${BASE_TAG}, ${BASE_TAG}_${{ steps.date.outputs.current_date }}_${{ env.GITHUB_REFSHA }}"
if [[ -n "${{ inputs.image-tag }}" ]]; then
IMAGE_TAGS="${{ inputs.image-tag }}"
elif [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
elif [[ "${{ env.GITHUB_REF }}" == "refs/heads/main" ]]; then
IMAGE_TAGS="${IMAGE_TAGS}, latest"
fi
echo "IMAGE_TAGS=${IMAGE_TAGS}" >> $GITHUB_ENV
echo "IMAGE_TAGS=${IMAGE_TAGS}" >> "${GITHUB_ENV}"

- name: Build ${{ inputs.image-name }} image
uses: mr-smithers-excellent/docker-build-push@59523c638baec979a74fea99caa9e29d97e5963c # v6.4
Expand All @@ -76,9 +96,9 @@ jobs:
if: ${{ failure() }}
uses: rtCamp/action-slack-notify@c33737706dea87cd7784c687dadc9adf1be59990 # 2.3.2
env:
SLACK_TITLE: 'GitHub Action Failed in ${{ github.repository }}'
SLACK_COLOR: '#FF0000'
SLACK_MESSAGE: 'The GitHub Action workflow failed for ${{ inputs.image-name }} image build.'
SLACK_TITLE: "GitHub Action Failed in ${{ github.repository }}"
SLACK_COLOR: "#FF0000"
SLACK_MESSAGE: "The GitHub Action workflow failed for ${{ inputs.image-name }} image build."
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_CHANNEL: metal3-github-actions-notify
SLACK_USERNAME: metal3-github-actions-notify
Loading