Skip to content

Instantly share code, notes, and snippets.

@subudear
Last active November 21, 2022 12:56
Show Gist options
  • Select an option

  • Save subudear/2de86423326f6dfd7494de9dee110e4a to your computer and use it in GitHub Desktop.

Select an option

Save subudear/2de86423326f6dfd7494de9dee110e4a to your computer and use it in GitHub Desktop.
Build agent file
#!/bin/bash
set -e
export DEBIAN_FRONTEND=noninteractive
echo $1, $2, $3, $4, $5, $6
if [ ! -e $6/.configure ]; then
touch $6/.configure
fi
if [ ! -e $6/.token ]; then
touch $6/.token
fi
# Install latest release of .NET Core
echo "Installing .NET"
sudo apt-get -y install libunwind8 libcurl3
if [ $(dpkg-query -W -f='${Status}' jq 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
sudo apt-get install -y jq
fi
if [ -e $6/agent -a ! -e $6/.configure ]; then
trap 'kill -SIGINT $!; exit 130' INT
trap 'kill -SIGTERM $!; exit 143' TERM
$6/agent/bin/Agent.Listener run & wait $!
exit $?
fi
if [ -z "$1" ]; then
echo 1>&2 error: missing VSTS_ACCOUNT environment variable
exit 1
fi
if [ -z "$VSTS_TOKEN_FILE" ]; then
if [ -z "$2" ]; then
echo 1>&2 error: missing VSTS_TOKEN environment variable
exit 1
fi
VSTS_TOKEN_FILE=$6/.token
echo -n $2 > "$VSTS_TOKEN_FILE"
fi
if [ -n "$4" ]; then
export VSTS_AGENT="$(eval echo $4)"
fi
if [ -n "$6/_work" ]; then
export VSTS_WORK="$(eval echo $6/_work)"
mkdir -p "$6/_work"
fi
#touch /vsts/.configure
rm -rf $6/agent
mkdir $6/agent
cd $6/agent
cleanup() {
if [ -e config.sh ]; then
./bin/Agent.Listener remove --unattended \
--auth PAT \
--token $(cat "$VSTS_TOKEN_FILE")
fi
}
trap 'cleanup; exit 130' INT
trap 'cleanup; exit 143' TERM
echo Determining matching VSTS agent...
VSTS_AGENT_RESPONSE=$(curl -LsS \
-u user:$(cat "$VSTS_TOKEN_FILE") \
-H 'Accept:application/json;api-version=3.0-preview' \
"https://$1.visualstudio.com/_apis/distributedtask/packages/agent?platform=linux-x64")
if echo "$VSTS_AGENT_RESPONSE" | jq . >/dev/null 2>&1; then
VSTS_AGENT_URL=$(echo "$VSTS_AGENT_RESPONSE" \
| jq -r '.value | map([.version.major,.version.minor,.version.patch,.downloadUrl]) | sort | .[length-1] | .[3]')
fi
if [ -z "$VSTS_AGENT_URL" -o "$VSTS_AGENT_URL" == "null" ]; then
echo 1>&2 error: could not determine a matching VSTS agent - check that account \'$1\' is correct and the token is valid for that account
exit 1
fi
echo Downloading and installing VSTS agent...
curl -LsS $VSTS_AGENT_URL | tar -xz --no-same-owner & wait $!
#source ./env.sh
./bin/Agent.Listener configure --unattended \
--agent "$4" \
--url "https://$1.visualstudio.com" \
--auth PAT \
--token $(cat "$VSTS_TOKEN_FILE") \
--pool "$3" \
--work "$6/_work" \
--replace & wait $!
#./bin/Agent.Listener run & wait $!
sudo ./svc.sh start &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment