2023-01-09 17:02:50 +00:00
|
|
|
set -eu
|
|
|
|
|
2023-01-09 21:19:40 +00:00
|
|
|
DEST="$1"
|
|
|
|
GIT_ISH="$2"
|
2023-01-20 17:42:14 +00:00
|
|
|
DEST_INSTALL_URL="$3"
|
|
|
|
|
|
|
|
is_tag() {
|
|
|
|
if [[ "$GITHUB_REF_TYPE" == "tag" ]]; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2023-01-09 21:19:40 +00:00
|
|
|
|
2023-01-09 17:02:50 +00:00
|
|
|
# If the revision directory has already been created in S3 somehow, we don't want to reupload
|
2023-01-09 20:38:53 +00:00
|
|
|
if aws s3 ls "$AWS_BUCKET"/"$GIT_ISH"/; then
|
2023-01-20 17:42:14 +00:00
|
|
|
# Only exit if it's not a tag (since we're tagging a commit previously pushed to main)
|
|
|
|
if ! is_tag; then
|
|
|
|
echo "Revision $GIT_ISH was already uploaded; exiting"
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-01-09 17:02:50 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
sudo chown $USER: -R artifacts/
|
|
|
|
|
2023-01-20 17:42:14 +00:00
|
|
|
mkdir "$DEST"
|
2023-01-09 20:38:53 +00:00
|
|
|
mkdir "$GIT_ISH"
|
2023-01-09 17:02:50 +00:00
|
|
|
|
2023-01-20 17:42:14 +00:00
|
|
|
cp nix-installer.sh "$DEST"/
|
2023-01-09 20:38:53 +00:00
|
|
|
cp nix-installer.sh "$GIT_ISH"/
|
2023-01-09 17:02:50 +00:00
|
|
|
|
|
|
|
for artifact in $(find artifacts/ -type f); do
|
|
|
|
chmod +x "$artifact"
|
2023-01-20 17:42:14 +00:00
|
|
|
cp "$artifact" "$DEST"/
|
2023-01-09 20:38:53 +00:00
|
|
|
cp "$artifact" "$GIT_ISH"/
|
2023-01-09 17:02:50 +00:00
|
|
|
done
|
|
|
|
|
2023-01-20 17:42:14 +00:00
|
|
|
sed -i "s@https://install.determinate.systems/nix@$DEST_INSTALL_URL@" "$DEST/nix-installer.sh"
|
|
|
|
sed -i "s@https://install.determinate.systems/nix@https://install.determinate.systems/nix/rev/$GIT_ISH@" "$GIT_ISH/nix-installer.sh"
|
|
|
|
|
|
|
|
if is_tag; then
|
|
|
|
cp "$DEST/nix-installer.sh" ./nix-installer.sh
|
|
|
|
fi
|
|
|
|
|
2023-01-09 17:02:50 +00:00
|
|
|
# If any artifact already exists in S3 and the hash is the same, we don't want to reupload
|
2023-01-20 17:42:14 +00:00
|
|
|
check_reupload() {
|
|
|
|
dest="$1"
|
|
|
|
|
|
|
|
for file in $(find "$dest" -type f); do
|
|
|
|
artifact_path="$dest"/"$(basename "$artifact")"
|
|
|
|
md5="$(md5sum "$artifact" | cut -d' ' -f1)"
|
|
|
|
obj="$(aws s3api head-object --bucket "$AWS_BUCKET" --key "$artifact_path" || echo '{}')"
|
|
|
|
obj_md5="$(jq -r .ETag <<<"$obj" | jq -r)" # head-object call returns ETag quoted, so `jq -r` again to unquote it
|
|
|
|
|
|
|
|
if [[ "$md5" == "$obj_md5" ]]; then
|
|
|
|
echo "Artifact $artifact was already uploaded; exiting"
|
|
|
|
# If we already uploaded to a tag, that's probably bad
|
|
|
|
is_tag && exit 1 || exit 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
2023-01-09 17:02:50 +00:00
|
|
|
|
2023-01-20 17:42:14 +00:00
|
|
|
check_reupload "$DEST"
|
|
|
|
if ! is_tag; then
|
|
|
|
check_reupload "$GIT_ISH"
|
|
|
|
fi
|
|
|
|
|
|
|
|
aws s3 sync "$DEST"/ s3://"$AWS_BUCKET"/"$DEST"/ --acl public-read
|
|
|
|
if ! is_tag; then
|
|
|
|
aws s3 sync "$GIT_ISH"/ s3://"$AWS_BUCKET"/"$GIT_ISH"/ --acl public-read
|
|
|
|
fi
|