Skip to content

Commit

Permalink
PMM-13487 Move rewinding branches to the entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ademidoff committed Dec 14, 2024
1 parent d5cf4db commit 05d9230
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 67 deletions.
5 changes: 3 additions & 2 deletions build/local/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ Currently, Local Builds target the following platforms and distributions:
* use the `--debug` parameter to control the verbosity of the logs (1/2 ✅)
* remove `jq` from prerequisites ✅
* do not require `ci.yml` to be present, generate it based on the current branch name of this (percona/pmm) repository ✅
* output the build summary at the end of the build
* implement the `--release` parameter
* output the build summary at the build completion
* implement the `--release-build` parameter
* implement the `--clean` parameter
* move the builds and the cache from the host to the container, fully isolating the build process
* cache the `nomad` client component to speed up the builds
55 changes: 29 additions & 26 deletions build/local/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/bin/bash -e
set -o errexit
set -o nounset
#!/bin/bash -eu

usage() {
cat <<-EOF
Expand All @@ -15,7 +13,7 @@ Options:
--no-client-docker Do not build PMM Client docker image
--log-file <path> Save build logs to a file located at <path> (defaults to $PWD/build.log)
Note: the log file will get reset on every subsequent run
--release-build Make it a release, or release candidate build (otherwise it's a feature build)
--release-build Mark it a release, or release candidate build (otherwise it's a feature build)
-d --debug Turn on a more verbose output mode, useful for troubleshooting
-h --help Display help
Expand Down Expand Up @@ -254,32 +252,33 @@ update() {
fi

echo
echo "This script rewinds submodule branches as per the joint config of '.gitmodules' and user-supplied 'ci.yml'"
echo "This script rewinds submodule branches as per the joint config of '.gitmodules' and the user-supplied 'ci.yml'"
echo

cd "$SUBMODULES"

# Loop through submodules and rewind them one-by-one
while IFS= read -r line; do
local key="" val="" pair="" name="" path="" branch="" url="" pairs=()

# Parse the colon-separated subkeys and subvalues
IFS='|' read -r -a pairs <<< "$line"
for pair in "${pairs[@]}"; do
key="${pair%%=*}"
val="${pair#*=}"
if [[ "$key" =~ name|path|branch|url ]]; then
eval "$key=$val"
fi
done
# # Loop through submodules and rewind them one-by-one
# while IFS= read -r line; do
# local key="" val="" pair="" name="" path="" branch="" url="" commit="" pairs=()

# # Parse the colon-separated subkeys and subvalues
# IFS='|' read -r -a pairs <<< "$line"
# for pair in "${pairs[@]}"; do
# key="${pair%%=*}"
# val="${pair#*=}"
# if [[ "$key" =~ name|path|branch|url ]]; then
# # Set the variables dynamically
# eval "$key=$val"
# fi
# done

rewind "$path" "$branch" "$name"
# rewind "$path" "$branch" "$name"

commit=$(git -C "$path" rev-parse HEAD)
echo "name=${name}|path=${path}|url=${url}|branch=${branch}|commit=${commit}" >> "build/deps2.txt"
done < "build/deps.txt"
# commit=$(git -C "$path" rev-parse HEAD)
# echo "name=${name}|path=${path}|url=${url}|branch=${branch}|commit=${commit}" >> "build/deps2.txt"
# done < "build/deps.txt"

mv -f "build/deps2.txt" "build/deps.txt"
# mv -f "build/deps2.txt" "build/deps.txt"
echo
echo "Printing git status..."
git status --short
Expand All @@ -289,6 +288,7 @@ update() {
git submodule status

cd "$CURDIR" > /dev/null
exit 0

if [ "$UPDATE_ONLY" -eq 1 ]; then
exit 0
Expand Down Expand Up @@ -387,9 +387,9 @@ check_volumes() {
for volume in pmm-gobuild pmm-gomod pmm-yarn pmm-dnf; do
if ! docker volume ls | grep "$volume" >/dev/null; then
docker volume create "$volume" > /dev/null
echo "Info: docker volume $volume created."
echo "Info: docker volume $volume created"
else
echo "Info: docker volume $volume checked."
echo "Info: docker volume $volume checked"
fi
done

Expand Down Expand Up @@ -454,7 +454,7 @@ check_if_installed() {
}

check_preprequisites() {
local commands=("docker" "make" "bash" "tar" "git" "curl")
local commands=("docker" "make" "bash" "tar" "git" "curl" "find" "shasum")
echo "Checking pre-requisites..."
for cmd in "${commands[@]}"; do
check_if_installed "$cmd"
Expand All @@ -465,6 +465,9 @@ check_preprequisites() {
echo
exit 1
fi

echo "Pre-requisites check passed"
echo
}

cleanup() {
Expand Down
137 changes: 98 additions & 39 deletions build/local/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,65 +1,124 @@
#!/bin/bash
set -o errexit

git config --global --add safe.directory /app
git config --add safe.directory /app

rm -f gitmodules.yml

if [ -s "ci.yml" ]; then
echo "Info: ci.yml was found, we will use it in combination with defaults to resolve dependencies."
echo "If you like to (re)discover the dependencies base on the '${BRANCH_NAME}' branch, please remove the ci.yml file."
fi

needs_to_pull() {
local UPSTREAM=${1:-'@{u}'}
local LOCAL=$(git rev-parse @)
local BASE=$(git merge-base @ "$UPSTREAM")
local REMOTE=$(git rev-parse "$UPSTREAM")

if [ "$LOCAL" = "$REMOTE" ]; then
return 1 # false, we are up-to-date
fi

if [ "$LOCAL" = "$BASE" ]; then
return 0 # true, we are behind upstream
fi
}

rewind() {
local DIR="$1"
local BRANCH="$2"
local NAME="$3"

cd "$DIR" > /dev/null
local CURRENT=$(git rev-parse --abbrev-ref HEAD)
echo "Rewinding submodule ${NAME}..."
git fetch

if [ "$CURRENT" != "$BRANCH" ]; then
echo "Currently on $CURRENT, checking out $BRANCH"
git checkout "$BRANCH"
fi

if needs_to_pull; then
if ! git pull origin; then
git reset --hard HEAD~30
git pull origin > /dev/null
fi
echo "Submodule ${NAME} has pulled from upstream"
git log --oneline -n 2
cd - > /dev/null
git add "$DIR"
echo
else
cd - > /dev/null
echo "Submodule ${NAME} is up-to-date with upstream"
echo
fi
}

# Join the dependencies listed in .gitmodules and ci.yml and output the result to gitmodules.yml.
# This script will not fail even if ci.yml is empty.
# This script accepts an empty ci.yml.
if ! python ci.py --convert; then
echo "Error: could not convert the ci.yml config file to gitmodules.yml, exiting..."
exit 1
fi

DEPS=$(yq -o=json '.' gitmodules.yml | jq -r '[.deps[] | {name: .name, branch: .branch, path: .path, url: .url}]')
echo "$DEPS" > /app/build/build.json
echo -n > /tmp/deps.txt

DISCOVERED_BRANCHES=()

while read -r item; do
branch=$(echo "$item" | jq -r '.branch')
path=$(echo "$item" | jq -r '.path')
name=$(echo "$item" | jq -r '.name')
url=$(echo "$item" | jq -r '.url')

if [ -n "${BRANCH_NAME:-}" ]; then
commit=$(git ls-remote --heads "$url" "$BRANCH_NAME" | cut -f1)
if [ -n "$commit" ]; then
echo
echo "Info: branch '$BRANCH_NAME' found in '$name' submodule, will use it instead of '$branch'"
branch="$BRANCH_NAME"
DISCOVERED_BRANCHES+=( "$name|$branch|$path|$url" )
echo "name=${name}|path=${path}|url=${url}|branch=${branch}|commit=${commit}" >> /tmp/deps.txt
else
echo "name=${name}|path=${path}|url=${url}|branch=${branch}|commit=none" >> /tmp/deps.txt
declare deps=$(yq -o=json '.' gitmodules.yml | jq -r '[.deps[] | {name: .name, branch: .branch, path: .path, url: .url}]')
# echo "$deps" > /app/build/build.json

declare -a discovered_branches
declare discovered_file="/tmp/ci.yml"
declare branch path name url commit
declare updated_deps="[]"
# BRANCHE_NAME is passed via the environment variable

# Only run this block if ci.yml is not present or empty
if [ ! -s "ci.yml" ]; then
while read -r item; do
branch=$(echo "$item" | jq -r '.branch')
path=$(echo "$item" | jq -r '.path')
name=$(echo "$item" | jq -r '.name')
url=$(echo "$item" | jq -r '.url')

if [ -n "${BRANCH_NAME:-}" ]; then
commit=$(git ls-remote --heads "$url" "$BRANCH_NAME" | cut -f1)
if [ -n "$commit" ]; then
echo
echo "Info: branch '$BRANCH_NAME' found in '$name' submodule, will use it instead of the default '$branch' branch"
branch="$BRANCH_NAME"
discovered_branches+=( "$name|$branch|$path|$url" )
fi
fi
else
echo "name=${name}|path=${path}|url=${url}|branch=${branch}|commit=none" >> /tmp/deps.txt
fi
done < <(echo "$DEPS" | jq -c '.[]')

cat /tmp/deps.txt > /app/build/deps.txt
rewind "$path" "$branch" "$name"
commit=$(git -C "$path" rev-parse HEAD)

DISCOVERED_FILE="/tmp/discovered.yml"
updated_deps=$(echo "$updated_deps" | jq ". += [{name: \"$name\", branch: \"$branch\", commit: \"$commit\", path: \"$path\", url: \"$url\"}]")

# echo "name=${name}|path=${path}|url=${url}|branch=${branch}" >> /tmp/deps.txt
done < <(echo "$deps" | jq -c '.[]')

# cat /tmp/deps.txt > /app/build/deps.txt
echo "$updated_deps" > /app/build/build.json
fi

if [[ "${#DISCOVERED_BRANCHES[@]}" -gt 0 ]]; then
echo "Generating... $DISCOVERED_FILE"
echo "deps:" > "$DISCOVERED_FILE"
for item in "${DISCOVERED_BRANCHES[@]}"; do
if [[ "${#discovered_branches[@]}" -gt 0 ]]; then
echo "Generating... $discovered_file"
echo "deps:" > "$discovered_file"
for item in "${discovered_branches[@]}"; do
echo "$item" | IFS='|' read -r name branch path url
echo " - name: $name" >> "$DISCOVERED_FILE"
echo " branch: $branch" >> "$DISCOVERED_FILE"
echo " path: $path" >> "$DISCOVERED_FILE"
echo " url: $url" >> "$DISCOVERED_FILE"
echo " - name: $name" >> "$discovered_file"
echo " branch: $branch" >> "$discovered_file"
echo " path: $path" >> "$discovered_file"
echo " url: $url" >> "$discovered_file"
done
fi

if [ ! -s "ci.yml" ]; then
echo
echo "Info: generating ci.yml..."
cat "$DISCOVERED_FILE" > ci.yml
cat "$discovered_file" > ci.yml
fi

rm -f gitmodules.yml

0 comments on commit 05d9230

Please sign in to comment.