Skip to content

Commit

Permalink
chore(bors): merge pull request #807
Browse files Browse the repository at this point in the history
807: build: harden build by pre-fetching dependencies r=tiagolobocastro a=tiagolobocastro

Attempt to vendor the cargo deps for up to 25 times Supports linking the resulting derivation into a global location through env var CARGO_VENDOR_DIR, example:
> CARGO_VENDOR_DIR=/tmp ./scripts/release.sh --image ""
/nix/store/2x03mh7l1q0jx4xfsd3nw4h4ks0pxxya-cargo-vendor-dir Cargo vendored dependencies pre-fetched into /tmp/$BRANCH/mayastor-controller after 1 attempt(s)

This can allow us to create a place holder in the jenkins nodes to keep the last dependencies for each main branch of each repo.

Co-authored-by: Tiago Castro <[email protected]>
  • Loading branch information
mayastor-bors and tiagolobocastro committed Apr 12, 2024
2 parents f833a40 + f481447 commit 831da56
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
5 changes: 4 additions & 1 deletion nix/pkgs/control-plane/cargo-project.nix
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ let
doCheck = false;
};
release_build = { "release" = true; "debug" = false; };
cargoDeps = rustPlatform.importCargoLock {
lockFile = ../../../Cargo.lock;
};
in
let
build_with_naersk = { buildType, cargoBuildFlags }:
Expand Down Expand Up @@ -104,7 +107,7 @@ let
builder = if incremental then build_with_naersk else build_with_default;
in
{
inherit LIBCLANG_PATH PROTOC PROTOC_INCLUDE version src;
inherit LIBCLANG_PATH PROTOC PROTOC_INCLUDE version src cargoDeps;

build = { buildType, cargoBuildFlags ? [ ] }:
if allInOne then
Expand Down
48 changes: 43 additions & 5 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ nix_experimental() {
echo -n " "
fi
}
pre_fetch_cargo_deps() {
local nixAttrPath=$1
local project=$2
local maxAttempt=$3

local outLink="--no-out-link"
local cargoVendorMsg=""
if [ -n "$CARGO_VENDOR_DIR" ]; then
if [ "$(realpath -s "$CARGO_VENDOR_DIR")" = "$(realpath -s "$SCRIPTDIR/..")" ]; then
cargoVendorDir="$CARGO_VENDOR_DIR/$GIT_BRANCH"
else
cargoVendorDir="$CARGO_VENDOR_DIR/$project/$GIT_BRANCH"
fi

outLink="--out-link "$cargoVendorDir""
cargoVendorMsg="into $(realpath -s "$cargoVendorDir") "
fi

for (( attempt=1; attempt<=maxAttempt; attempt++ )); do
if $NIX_BUILD $outLink -A "$nixAttrPath"; then
echo "Cargo vendored dependencies pre-fetched "$cargoVendorMsg"after $attempt attempt(s)"
return 0
fi
sleep 1
done
if [ "$attempt" = "1" ]; then
echo "Cargo vendor pre-fetch is disabled"
return 0
fi

echo "Failed to pre-fetch the cargo vendored dependencies in $maxAttempt attempts"
exit 1
}

help() {
cat <<EOF
Expand Down Expand Up @@ -62,8 +95,8 @@ RM="rm"
SCRIPTDIR=$(dirname "$0")
TAG=`get_tag`
HASH=`get_hash`
BRANCH=`git rev-parse --abbrev-ref HEAD`
BRANCH=${BRANCH////-}
GIT_BRANCH=`git rev-parse --abbrev-ref HEAD`
BRANCH=${GIT_BRANCH////-}
IMAGES=
DEFAULT_IMAGES="agents.core agents.ha.node agents.ha.cluster operators.diskpool rest csi.controller csi.node"
UPLOAD=
Expand All @@ -79,6 +112,8 @@ DEFAULT_BINARIES=""
BUILD_BINARIES=
BIN_TARGET_PLAT="linux-musl"
BINARY_OUT_LINK="."
CARGO_VENDOR_DIR=${CARGO_VENDOR_DIR:-}
CARGO_VENDOR_ATTEMPTS=${CARGO_VENDOR_ATTEMPTS:-25}

# Check if all needed tools are installed
curl --version >/dev/null
Expand Down Expand Up @@ -171,8 +206,13 @@ while [ "$#" -gt 0 ]; do
esac
done

IMAGES=${IMAGES:-$DEFAULT_IMAGES}

cd $SCRIPTDIR/..

# pre-fetch build dependencies with a number of attempts to harden against flaky networks
pre_fetch_cargo_deps control-plane.project-builder.cargoDeps "mayastor-controller" "$CARGO_VENDOR_ATTEMPTS"

if [ -n "$BUILD_BINARIES" ]; then
mkdir -p $BINARY_OUT_LINK
for name in $BUILD_BINARIES; do
Expand All @@ -181,9 +221,7 @@ if [ -n "$BUILD_BINARIES" ]; then
done
fi

if [ -z "$IMAGES" ]; then
IMAGES="$DEFAULT_IMAGES"
elif [ $(echo "$IMAGES" | wc -w) == "1" ]; then
if [ $(echo "$IMAGES" | wc -w) == "1" ]; then
image=$(echo "$IMAGES" | xargs)
if $NIX_EVAL -f . "images.debug.$image.imageName" 1>/dev/null 2>/dev/null; then
if [ "$INCREMENTAL" == "true" ]; then
Expand Down

0 comments on commit 831da56

Please sign in to comment.