Skip to content

Commit

Permalink
chore: script to extract tag version (#2368)
Browse files Browse the repository at this point in the history
Fixes #2058
This should mean that versioning is consistent across the board, using
semver format (e.g. `0.7.5` with **no** leading `v`)
  • Loading branch information
spypsy authored Sep 18, 2023
1 parent 68fe053 commit 4b686b0
Showing 6 changed files with 71 additions and 129 deletions.
35 changes: 4 additions & 31 deletions build-system/scripts/build
Original file line number Diff line number Diff line change
@@ -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=""
@@ -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
28 changes: 3 additions & 25 deletions build-system/scripts/create_dockerhub_manifest
Original file line number Diff line number Diff line change
@@ -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
@@ -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
23 changes: 2 additions & 21 deletions build-system/scripts/deploy_dockerhub
Original file line number Diff line number Diff line change
@@ -23,33 +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
fi

echo "Deploying to dockerhub: $IMAGE_DEPLOY_URI"

# Login.
25 changes: 2 additions & 23 deletions build-system/scripts/deploy_npm
Original file line number Diff line number Diff line change
@@ -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)
@@ -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
@@ -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

0 comments on commit 4b686b0

Please sign in to comment.