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

chore: script to extract tag version #2368

Merged
merged 5 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
35 changes: 4 additions & 31 deletions build-system/scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,7 @@ for PARENT in $PARENTS; do
retry docker tag $PARENT_IMAGE_URI $TAG
done


# Extract version from commit tag
# Check if there is a commit tag
if [[ -n "$COMMIT_TAG" ]]; then

# Check if it's a repo-specific tag
if [[ "$COMMIT_TAG" == *"/"* ]]; then
REPO_NAME="${COMMIT_TAG%%/*}"
COMMIT_TAG_VERSION="${COMMIT_TAG#*/}"
echo "Tag was made for: $REPO_NAME"
echo "Version: $COMMIT_TAG_VERSION"

# Check if REPO_NAME is equal to REPOSITORY
if [ "$REPO_NAME" != "$REPOSITORY" ]; then
echo "REPO_NAME ($REPO_NAME) does not match REPOSITORY ($REPOSITORY). Ignoring..."
COMMIT_TAG_VERSION=""
fi
else
COMMIT_TAG_VERSION=$COMMIT_TAG
fi

# We are building a tagged commit.
# We prefer to strip the leading 'v' (if it exists) from versions when passing to docker.
COMMIT_TAG_VERSION=${COMMIT_TAG_VERSION#v}
else
COMMIT_TAG_VERSION=""
fi

COMMIT_TAG_VERSION=$(extract_tag_version $REPOSITORY false)

# Pull, build and push each named stage to cache.
STAGE_CACHE_FROM=""
Expand All @@ -169,16 +142,16 @@ for STAGE in $STAGES; do
STAGE_CACHE_FROM="--cache-from $STAGE_IMAGE_LAST_URI"
fi
fi

echo "Building stage: $STAGE"
STAGE_IMAGE_COMMIT_URI=$ECR_URL/$REPOSITORY:cache-$CONTENT_HASH-$STAGE
# Build our dockerfile, add timing information
docker build --target $STAGE $STAGE_CACHE_FROM -t $STAGE_IMAGE_COMMIT_URI -f $DOCKERFILE --build-arg COMMIT_TAG=$COMMIT_TAG_VERSION --build-arg ARG_CONTENT_HASH=$CONTENT_HASH . \
| while read line ; do echo "$(date "+%H:%M:%S")| $line"; done

# We don't want to have redo this stages work when building the final image. Use it as a layer cache.
CACHE_FROM="--cache-from $STAGE_IMAGE_COMMIT_URI $CACHE_FROM"

echo "Pushing stage: $STAGE"
retry docker push $STAGE_IMAGE_COMMIT_URI > /dev/null 2>&1
echo
Expand Down
28 changes: 3 additions & 25 deletions build-system/scripts/create_dockerhub_manifest
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,8 @@ echo "Arch list: $ARCH_LIST"
ACCOUNT="aztecprotocol"
USERNAME="aztecprotocolci"

COMMIT_TAG_VERSION=$COMMIT_TAG # default unless repo-specific
# Check if it's a repo-specific tag
if [[ "$COMMIT_TAG" == *"/"* ]]; then
REPO_NAME="${COMMIT_TAG%%/*}"
COMMIT_TAG_VERSION="${COMMIT_TAG#*/}"
echo "Tag was made for: $REPO_NAME"
echo "Version: $COMMIT_TAG_VERSION"

# Check if REPO_NAME is equal to REPOSITORY
if [ "$REPO_NAME" != "$REPOSITORY" ]; then
echo "REPO_NAME ($REPO_NAME) does not match REPOSITORY ($REPOSITORY). Exiting..."
exit 1
fi
fi

# Check it's a valid semver.
VERSION=$(npx semver $COMMIT_TAG_VERSION)
if [ -z "$VERSION" ]; then
echo "$COMMIT_TAG_VERSION is not a semantic version."
exit 1
fi

# We now have the tage for each image
IMAGE_TAG=$COMMIT_TAG_VERSION
# Extract the version, if valid; exit otherwise.
IMAGE_TAG=$(extract_tag_version $REPOSITORY "true")

MANIFEST_DEPLOY_URI=$ACCOUNT/$REPOSITORY:$IMAGE_TAG
MANIFEST_LATEST_URI=$ACCOUNT/$REPOSITORY:latest
Expand Down Expand Up @@ -73,7 +51,7 @@ do
echo "Adding image $IMAGE_DEPLOY_URI to manifest list $MANIFEST_DEPLOY_URI"
docker manifest create $MANIFEST_DEPLOY_URI \
--amend $IMAGE_DEPLOY_URI

echo "Adding image $IMAGE_DEPLOY_URI to manifest list $MANIFEST_LATEST_URI"
docker manifest create $MANIFEST_LATEST_URI \
--amend $IMAGE_DEPLOY_URI
Expand Down
25 changes: 2 additions & 23 deletions build-system/scripts/deploy_dockerhub
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,14 @@ if [[ -n "$ARCH" ]]; then
IMAGE_LATEST_URI=$IMAGE_LATEST_URI-$ARCH
fi

COMMIT_TAG_VERSION=$COMMIT_TAG # default unless repo-specific
# Check if it's a repo-specific tag
if [[ "$COMMIT_TAG" == *"/"* ]]; then
REPO_NAME="${COMMIT_TAG%%/*}"
COMMIT_TAG_VERSION="${COMMIT_TAG#*/}"
echo "Tag was made for: $REPO_NAME"
echo "Version: $COMMIT_TAG_VERSION"

# Check if REPO_NAME is equal to REPOSITORY
if [ "$REPO_NAME" != "$REPOSITORY" ]; then
echo "REPO_NAME ($REPO_NAME) does not match REPOSITORY ($REPOSITORY). Exiting..."
exit 1
fi
fi
COMMIT_TAG_VERSION=$(extract_tag_version $REPOSITORY "true")

IMAGE_TAG=$COMMIT_TAG_VERSION
IMAGE_DEPLOY_URI=$ACCOUNT/$REPOSITORY:$IMAGE_TAG
if [[ -n "$ARCH" ]]; then
IMAGE_DEPLOY_URI=$IMAGE_DEPLOY_URI-$ARCH
fi

# Check it's a valid semver.
VERSION=$(npx semver $COMMIT_TAG_VERSION)
if [ -z "$VERSION" ]; then
echo "$COMMIT_TAG_VERSION is not a semantic version."
exit 1
else
COMMIT_TAG_VERSION=$VERSION
fi

echo "Deploying to dockerhub: $IMAGE_DEPLOY_URI"

# Login.
Expand Down
25 changes: 2 additions & 23 deletions build-system/scripts/deploy_npm
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,7 @@ cd project/src/$(query_manifest relativeProjectDir $REPOSITORY)

echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc

# Check if it's a repo-specific tag
if [[ "$COMMIT_TAG" == *"/"* ]]; then
REPO_NAME="${COMMIT_TAG%%/*}"
COMMIT_TAG_VERSION="${COMMIT_TAG#*/}"
echo "Tag was made for: $REPO_NAME"
echo "Version: $COMMIT_TAG_VERSION"

# Check if REPO_NAME is equal to REPOSITORY
if [ "$REPO_NAME" != "$REPOSITORY" ]; then
echo "REPO_NAME ($REPO_NAME) does not match REPOSITORY ($REPOSITORY). Exiting..."
exit 1
fi
else
COMMIT_TAG_VERSION=$COMMIT_TAG
fi

# We are publishing a tagged commit. Check it's a valid semver.
VERSION=$(npx semver $COMMIT_TAG_VERSION)
if [ -z "$VERSION" ]; then
echo "$COMMIT_TAG_VERSION is not a semantic version."
exit 1
fi
COMMIT_TAG_VERSION=$(extract_tag_version $REPOSITORY "true")

# If the commit tag itself has a dist-tag (e.g. v2.1.0-testnet.123), extract the dist-tag.
TAG=$(echo "$VERSION" | grep -oP ".*-\K(.*)(?=\.\d+)" || true)
Expand Down Expand Up @@ -71,7 +50,7 @@ fi

# Filter on whitelist of properties.
jq '{name, version, exports, main, homepage, author, type, license, dependencies, description, bin} | with_entries( select( .value != null ) )' \
package.json > $TMP && mv $TMP package.json
package.json > $TMP && mv $TMP package.json

# Publish
npm publish $TAG_ARG --access public
53 changes: 53 additions & 0 deletions build-system/scripts/extract_tag_version
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash
# This script takes a repository name as variable,
# then checks if the commit tag variable (if any)
# is a valid semver & echoes that valid semver.

set -eu

REPOSITORY="$1"
ERROR_ON_FAIL="${2:-"false"}"

# Check if there is a commit tag
if [[ -z "$COMMIT_TAG" ]]; then
if [[ "$ERROR_ON_FAIL" == "true" ]]; then
echo "No commit tag found. Exiting" >&2
exit 1
else
echo ""
exit 0
fi
fi

COMMIT_TAG_VERSION="$COMMIT_TAG" # default unless repo-specific

# Check if it's a repo-specific tag
if [[ "$COMMIT_TAG" == *"/"* ]]; then
REPO_NAME="${COMMIT_TAG%%/*}"
COMMIT_TAG_VERSION="${COMMIT_TAG#*/}"
echo "Tag was made for: $REPO_NAME" >&2
echo "Version: $COMMIT_TAG_VERSION" >&2

# Check if REPO_NAME is equal to REPOSITORY
if [[ "$REPO_NAME" != "$REPOSITORY" ]]; then
echo "REPO_NAME ($REPO_NAME) does not match REPOSITORY ($REPOSITORY). Exiting..." >&2
if [[ "$ERROR_ON_FAIL" == "true" ]]; then
exit 1
else
exit 0
fi
fi
fi

# Check it's a valid semver.
VERSION=$(npx semver "$COMMIT_TAG_VERSION")
if [[ -z "$VERSION" ]]; then
echo "$COMMIT_TAG_VERSION is not a semantic version." >&2
if [[ "$ERROR_ON_FAIL" == "true" ]]; then
exit 1
else
VERSION=""
fi
fi

echo "$VERSION"
36 changes: 7 additions & 29 deletions build-system/scripts/force_deploy_build
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,12 @@ if [[ $FORCE_BUILD == 'true' ]]; then
exit 0
fi

# Check if there's a commit TAG
if [[ -n "${COMMIT_TAG:-}" ]]; then
# Check if it's a repo-specific tag
if [[ "$COMMIT_TAG" == *"/"* ]]; then
REPO_NAME="${COMMIT_TAG%%/*}"
COMMIT_TAG_VERSION="${COMMIT_TAG#*/}"
echo "Tag was made for: $REPO_NAME"
echo "Version: $COMMIT_TAG_VERSION"

# Check if REPO_NAME is equal to REPOSITORY
if [ "$REPO_NAME" != "$REPOSITORY" ]; then
echo "REPO_NAME ($REPO_NAME) does not match REPOSITORY ($REPOSITORY). Ignoring..."
COMMIT_TAG_VERSION=""
fi
else
COMMIT_TAG_VERSION=$COMMIT_TAG
fi

# We are building a tagged commit. Check it's a valid semver.
VERSION=$(npx semver $COMMIT_TAG_VERSION)
if [ -z "$VERSION" ]; then
# Not a version tag, build normally
build $REPOSITORY false $@
else
# Force build
build $REPOSITORY true $@
fi
else
# Not a tagged commit, build normally
COMMIT_TAG_VERSION=$(extract_tag_version $REPOSITORY "false")

if [ -z "$VERSION" ]; then
# Not a version tag, build normally
build $REPOSITORY false $@
else
# Force build
build $REPOSITORY true $@
fi