diff --git a/.github/scripts/inject_commit.sh b/.github/scripts/inject_commit.sh new file mode 100644 index 00000000..aed60449 --- /dev/null +++ b/.github/scripts/inject_commit.sh @@ -0,0 +1,58 @@ +#!/bin/bash +# Copyright (C) 2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 +WORKPATH=$PWD +blocks=() +while IFS= read -r line; do + if [[ $line =~ build ]]; then + block="$line" + while IFS= read -r next_line; do + if [[ $next_line =~ ^\ {6,} ]]; then + block="$block"$'\n'"$next_line" + else + blocks+=("$block") + block="" + break + fi + done + fi +done < docker-compose.yaml +for build_block in "${blocks[@]}"; do + config_path=$(echo "$build_block" | awk '/context:/ { context = $2 } /dockerfile:/ { dockerfile = $2; combined = context "/" dockerfile; gsub(/\/+/, "/", combined); print combined }') + sed -i -e '/^[^#]*FROM /a\'"\nARG COMMIT_SHA\nARG COMMIT_MESSAGE\nLABEL commit.sha=\$COMMIT_SHA\nLABEL commit.message=\$COMMIT_MESSAGE" $WORKPATH/$config_path + + commit_path=$(echo "$build_block" | awk '$1 == "context:" {print$2}') + cd $WORKPATH/$commit_path + COMMIT_SHA=$(git rev-parse --short HEAD) + COMMIT_MESSAGE=$(git log -1 --pretty=%B) + new_content=$(printf " COMMIT_SHA: %s\n COMMIT_MESSAGE: |\n%s" "$COMMIT_SHA" "$(echo "$COMMIT_MESSAGE" | sed 's/^/ /')") + if [[ "$build_block" == *"args:"* ]]; then + mapfile -t lines <<< "$build_block" + args_line_index=0 + for i in "${!lines[@]}"; do + if [[ "${lines[$i]}" == *"args:"* ]]; then + args_line_index=$((i+1)) + break + fi + done + first_part=("${lines[@]:0:args_line_index}") + second_part=("${lines[@]:args_line_index}") + insert_build=$(printf "%s\n" "${first_part[@]}" "$new_content" "${second_part[@]}") + else + mapfile -t lines <<< "$build_block" + args_line_index=0 + for i in "${!lines[@]}"; do + if [[ "${lines[$i]}" == *"dockerfile:"* ]]; then + args_line_index=$((i+1)) + break + fi + done + first_part=("${lines[@]:0:args_line_index}") + second_part=("${lines[@]:args_line_index}") + insert_build=$(printf "%s\n" "${first_part[@]}" " args:" "$new_content" "${second_part[@]}") + fi + + escaped_build_block=$(printf '%s\n' "$build_block" | sed 's/[\/&]/\\&/g; $!s/$/\\/') + escaped_insert_build=$(printf '%s\n' "$insert_build" | sed 's/[\/&]/\\&/g; $!s/$/\\/') + sed -i ":a;N;\$!ba;s/$escaped_build_block/$escaped_insert_build/" $WORKPATH/docker-compose.yaml +done diff --git a/actions/image-build/action.yml b/actions/image-build/action.yml index 04dec7d9..5a9d88ad 100644 --- a/actions/image-build/action.yml +++ b/actions/image-build/action.yml @@ -28,16 +28,35 @@ inputs: required: false default: "latest" type: string + inject_commit: + default: false + description: "Whether the test range is CI or CD" + required: false + type: string runs: using: "composite" steps: + - name: Checkout out Validation + uses: actions/checkout@v4 + with: + repository: opea-project/Validation + path: Validation - name: Build Containers shell: bash run: | service_list=$(echo ${{ inputs.service_list }} | tr ',' ' ') echo $service_list cp ${{ inputs.docker_compose_path }} ${{ inputs.work_dir }}/docker-compose.yaml + if [[ "true"=="${{ inputs.inject_commit }}" ]]; then + echo "************Start inject commit id and message************" + cp ${{ github.workspace }}/Validation/.github/scripts/inject_commit.sh ${{ inputs.work_dir }}/ + WORKPATH=${{ inputs.work_dir }} + bash inject_commit.sh + cat ${{ inputs.work_dir }}/docker-compose.yaml + echo "************Inject commit id and message end************" + fi + cd ${{ inputs.work_dir }} REGISTRY=${{ inputs.registry }} \ TAG=${{ inputs.tag }} \ docker compose build ${service_list} --no-cache