|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# DEFAULTS |
| 5 | +PLUGIN_OS_CODENAME="${PLUGIN_OS_CODENAME:-bionic}" |
| 6 | + |
| 7 | +# Verify all variables are present |
| 8 | +if [[ -z $PLUGIN_GPG_KEY || -z $PLUGIN_GPG_PASS || -z $PLUGIN_REGION \ |
| 9 | + || -z $PLUGIN_REPO_NAME || -z $PLUGIN_ACL || -z $PLUGIN_PREFIX \ |
| 10 | + || -z $AWS_SECRET_ACCESS_KEY || -z $AWS_ACCESS_KEY_ID \ |
| 11 | + || -z $PLUGIN_DEB_PATH || -z $PLUGIN_OS_CODENAME ]]; then |
| 12 | + echo "ERROR: Environment Variable Missing!" |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +# Verify if its the first time publishing. Will need to know later. |
| 17 | +# Probably an easier way to do this check :) |
| 18 | +EXISTS=$(aws s3 ls s3://"$PLUGIN_REPO_NAME"/releases/dists/ --region "$PLUGIN_REGION" | grep "$PLUGIN_OS_CODENAME") || EXISTS_RET="false" |
| 19 | + |
| 20 | +# Sanity Check for later |
| 21 | +if [ "$EXISTS_RET" = "false" ]; then |
| 22 | + echo "First time uploading repo!" |
| 23 | +else |
| 24 | + echo "Repo Exists! Defaulting to publish update..." |
| 25 | +fi |
| 26 | + |
| 27 | +### APTLY SECTION |
| 28 | + |
| 29 | +# Move old config file to use in jq query |
| 30 | +mv ~/.aptly.conf ~/.aptly.conf.orig |
| 31 | + |
| 32 | +# Inject ENV Variables and save as .aptly.conf |
| 33 | +jq --arg region "$PLUGIN_REGION" --arg bucket "$PLUGIN_REPO_NAME" --arg acl "$PLUGIN_ACL" --arg prefix "$PLUGIN_PREFIX" '.S3PublishEndpoints[$bucket] = {"region":$region, "bucket":$bucket, "acl": $acl, "prefix": $prefix}' ~/.aptly.conf.orig > ~/.aptly.conf |
| 34 | + |
| 35 | +# If aptly repo DOESNT exist locally already |
| 36 | +if [ ! "$(aptly repo list | grep $PLUGIN_OS_CODENAME)" ]; then |
| 37 | + aptly repo create -distribution="$PLUGIN_OS_CODENAME" -component=main "release-$PLUGIN_OS_CODENAME" |
| 38 | +fi |
| 39 | + |
| 40 | +# If aptly mirror DOESNT exist locally already |
| 41 | +if [ ! "$(aptly mirror list | grep $PLUGIN_OS_CODENAME)" ] && [ ! "$EXISTS_RET" = "false" ] ; then |
| 42 | + aptly mirror create -ignore-signatures "local-repo-$PLUGIN_OS_CODENAME" https://"${PLUGIN_REPO_NAME}"/"${PLUGIN_PREFIX}"/ "${PLUGIN_OS_CODENAME}" main |
| 43 | +fi |
| 44 | + |
| 45 | +# When it's not the first time uploading. |
| 46 | +if [ ! "$EXISTS_RET" = "false" ]; then |
| 47 | + aptly mirror update -ignore-signatures "local-repo-$PLUGIN_OS_CODENAME" |
| 48 | + # Found an article that said using 'Name' will select all packages for us |
| 49 | + aptly repo import "local-repo-$PLUGIN_OS_CODENAME" "release-$PLUGIN_OS_CODENAME" Name |
| 50 | +fi |
| 51 | + |
| 52 | +# Add .debs to the local repo |
| 53 | +aptly repo add -force-replace "release-$PLUGIN_OS_CODENAME" "$PLUGIN_DEB_PATH"/*.deb |
| 54 | + |
| 55 | +# Publish to S3 |
| 56 | +if [ ! "$(aptly publish list | grep $PLUGIN_REPO_NAME | grep $PLUGIN_OS_CODENAME)" ]; then |
| 57 | + # If the repo is new |
| 58 | + aptly publish repo -batch -force-overwrite -passphrase="$PLUGIN_GPG_PASS" "release-$PLUGIN_OS_CODENAME" s3:"${PLUGIN_REPO_NAME}": |
| 59 | +else |
| 60 | + # If the repo exists |
| 61 | + aptly publish update -batch -force-overwrite -passphrase="$PLUGIN_GPG_PASS" "$PLUGIN_OS_CODENAME" s3:"${PLUGIN_REPO_NAME}": |
| 62 | +fi |
0 commit comments