SetupNugetSources.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. # - task: Bash@3
  16. # displayName: Setup Private Feeds Credentials
  17. # inputs:
  18. # filePath: $(Build.SourcesDirectory)/eng/common/SetupNugetSources.sh
  19. # arguments: $(Build.SourcesDirectory)/NuGet.config $Token
  20. # condition: ne(variables['Agent.OS'], 'Windows_NT')
  21. # env:
  22. # Token: $(dn-bot-dnceng-artifact-feeds-rw)
  23. ConfigFile=$1
  24. CredToken=$2
  25. NL='\n'
  26. TB=' '
  27. source="${BASH_SOURCE[0]}"
  28. # resolve $source until the file is no longer a symlink
  29. while [[ -h "$source" ]]; do
  30. scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
  31. source="$(readlink "$source")"
  32. # if $source was a relative symlink, we need to resolve it relative to the path where the
  33. # symlink file was located
  34. [[ $source != /* ]] && source="$scriptroot/$source"
  35. done
  36. scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
  37. . "$scriptroot/tools.sh"
  38. if [ ! -f "$ConfigFile" ]; then
  39. Write-PipelineTelemetryError -Category 'Build' "Error: Eng/common/SetupNugetSources.sh returned a non-zero exit code. Couldn't find the NuGet config file: $ConfigFile"
  40. ExitWithExitCode 1
  41. fi
  42. if [ -z "$CredToken" ]; then
  43. Write-PipelineTelemetryError -category 'Build' "Error: Eng/common/SetupNugetSources.sh returned a non-zero exit code. Please supply a valid PAT"
  44. ExitWithExitCode 1
  45. fi
  46. if [[ `uname -s` == "Darwin" ]]; then
  47. NL=$'\\\n'
  48. TB=''
  49. fi
  50. # Ensure there is a <packageSources>...</packageSources> section.
  51. grep -i "<packageSources>" $ConfigFile
  52. if [ "$?" != "0" ]; then
  53. echo "Adding <packageSources>...</packageSources> section."
  54. ConfigNodeHeader="<configuration>"
  55. PackageSourcesTemplate="${TB}<packageSources>${NL}${TB}</packageSources>"
  56. sed -i.bak "s|$ConfigNodeHeader|$ConfigNodeHeader${NL}$PackageSourcesTemplate|" NuGet.config
  57. fi
  58. # Ensure there is a <packageSourceCredentials>...</packageSourceCredentials> section.
  59. grep -i "<packageSourceCredentials>" $ConfigFile
  60. if [ "$?" != "0" ]; then
  61. echo "Adding <packageSourceCredentials>...</packageSourceCredentials> section."
  62. PackageSourcesNodeFooter="</packageSources>"
  63. PackageSourceCredentialsTemplate="${TB}<packageSourceCredentials>${NL}${TB}</packageSourceCredentials>"
  64. sed -i.bak "s|$PackageSourcesNodeFooter|$PackageSourcesNodeFooter${NL}$PackageSourceCredentialsTemplate|" NuGet.config
  65. fi
  66. PackageSources=()
  67. # Ensure dotnet3-internal and dotnet3-internal-transport are in the packageSources if the public dotnet3 feeds are present
  68. grep -i "<add key=\"dotnet3\"" $ConfigFile
  69. if [ "$?" == "0" ]; then
  70. grep -i "<add key=\"dotnet3-internal\">" $ConfigFile
  71. if [ "$?" != "0" ]; then
  72. echo "Adding dotnet3-internal to the packageSources."
  73. PackageSourcesNodeFooter="</packageSources>"
  74. PackageSourceTemplate="${TB}<add key=\"dotnet3-internal\" value=\"https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3-internal/nuget/v2\" />"
  75. sed -i.bak "s|$PackageSourcesNodeFooter|$PackageSourceTemplate${NL}$PackageSourcesNodeFooter|" $ConfigFile
  76. fi
  77. PackageSources+=('dotnet3-internal')
  78. grep -i "<add key=\"dotnet3-internal-transport\"" $ConfigFile
  79. if [ "$?" != "0" ]; then
  80. echo "Adding dotnet3-internal-transport to the packageSources."
  81. PackageSourcesNodeFooter="</packageSources>"
  82. PackageSourceTemplate="${TB}<add key=\"dotnet3-internal-transport\" value=\"https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3-internal-transport/nuget/v2\" />"
  83. sed -i.bak "s|$PackageSourcesNodeFooter|$PackageSourceTemplate${NL}$PackageSourcesNodeFooter|" $ConfigFile
  84. fi
  85. PackageSources+=('dotnet3-internal-transport')
  86. fi
  87. # Ensure dotnet3.1-internal and dotnet3.1-internal-transport are in the packageSources if the public dotnet3.1 feeds are present
  88. grep -i "<add key=\"dotnet3.1\"" $ConfigFile
  89. if [ "$?" == "0" ]; then
  90. grep -i "<add key=\"dotnet3.1-internal\"" $ConfigFile
  91. if [ "$?" != "0" ]; then
  92. echo "Adding dotnet3.1-internal to the packageSources."
  93. PackageSourcesNodeFooter="</packageSources>"
  94. PackageSourceTemplate="${TB}<add key=\"dotnet3.1-internal\" value=\"https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal/nuget/v2\" />"
  95. sed -i.bak "s|$PackageSourcesNodeFooter|$PackageSourceTemplate${NL}$PackageSourcesNodeFooter|" $ConfigFile
  96. fi
  97. PackageSources+=('dotnet3.1-internal')
  98. grep -i "<add key=\"dotnet3.1-internal-transport\">" $ConfigFile
  99. if [ "$?" != "0" ]; then
  100. echo "Adding dotnet3.1-internal-transport to the packageSources."
  101. PackageSourcesNodeFooter="</packageSources>"
  102. PackageSourceTemplate="${TB}<add key=\"dotnet3.1-internal-transport\" value=\"https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal-transport/nuget/v2\" />"
  103. sed -i.bak "s|$PackageSourcesNodeFooter|$PackageSourceTemplate${NL}$PackageSourcesNodeFooter|" $ConfigFile
  104. fi
  105. PackageSources+=('dotnet3.1-internal-transport')
  106. fi
  107. # I want things split line by line
  108. PrevIFS=$IFS
  109. IFS=$'\n'
  110. PackageSources+="$IFS"
  111. PackageSources+=$(grep -oh '"darc-int-[^"]*"' $ConfigFile | tr -d '"')
  112. IFS=$PrevIFS
  113. for FeedName in ${PackageSources[@]} ; do
  114. # Check if there is no existing credential for this FeedName
  115. grep -i "<$FeedName>" $ConfigFile
  116. if [ "$?" != "0" ]; then
  117. echo "Adding credentials for $FeedName."
  118. PackageSourceCredentialsNodeFooter="</packageSourceCredentials>"
  119. NewCredential="${TB}${TB}<$FeedName>${NL}<add key=\"Username\" value=\"dn-bot\" />${NL}<add key=\"ClearTextPassword\" value=\"$CredToken\" />${NL}</$FeedName>"
  120. sed -i.bak "s|$PackageSourceCredentialsNodeFooter|$NewCredential${NL}$PackageSourceCredentialsNodeFooter|" $ConfigFile
  121. fi
  122. done