Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
craigthackerx committed Feb 21, 2024
1 parent 29e8165 commit bb3bb67
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
5 changes: 4 additions & 1 deletion containers/ubuntu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ ARG DEBIAN_FRONTEND=noninteractive
ARG AZP_URL=https://dev.azure.com/Example
ARG AZP_TOKEN=ExamplePatToken
ARG AZP_AGENT_NAME=Example
ARG AZP_POOL=PoolName
ARG AZP_POOL=Default
ARG AZP_WORK=_work
ARG AZP_DIRECTORY=/home/$NORMAL_USER

#Set the environment with the CLI-passed arguements
ENV AZP_URL ${AZP_URL}
ENV AZP_TOKEN ${AZP_TOKEN}
ENV AZP_AGENT_NAME ${AZP_AGENT_NAME}
ENV AZP_POOL ${AZP_POOL}
ENV AZP_WORK ${AZP_WORK}
ENV AZP_DIRECTORY ${AZP_DIRECTORY}

ENV NORMAL_USER ${NORMAL_USER}
ENV DEBIAN_FRONTEND=noninteractive
Expand Down Expand Up @@ -58,6 +60,7 @@ RUN useradd -ms /bin/bash ${NORMAL_USER} && \
libffi-dev \
libicu-dev \
make \
nano \
software-properties-common \
libsqlite3-dev \
libssl-dev \
Expand Down
45 changes: 45 additions & 0 deletions containers/ubuntu/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

varCheckList=(
'LANG'
'JAVA_HOME'
'ANT_HOME'
'M2_HOME'
'ANDROID_HOME'
'GRADLE_HOME'
'NVM_BIN'
'NVM_PATH'
'VSTS_HTTP_PROXY'
'VSTS_HTTP_PROXY_USERNAME'
'VSTS_HTTP_PROXY_PASSWORD'
'LD_LIBRARY_PATH'
'PERL5LIB'
'AGENT_TOOLSDIRECTORY'
)

envContents=""

if [ -f ".env" ]; then
envContents=$(cat .env)
else
touch .env
fi

function writeVar()
{
local checkVar="$1"
local checkDelim="${1}="
if test "${envContents#*$checkDelim}" = "$envContents"
then
if [ -z "${!checkVar}" ]; then
echo "${checkVar}=${!checkVar}">>.env
fi
fi
}

echo "$PATH">.path

for var_name in "${varCheckList[@]}"
do
writeVar "${var_name}"
done
46 changes: 25 additions & 21 deletions containers/ubuntu/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
set -e

print_header() {
lightcyan='\033[1;36m'
nocolor='\033[0m'
local lightcyan='\033[1;36m'
local nocolor='\033[0m'
echo -e "${lightcyan}$1${nocolor}"
}

Expand All @@ -19,16 +19,14 @@ PACKAGE="tar.gz"
if [ "$(command -v jq)" ] && [ "$(command -v curl)" ] && [ "$(command -v sed)" ]; then
print_header "0. Checking jq, curl and sed are installed..."

else
echo "You do not have the needed packages to run the script, please install them" && exit 1
else
echo "You do not have the needed packages to run the script, please install them" && exit 1

fi

azdoLatestAgentVersion="$(curl --silent "https://api.github.com/repos/${USER}/${REPO}/releases/latest" | jq -r .tag_name)" && \

strippedTagAzDoAgentVersion="$(echo "${azdoLatestAgentVersion}" | sed 's/v//')" && \

AZP_AGENTPACKAGE_URL="https://vstsagentpackage.azureedge.net/agent/${strippedTagAzDoAgentVersion}/vsts-agent-${OS}-${ARCH}-${strippedTagAzDoAgentVersion}.${PACKAGE}"
azdoLatestAgentVersion="$(curl --silent "https://api.github.com/repos/${USER}/${REPO}/releases/latest" | jq -r .tag_name)" &&
strippedTagAzDoAgentVersion="${azdoLatestAgentVersion//v/}" &&
AZP_AGENTPACKAGE_URL="https://vstsagentpackage.azureedge.net/agent/${strippedTagAzDoAgentVersion}/vsts-agent-${OS}-${ARCH}-${strippedTagAzDoAgentVersion}.${PACKAGE}"

if [ -z "${AZP_URL}" ]; then
echo 1>&2 "error: missing AZP_URL environment variable"
Expand All @@ -41,8 +39,8 @@ if [ -z "${AZP_TOKEN_FILE}" ]; then
exit 1
fi

AZP_TOKEN_FILE=/azp/.token
echo -n "${AZP_TOKEN}" > "${AZP_TOKEN_FILE}"
AZP_TOKEN_FILE="${AZP_DIRECTORY}/.token"
echo -n "${AZP_TOKEN}" >"${AZP_TOKEN_FILE}"
fi

unset AZP_TOKEN
Expand All @@ -51,9 +49,9 @@ if [ -n "${AZP_WORK}" ]; then
mkdir -p "${AZP_WORK}"
fi

rm -rf /azp/agent
mkdir /azp/agent
cd /azp/agent
rm -rf "${AZP_DIRECTORY}/agent"
mkdir "${AZP_DIRECTORY}/agent"
cd "${AZP_DIRECTORY}/agent"

export AGENT_ALLOW_RUNASROOT="1"

Expand All @@ -78,8 +76,8 @@ AZP_AGENT_RESPONSE=$(curl -LsS \
"${AZP_URL}/_apis/distributedtask/packages/agent?platform=linux-x64")

if echo "${AZP_AGENT_RESPONSE}" | jq . >/dev/null 2>&1; then
AZP_AGENTPACKAGE_URL="$(echo "${AZP_AGENT_RESPONSE}" \
| jq -r '.value | map([.version.major,.version.minor,.version.patch,.downloadUrl]) | sort | .[length-1] | .[3]')"
AZP_AGENTPACKAGE_URL="$(echo "${AZP_AGENT_RESPONSE}" |
jq -r '.value | map([.version.major,.version.minor,.version.patch,.downloadUrl]) | sort | .[length-1] | .[3]')"
fi

if [ -z "${AZP_AGENTPACKAGE_URL}" ] || [ "${AZP_AGENTPACKAGE_URL}" == "null" ]; then
Expand All @@ -89,11 +87,15 @@ fi

print_header "2. Downloading and installing Azure Pipelines agent..."

curl -LsS "${AZP_AGENTPACKAGE_URL}" | tar -xz & wait $!
curl -LsS "${AZP_AGENTPACKAGE_URL}" | tar -xz &
wait $!


print_header "3. Sourcing env.sh"

source ./env.sh

print_header "3. Configuring Azure Pipelines agent..."
print_header "4. Configuring Azure Pipelines agent..."

./config.sh --unattended \
--agent "${AZP_AGENT_NAME:-$(hostname)}" \
Expand All @@ -103,13 +105,15 @@ print_header "3. Configuring Azure Pipelines agent..."
--pool "${AZP_POOL}" \
--work "${AZP_WORK:-_work}" \
--replace \
--acceptTeeEula & wait $!
--acceptTeeEula &
wait $!

print_header "4. Running Azure Pipelines agent..."
print_header "5. Running Azure Pipelines agent..."

trap 'cleanup; exit 130' INT
trap 'cleanup; exit 143' TERM

# To be aware of TERM and INT signals call run.sh
# Running it with the --once flag at the end will shut down the agent after the build is executed
./run.sh & wait $!
./run.sh &
wait $!

0 comments on commit bb3bb67

Please sign in to comment.