From c8288f26a086defe86d6a5449a72f798d4d96014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mence=20Lesn=C3=A9?= Date: Mon, 25 Mar 2024 12:17:26 +0100 Subject: [PATCH] fix: Linux agents are not deleted from server history --- src/docker/start.sh | 25 ++++++++++++++++++- .../templates/_helpers.tpl | 6 +---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/docker/start.sh b/src/docker/start.sh index d729d88e..b0531cf8 100644 --- a/src/docker/start.sh +++ b/src/docker/start.sh @@ -38,6 +38,23 @@ write_header() { echo -e "${lightcyan}➡️ $1${nocolor}" } +unregister() { + write_header "Unregister, removing agent from server..." + + # If the agent has some running jobs, the configuration removal process will fail ; so, give it some time to finish the job + while true; do + # If the agent is removed successfully, exit the loop + bash config.sh remove \ + --auth PAT \ + --token "$$AZP_TOKEN" \ + --unattended \ + && break + + echo "Retrying in 15 secs..." + sleep 15 + done +} + if [ -d "$AZP_CUSTOM_CERT_PEM" ] && [ "$(ls -A $AZP_CUSTOM_CERT_PEM)" ]; then write_header "Adding custom SSL certificates..." echo "Searching for *.crt in $AZP_CUSTOM_CERT_PEM" @@ -100,7 +117,13 @@ bash config.sh \ # See: https://stackoverflow.com/a/62183992/12732154 wait $! -print_header "Running agent..." +# Unregister on success +trap 'unregister; exit 0' EXIT +# Unregister on Ctrl+C +trap 'unregister; exit 130' INT +# Unregister on SIGTERM +trap 'unregister; exit 143' TERM + write_header "Running agent..." # Running it with the --once flag at the end will shut down the agent after the build is executed diff --git a/src/helm/azure-pipelines-agent/templates/_helpers.tpl b/src/helm/azure-pipelines-agent/templates/_helpers.tpl index e81f5fc7..3d172166 100644 --- a/src/helm/azure-pipelines-agent/templates/_helpers.tpl +++ b/src/helm/azure-pipelines-agent/templates/_helpers.tpl @@ -157,14 +157,10 @@ containers: # For security reasons, force clean the pipeline workspace at restart -- Sharing data bewteen pipelines is a security risk Remove-Item -Recurse -Force $Env:AZP_WORK; {{- end }} - {{- else }} + {{- else not .Values.pipelines.cache.volumeEnabled or not .Values.pipelines.tmpdir.volumeEnabled}} - bash - -c - | - bash config.sh \ - remove \ - --auth PAT \ - --token ${AZP_TOKEN}; {{- if not .Values.pipelines.cache.volumeEnabled }} # For security reasons, force clean the pipeline workspace at restart -- Sharing data bewteen pipelines is a security risk rm -rf ${AZP_WORK};