Skip to content

Instantly share code, notes, and snippets.

@VladRassokhin
Created June 25, 2022 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VladRassokhin/e692e62859ab5bef669dd1b2b917d854 to your computer and use it in GitHub Desktop.
Save VladRassokhin/e692e62859ab5bef669dd1b2b917d854 to your computer and use it in GitHub Desktop.
Cleanup TeamCity composite build configurations xml configs
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" method="xml" indent="yes" encoding="UTF-8"
cdata-section-elements="param"
xalan:indent-amount="2" xmlns:xalan="http://xml.apache.org/xalan"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- drop unnecessary options -->
<xsl:template
match="/build-type/settings/options/option[
../option[@name='buildConfigurationType' and @value='COMPOSITE']
and (@name = 'artifactRules' or @name = 'checkoutMode' or @name = 'cleanBuild')
]"/>
<!-- drop agent requirements -->
<xsl:template
match="/build-type/settings/requirements[
../options/option[@name='buildConfigurationType' and @value='COMPOSITE']
]">
<requirements/>
</xsl:template>
<!-- drop build steps -->
<xsl:template
match="/build-type/settings/build-runners[
../options/option[@name='buildConfigurationType' and @value='COMPOSITE']
]">
<build-runners/>
</xsl:template>
<!-- drop some extensions and failure conditions -->
<xsl:template
match="/build-type/settings/build-extensions/extension[
../../options/option[@name='buildConfigurationType' and @value='COMPOSITE']
and (@type='jetbrains.agent.free.space' or @type='swabra' or @type='BuildFailureOnMessage')
]">
</xsl:template>
</xsl:stylesheet>
#!/bin/bash
set -euo pipefail
if [ -z "${1:-}" ]; then
echo "Usage: $0 <scope>"
exit 1
fi
scope="$1"
if ! which xalan &>/dev/null; then
echo "xalan required to operate"
exit 2
fi
grep '<option name="buildConfigurationType" value="COMPOSITE" />' .teamcity -r -l --include '*.xml' | grep "$scope" | sort >composites.list
# shellcheck disable=SC2046
grep -e '"artifactRules"' -e '"checkoutMode"' -e '"cleanBuild"' \
-e '"jetbrains.agent.free.space"' -e '"swabra"' -e '"BuildFailureOnMessage"' \
-e '<build-runners>' -e '<requirements>' \
-l $(cat composites.list) | grep "$scope" | sort >composites-to-cleanup.list
while IFS= read -r f; do
echo "Processing $f"
cp "$f" "$f.bak"
xalan "$f.bak" composite-cleanup.xslt | \
sed -e 's~/>~ />~' -e 's~&#13;~\&#xD;~g' -e 's~&#10;~\&#xA;~g' -e 's~> +<!\[CDATA~><![CDATA~' -e 's~\]\]> +</param>~]]></param>~' >"$f"
echo >>"$f"
rm "$f.bak"
done < <(grep -v '^ *#' <composites-to-cleanup.list)
rm composites.list composites-to-cleanup.list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment