[2020-02] Bump msbuild to track mono-2019-12 (#19661)
[mono-project.git] / eng / common / SetupNugetSources.sh
blob7d6fef27fe495e2c3470b81c9d872e8f470f6c42
1 #!/usr/bin/env bash
3 # This file is a temporary workaround for internal builds to be able to restore from private AzDO feeds.
4 # This file should be removed as part of this issue: https://github.com/dotnet/arcade/issues/4080
6 # What the script does is iterate over all package sources in the pointed NuGet.config and add a credential entry
7 # under <packageSourceCredentials> for each Maestro's managed private feed. Two additional credential
8 # entries are also added for the two private static internal feeds: dotnet3-internal and dotnet3-internal-transport.
10 # This script needs to be called in every job that will restore packages and which the base repo has
11 # private AzDO feeds in the NuGet.config.
13 # See example YAML call for this script below. Note the use of the variable `$(dn-bot-dnceng-artifact-feeds-rw)`
14 # from the AzureDevOps-Artifact-Feeds-Pats variable group.
16 # - task: Bash@3
17 # displayName: Setup Private Feeds Credentials
18 # inputs:
19 # filePath: $(Build.SourcesDirectory)/eng/common/SetupNugetSources.sh
20 # arguments: $(Build.SourcesDirectory)/NuGet.config $Token
21 # condition: ne(variables['Agent.OS'], 'Windows_NT')
22 # env:
23 # Token: $(dn-bot-dnceng-artifact-feeds-rw)
25 ConfigFile=$1
26 CredToken=$2
27 NL='\n'
28 TB=' '
30 source="${BASH_SOURCE[0]}"
32 # resolve $source until the file is no longer a symlink
33 while [[ -h "$source" ]]; do
34 scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
35 source="$(readlink "$source")"
36 # if $source was a relative symlink, we need to resolve it relative to the path where the
37 # symlink file was located
38 [[ $source != /* ]] && source="$scriptroot/$source"
39 done
40 scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
42 . "$scriptroot/tools.sh"
44 if [ ! -f "$ConfigFile" ]; then
45 Write-PipelineTelemetryError -Category 'Build' "Error: Eng/common/SetupNugetSources.sh returned a non-zero exit code. Couldn't find the NuGet config file: $ConfigFile"
46 ExitWithExitCode 1
49 if [ -z "$CredToken" ]; then
50 Write-PipelineTelemetryError -category 'Build' "Error: Eng/common/SetupNugetSources.sh returned a non-zero exit code. Please supply a valid PAT"
51 ExitWithExitCode 1
54 if [[ `uname -s` == "Darwin" ]]; then
55 NL=$'\\\n'
56 TB=''
59 # Ensure there is a <packageSources>...</packageSources> section.
60 grep -i "<packageSources>" $ConfigFile
61 if [ "$?" != "0" ]; then
62 echo "Adding <packageSources>...</packageSources> section."
63 ConfigNodeHeader="<configuration>"
64 PackageSourcesTemplate="${TB}<packageSources>${NL}${TB}</packageSources>"
66 sed -i.bak "s|$ConfigNodeHeader|$ConfigNodeHeader${NL}$PackageSourcesTemplate|" NuGet.config
69 # Ensure there is a <packageSourceCredentials>...</packageSourceCredentials> section.
70 grep -i "<packageSourceCredentials>" $ConfigFile
71 if [ "$?" != "0" ]; then
72 echo "Adding <packageSourceCredentials>...</packageSourceCredentials> section."
74 PackageSourcesNodeFooter="</packageSources>"
75 PackageSourceCredentialsTemplate="${TB}<packageSourceCredentials>${NL}${TB}</packageSourceCredentials>"
77 sed -i.bak "s|$PackageSourcesNodeFooter|$PackageSourcesNodeFooter${NL}$PackageSourceCredentialsTemplate|" NuGet.config
80 PackageSources=()
82 # Ensure dotnet3-internal and dotnet3-internal-transport are in the packageSources if the public dotnet3 feeds are present
83 grep -i "<add key=\"dotnet3\"" $ConfigFile
85 if [ "$?" == "0" ]; then
86 grep -i "<add key=\"dotnet3-internal\">" $ConfigFile
87 if [ "$?" != "0" ]; then
88 echo "Adding dotnet3-internal to the packageSources."
89 PackageSourcesNodeFooter="</packageSources>"
90 PackageSourceTemplate="${TB}<add key=\"dotnet3-internal\" value=\"https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3-internal/nuget/v2\" />"
92 sed -i.bak "s|$PackageSourcesNodeFooter|$PackageSourceTemplate${NL}$PackageSourcesNodeFooter|" $ConfigFile
94 PackageSources+=('dotnet3-internal')
96 grep -i "<add key=\"dotnet3-internal-transport\"" $ConfigFile
97 if [ "$?" != "0" ]; then
98 echo "Adding dotnet3-internal-transport to the packageSources."
99 PackageSourcesNodeFooter="</packageSources>"
100 PackageSourceTemplate="${TB}<add key=\"dotnet3-internal-transport\" value=\"https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3-internal-transport/nuget/v2\" />"
102 sed -i.bak "s|$PackageSourcesNodeFooter|$PackageSourceTemplate${NL}$PackageSourcesNodeFooter|" $ConfigFile
104 PackageSources+=('dotnet3-internal-transport')
107 # Ensure dotnet3.1-internal and dotnet3.1-internal-transport are in the packageSources if the public dotnet3.1 feeds are present
108 grep -i "<add key=\"dotnet3.1\"" $ConfigFile
109 if [ "$?" == "0" ]; then
110 grep -i "<add key=\"dotnet3.1-internal\"" $ConfigFile
111 if [ "$?" != "0" ]; then
112 echo "Adding dotnet3.1-internal to the packageSources."
113 PackageSourcesNodeFooter="</packageSources>"
114 PackageSourceTemplate="${TB}<add key=\"dotnet3.1-internal\" value=\"https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal/nuget/v2\" />"
116 sed -i.bak "s|$PackageSourcesNodeFooter|$PackageSourceTemplate${NL}$PackageSourcesNodeFooter|" $ConfigFile
118 PackageSources+=('dotnet3.1-internal')
120 grep -i "<add key=\"dotnet3.1-internal-transport\">" $ConfigFile
121 if [ "$?" != "0" ]; then
122 echo "Adding dotnet3.1-internal-transport to the packageSources."
123 PackageSourcesNodeFooter="</packageSources>"
124 PackageSourceTemplate="${TB}<add key=\"dotnet3.1-internal-transport\" value=\"https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal-transport/nuget/v2\" />"
126 sed -i.bak "s|$PackageSourcesNodeFooter|$PackageSourceTemplate${NL}$PackageSourcesNodeFooter|" $ConfigFile
128 PackageSources+=('dotnet3.1-internal-transport')
131 # I want things split line by line
132 PrevIFS=$IFS
133 IFS=$'\n'
134 PackageSources+="$IFS"
135 PackageSources+=$(grep -oh '"darc-int-[^"]*"' $ConfigFile | tr -d '"')
136 IFS=$PrevIFS
138 for FeedName in ${PackageSources[@]} ; do
139 # Check if there is no existing credential for this FeedName
140 grep -i "<$FeedName>" $ConfigFile
141 if [ "$?" != "0" ]; then
142 echo "Adding credentials for $FeedName."
144 PackageSourceCredentialsNodeFooter="</packageSourceCredentials>"
145 NewCredential="${TB}${TB}<$FeedName>${NL}<add key=\"Username\" value=\"dn-bot\" />${NL}<add key=\"ClearTextPassword\" value=\"$CredToken\" />${NL}</$FeedName>"
147 sed -i.bak "s|$PackageSourceCredentialsNodeFooter|$NewCredential${NL}$PackageSourceCredentialsNodeFooter|" $ConfigFile
149 done