[2020-02] Bump msbuild to track mono-2019-12 (#19661)
[mono-project.git] / eng / common / internal-feed-operations.sh
blob5941ea28335869884828103ab1f9abee9d235503
1 #!/usr/bin/env bash
3 set -e
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
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'..."
16 local url="https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh"
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"
26 echo "Installing plugin..."
27 . "$installcredproviderPath"
29 echo "Deleting local copy of 'installcredprovider.sh'..."
30 rm installcredprovider.sh
32 if [ ! -d "$HOME/.nuget/plugins" ]; then
33 Write-PipelineTelemetryError -category 'Build' 'CredProvider plugin was not installed correctly!'
34 ExitWithExitCode 1
35 else
36 echo "CredProvider plugin was installed correctly!"
39 # Then, we set the 'VSS_NUGET_EXTERNAL_FEED_ENDPOINTS' environment variable to restore from the stable
40 # feeds successfully
42 local nugetConfigPath="$repo_root/NuGet.config"
44 if [ ! "$nugetConfigPath" ]; then
45 Write-PipelineTelemetryError -category 'Build' "NuGet.config file not found in repo's root!"
46 ExitWithExitCode 1
49 local endpoints='['
50 local nugetConfigPackageValues=`cat "$nugetConfigPath" | grep "key=\"darc-int-"`
51 local pattern="value=\"(.*)\""
53 for value in $nugetConfigPackageValues
55 if [[ $value =~ $pattern ]]; then
56 local endpoint="${BASH_REMATCH[1]}"
57 endpoints+="{\"endpoint\": \"$endpoint\", \"password\": \"$authToken\"},"
59 done
61 endpoints=${endpoints%?}
62 endpoints+=']'
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"}"
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"
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"
81 echo "Installing dotnet SDK version $dotnetSdkVersion to restore Arcade SDK..."
82 echo "<Project Sdk=\"Microsoft.DotNet.Arcade.Sdk\"/>" > "$restoreProjPath"
84 InstallDotNetSdk "$dotnetTempDir" "$dotnetSdkVersion"
86 local res=`$dotnetTempDir/dotnet restore $restoreProjPath`
87 echo "Arcade SDK restored!"
89 # Cleanup
90 if [ "$restoreProjPath" ]; then
91 rm "$restoreProjPath"
94 if [ "$dotnetTempDir" ]; then
95 rm -r $dotnetTempDir
99 source="${BASH_SOURCE[0]}"
100 operation=''
101 authToken=''
102 repoName=''
104 while [[ $# > 0 ]]; do
105 opt="$(echo "$1" | awk '{print tolower($0)}')"
106 case "$opt" in
107 --operation)
108 operation=$2
109 shift
111 --authtoken)
112 authToken=$2
113 shift
116 echo "Invalid argument: $1"
117 usage
118 exit 1
120 esac
122 shift
123 done
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 )"
134 . "$scriptroot/tools.sh"
136 if [ "$operation" = "setup" ]; then
137 SetupCredProvider $authToken
138 elif [ "$operation" = "install-restore" ]; then
139 InstallDotNetSdkAndRestoreArcade
140 else
141 echo "Unknown operation '$operation'!"