|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Check if required environment variables are set |
| 4 | +if [ -z "$APPLE_DEVELOPER_ID_APPLICATION" ] || [ -z "$APPLE_DEVELOPER_ID_INSTALLER" ] || [ -z "$APPLE_ID" ] || [ -z "$APPLE_ID_PASSWORD" ] || [ -z "$KEYCHAIN_PROFILE" ] || [ -z "$KEYCHAIN_NAME" ]; then |
| 5 | + echo "Error: One or more required environment variables are not set." |
| 6 | + echo "Required variables: APPLE_DEVELOPER_ID_APPLICATION, APPLE_DEVELOPER_ID_INSTALLER, APPLE_ID, APPLE_ID_PASSWORD, KEYCHAIN_PROFILE, KEYCHAIN_NAME" |
| 7 | + exit 1 |
| 8 | +fi |
| 9 | + |
| 10 | +# Set variables |
| 11 | +REPO_ROOT=$(git rev-parse --show-toplevel) |
| 12 | +BASE_DIR="/tmp/mw-agent-pkg" |
| 13 | +INSTALLER_DIR="$REPO_ROOT/build" |
| 14 | +ROOT_DIR="$BASE_DIR/root" |
| 15 | +SCRIPTS_DIR="$BASE_DIR/scripts" |
| 16 | +RESOURCE_DIR="$BASE_DIR/resources" |
| 17 | +IDENTIFIER="io.middleware.mw-agent" |
| 18 | +INSTALLER_NAME="mw-macos-agent-setup-${ARCH}.pkg" |
| 19 | +RELEASE_VERSION=$1 |
| 20 | + |
| 21 | +# Unlock the keychain |
| 22 | +security unlock-keychain -p "$APPLE_KEYCHAIN_PASSWORD" $KEYCHAIN_NAME |
| 23 | +# Prepare directories for the installer |
| 24 | +mkdir -p $BASE_DIR |
| 25 | +mkdir -p $RESOURCE_DIR |
| 26 | +mkdir -p $ROOT_DIR/Library/LaunchDaemons |
| 27 | +mkdir -p $ROOT_DIR/opt/mw-agent/ |
| 28 | +mkdir -p $ROOT_DIR/etc/mw-agent/ |
| 29 | +mkdir -p $SCRIPTS_DIR |
| 30 | +sudo cp $REPO_ROOT/build/mw-host-agent $ROOT_DIR/opt/mw-agent/mw-agent |
| 31 | +sudo cp $REPO_ROOT/package-tooling/agent-config.yaml.sample $ROOT_DIR/etc/mw-agent/agent-config.yaml |
| 32 | +sudo cp -r $REPO_ROOT/package-tooling/darwin/resources/* $RESOURCE_DIR |
| 33 | +sudo cp -r $REPO_ROOT/package-tooling/darwin/preinstall $SCRIPTS_DIR |
| 34 | +sudo chmod +x $SCRIPTS_DIR/preinstall |
| 35 | + |
| 36 | +sudo cp -r $REPO_ROOT/package-tooling/darwin/prompt_user.applescript $SCRIPTS_DIR |
| 37 | + |
| 38 | +sudo cp -r $REPO_ROOT/package-tooling/darwin/postinstall $SCRIPTS_DIR |
| 39 | +sudo chmod +x $SCRIPTS_DIR/postinstall |
| 40 | + |
| 41 | +sudo cp -r $REPO_ROOT/package-tooling/darwin/uninstall.sh $ROOT_DIR/opt/mw-agent/uninstall.sh |
| 42 | +sudo chmod +x $ROOT_DIR/opt/mw-agent/uninstall.sh |
| 43 | + |
| 44 | +sudo cp -r $REPO_ROOT/package-tooling/darwin/io.middleware.mw-agent.plist $ROOT_DIR/Library/LaunchDaemons/ |
| 45 | + |
| 46 | +sudo cp -r $REPO_ROOT/package-tooling/darwin/distribution.xml $BASE_DIR/distribution.xml |
| 47 | + |
| 48 | +echo "Signing the mw-agent binary with hardened runtime" |
| 49 | +sudo codesign --sign "$APPLE_DEVELOPER_ID_APPLICATION" --options runtime --timestamp "$ROOT_DIR/opt/mw-agent/mw-agent" |
| 50 | +# Create component package for the installer |
| 51 | +sudo pkgbuild --root $ROOT_DIR \ |
| 52 | + --identifier $IDENTIFIER \ |
| 53 | + --version $RELEASE_VERSION \ |
| 54 | + --install-location / \ |
| 55 | + --scripts $SCRIPTS_DIR \ |
| 56 | + $BASE_DIR/middleware_agent.pkg |
| 57 | + |
| 58 | +# Check if pkgbuild command failed |
| 59 | +if [ $? -ne 0 ]; then |
| 60 | + echo "Error: pkgbuild command failed" |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +# Create and sign the installer package |
| 65 | +echo "Signing the installer package" |
| 66 | +sudo productbuild --distribution $BASE_DIR/distribution.xml \ |
| 67 | + --package-path $BASE_DIR \ |
| 68 | + --resources $RESOURCE_DIR \ |
| 69 | + --sign "$APPLE_DEVELOPER_ID_INSTALLER" \ |
| 70 | + --keychain $KEYCHAIN_NAME \ |
| 71 | + $INSTALLER_DIR/$INSTALLER_NAME |
| 72 | + |
| 73 | +# Check if productbuild command failed |
| 74 | +if [ $? -ne 0 ]; then |
| 75 | + echo "Error: productbuild command failed" |
| 76 | + exit 1 |
| 77 | +fi |
| 78 | + |
| 79 | +echo "Notarizing the installer package, team id: $APPLE_DEVELOPER_TEAM_ID" |
| 80 | +xcrun notarytool store-credentials "$KEYCHAIN_PROFILE" --apple-id $APPLE_ID --password $APPLE_ID_PASSWORD --team-id "$APPLE_DEVELOPER_TEAM_ID" |
| 81 | + |
| 82 | +echo "Submitting the installer package for notarization" |
| 83 | +sudo xcrun notarytool submit $INSTALLER_DIR/$INSTALLER_NAME --keychain-profile "$KEYCHAIN_PROFILE" --wait |
| 84 | + |
| 85 | +# Check if notarytool command failed |
| 86 | +if [ $? -ne 0 ]; then |
| 87 | + echo "Error: notarytool command failed" |
| 88 | + exit 1 |
| 89 | +fi |
| 90 | + |
| 91 | +echo "Stapling the notarization ticket to the installer package" |
| 92 | +sudo xcrun stapler staple $INSTALLER_DIR/$INSTALLER_NAME |
| 93 | + |
| 94 | +# Check if stapler command failed |
| 95 | +if [ $? -ne 0 ]; then |
| 96 | + echo "Error: stapler command failed" |
| 97 | + exit 1 |
| 98 | +fi |
| 99 | +echo "Installer package created at $INSTALLER_DIR/$INSTALLER_NAME" |
0 commit comments