PromucFlow_constructor/scripts/generate_info_json.sh
Shrikant Sharat Kandula b442ca930e
ci: Include build information in Docker image labels (#39047)
## Description

Completion of https://github.com/appsmithorg/appsmith/pull/39025.

We're able to see the labels with Docker API (script written by Cursor):


![shot-2025-02-06-04-41-44](https://github.com/user-attachments/assets/2523b85c-cd2c-4481-ac08-2de76a0dc979)


Implemented by these two Cursor prompts 🙂


![shot-2025-02-06-05-16-29](https://github.com/user-attachments/assets/6787e5f4-161e-41c6-b6fb-bdec4ea92b38)


![shot-2025-02-06-05-16-43](https://github.com/user-attachments/assets/1ec3d449-0dff-44b0-9b39-487ef6514eea)


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Docker image builds now include added metadata (commit revision,
source, and version details) to improve image traceability and version
tracking.
  
- **Chores**
- CI workflows have been streamlined to consistently generate and apply
metadata labels, ensuring reliable and automated image processing across
all pipelines.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-02-06 11:22:19 +05:30

56 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -o errexit
set -o nounset
commit_sha="$(git rev-parse HEAD)"
# Base URL of the current repository on GitHub.
base_url="$(git remote get-url origin | sed 's,^git@github\.com:,https://github.com/,; s/\.git$//')"
if [[ $# -gt 0 ]]; then
input_version="$1"
if [[ "$input_version" =~ ^v[0-9]+(\.[0-9]+){1,2}$ ]]; then
version="$input_version"
else
echo "Invalid version format. Use v[major].[minor] or v[major].[minor].[patch]." >&2
exit 1
fi
elif [[ "${GITHUB_REF-}" =~ ^refs/tags/v ]]; then
version="${GITHUB_REF#refs/tags/}"
else
latest_released_version="$(git ls-remote --tags --sort=-v:refname "$(git remote | head -1)" 'v*' | awk -F/ '{print $NF; exit}')"
echo "latest_released_version = $latest_released_version" >&2
next_version="$(echo "$latest_released_version" | awk -F. -v OFS=. '{ $2++; $3 = 0; print }')"
echo "next_version = $next_version" >&2
version="$next_version-SNAPSHOT"
fi
if [[ -n ${GITHUB_RUN_ID-} ]]; then
github_run_url="$base_url/actions/runs/$GITHUB_RUN_ID/attempts/${GITHUB_RUN_ATTEMPT-1}"
fi
jq -n \
--arg commitSha "$commit_sha" \
--arg commitUrl "$base_url/commit/$commit_sha" \
--arg githubRef "${GITHUB_REF-}" \
--arg githubRunUrl "${github_run_url-}" \
--arg version "$version" \
--arg imageBuiltAt "$(date -u -Iseconds)" \
--argjson isCI "${CI:-false}" \
'$ARGS.named' | tee "$(git rev-parse --show-toplevel)/deploy/docker/fs/opt/appsmith/info.json"
# If running in GitHub Actions, also output the values to GITHUB_OUTPUT
if [[ -n "${GITHUB_OUTPUT-}" ]]; then
{
echo "commitSha=$commit_sha"
echo "repo=$base_url"
echo "version=$version"
} >> "$GITHUB_OUTPUT"
fi
# Usage
# ./scripts/generate_info_json.sh v0.0.1
# ./scripts/generate_info_json.sh v0.1
# ./scripts/generate_info_json.sh