SetupNugetSources.sh 7.5 KB

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