|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Sets VSS_NUGET_EXTERNAL_FEED_ENDPOINTS based on the "darc-int-*" feeds defined in NuGet.config. This is needed |
| 6 | +# in build agents by CredProvider to authenticate the restore requests to internal feeds as specified in |
| 7 | +# https://github.com/microsoft/artifacts-credprovider/blob/0f53327cd12fd893d8627d7b08a2171bf5852a41/README.md#environment-variables. |
| 8 | +# This should ONLY be called from identified internal builds |
| 9 | +function SetupCredProvider { |
| 10 | + local authToken=$1 |
| 11 | + |
| 12 | + # Install the Cred Provider NuGet plugin |
| 13 | + echo "Setting up Cred Provider NuGet plugin in the agent..."... |
| 14 | + echo "Getting 'installcredprovider.ps1' from 'https://github.com/microsoft/artifacts-credprovider'..." |
| 15 | + |
| 16 | + local url="https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh" |
| 17 | + |
| 18 | + echo "Writing the contents of 'installcredprovider.ps1' locally..." |
| 19 | + local installcredproviderPath="installcredprovider.sh" |
| 20 | + if command -v curl > /dev/null; then |
| 21 | + curl $url > "$installcredproviderPath" |
| 22 | + else |
| 23 | + wget -q -O "$installcredproviderPath" "$url" |
| 24 | + fi |
| 25 | + |
| 26 | + echo "Installing plugin..." |
| 27 | + . "$installcredproviderPath" |
| 28 | + |
| 29 | + echo "Deleting local copy of 'installcredprovider.sh'..." |
| 30 | + rm installcredprovider.sh |
| 31 | + |
| 32 | + if [ ! -d "$HOME/.nuget/plugins" ]; then |
| 33 | + echo "CredProvider plugin was not installed correctly!" |
| 34 | + ExitWithExitCode 1 |
| 35 | + else |
| 36 | + echo "CredProvider plugin was installed correctly!" |
| 37 | + fi |
| 38 | + |
| 39 | + # Then, we set the 'VSS_NUGET_EXTERNAL_FEED_ENDPOINTS' environment variable to restore from the stable |
| 40 | + # feeds successfully |
| 41 | + |
| 42 | + local nugetConfigPath="$repo_root/NuGet.config" |
| 43 | + |
| 44 | + if [ ! "$nugetConfigPath" ]; then |
| 45 | + echo "NuGet.config file not found in repo's root!" |
| 46 | + ExitWithExitCode 1 |
| 47 | + fi |
| 48 | + |
| 49 | + local endpoints='[' |
| 50 | + local nugetConfigPackageValues=`cat "$nugetConfigPath" | grep "key=\"darc-int-"` |
| 51 | + local pattern="value=\"(.*)\"" |
| 52 | + |
| 53 | + for value in $nugetConfigPackageValues |
| 54 | + do |
| 55 | + if [[ $value =~ $pattern ]]; then |
| 56 | + local endpoint="${BASH_REMATCH[1]}" |
| 57 | + endpoints+="{\"endpoint\": \"$endpoint\", \"password\": \"$authToken\"}," |
| 58 | + fi |
| 59 | + done |
| 60 | + |
| 61 | + endpoints=${endpoints%?} |
| 62 | + endpoints+=']' |
| 63 | + |
| 64 | + if [ ${#endpoints} -gt 2 ]; then |
| 65 | + # Create the JSON object. It should look like '{"endpointCredentials": [{"endpoint":"http://example.index.json", "username":"optional", "password":"accesstoken"}]}' |
| 66 | + local endpointCredentials="{\"endpointCredentials\": "$endpoints"}" |
| 67 | + |
| 68 | + echo "##vso[task.setvariable variable=VSS_NUGET_EXTERNAL_FEED_ENDPOINTS]$endpointCredentials" |
| 69 | + echo "##vso[task.setvariable variable=NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED]False" |
| 70 | + else |
| 71 | + echo "No internal endpoints found in NuGet.config" |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +# Workaround for https://github.com/microsoft/msbuild/issues/4430 |
| 76 | +function InstallDotNetSdkAndRestoreArcade { |
| 77 | + local dotnetTempDir="$repo_root/dotnet" |
| 78 | + local dotnetSdkVersion="2.1.507" # After experimentation we know this version works when restoring the SDK (compared to 3.0.*) |
| 79 | + local restoreProjPath="$repo_root/eng/common/restore.proj" |
| 80 | + |
| 81 | + echo "Installing dotnet SDK version $dotnetSdkVersion to restore Arcade SDK..." |
| 82 | + echo "<Project Sdk=\"Microsoft.DotNet.Arcade.Sdk\"/>" > "$restoreProjPath" |
| 83 | + |
| 84 | + InstallDotNetSdk "$dotnetTempDir" "$dotnetSdkVersion" |
| 85 | + |
| 86 | + local res=`$dotnetTempDir/dotnet restore $restoreProjPath` |
| 87 | + echo "Arcade SDK restored!" |
| 88 | + |
| 89 | + # Cleanup |
| 90 | + if [ "$restoreProjPath" ]; then |
| 91 | + rm "$restoreProjPath" |
| 92 | + fi |
| 93 | + |
| 94 | + if [ "$dotnetTempDir" ]; then |
| 95 | + rm -r $dotnetTempDir |
| 96 | + fi |
| 97 | +} |
| 98 | + |
| 99 | +source="${BASH_SOURCE[0]}" |
| 100 | +operation='' |
| 101 | +authToken='' |
| 102 | +repoName='' |
| 103 | + |
| 104 | +while [[ $# > 0 ]]; do |
| 105 | + opt="$(echo "$1" | awk '{print tolower($0)}')" |
| 106 | + case "$opt" in |
| 107 | + --operation) |
| 108 | + operation=$2 |
| 109 | + shift |
| 110 | + ;; |
| 111 | + --authtoken) |
| 112 | + authToken=$2 |
| 113 | + shift |
| 114 | + ;; |
| 115 | + *) |
| 116 | + echo "Invalid argument: $1" |
| 117 | + usage |
| 118 | + exit 1 |
| 119 | + ;; |
| 120 | + esac |
| 121 | + |
| 122 | + shift |
| 123 | +done |
| 124 | + |
| 125 | +while [[ -h "$source" ]]; do |
| 126 | + scriptroot="$( cd -P "$( dirname "$source" )" && pwd )" |
| 127 | + source="$(readlink "$source")" |
| 128 | + # if $source was a relative symlink, we need to resolve it relative to the path where the |
| 129 | + # symlink file was located |
| 130 | + [[ $source != /* ]] && source="$scriptroot/$source" |
| 131 | +done |
| 132 | +scriptroot="$( cd -P "$( dirname "$source" )" && pwd )" |
| 133 | + |
| 134 | +. "$scriptroot/tools.sh" |
| 135 | + |
| 136 | +if [ "$operation" = "setup" ]; then |
| 137 | + SetupCredProvider $authToken |
| 138 | +elif [ "$operation" = "install-restore" ]; then |
| 139 | + InstallDotNetSdkAndRestoreArcade |
| 140 | +else |
| 141 | + echo "Unknown operation '$operation'!" |
| 142 | +fi |
0 commit comments