Gitlab yml cannot retreive password when create the new branch - gitlab

I create the gitlab ci yml as below:
before_script:
- export GITLAB_PACKAGE_REGISTRY_USERNAME="DeployUser"
- export GITLAB_PACKAGE_REGISTRY_PASSWORD=$DeployPassword
- dotnet restore ${PROJECT} --packages ./.nuget/ --verbosity quiet
Build and Test:
stage: build and test
script:
- dotnet build ${PROJECT} --no-restore -c Release --verbosity quiet -o out
Release
when: always
only:
- develop
- /^Production/[0-9]+(?:.[0-9]+)+$/
and the nuget.config as below
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="gitlab" value="https://gitlab.com/api/v4/projects/1234567/packages/nuget/index.json" />
</packageSources>
<packageSourceCredentials>
<gitlab>
<add key="Username" value="%GITLAB_PACKAGE_REGISTRY_USERNAME%" />
<add key="ClearTextPassword" value="%GITLAB_PACKAGE_REGISTRY_PASSWORD%" />
</gitlab>
</packageSourceCredentials>
</configuration>
When I push source code to gitlab into develop branch, it's all green and sucess, but when I create the new branch name Production/1.0 from develop
It throw below error
$ export GITLAB_PACKAGE_REGISTRY_USERNAME="DeployUser" $ export
GITLAB_PACKAGE_REGISTRY_PASSWORD=$DeployPassword $ dotnet restore
${PROJECT} --packages ./.nuget/ --verbosity quiet
/usr/share/dotnet/sdk/5.0.408/NuGet.targets(565,5): error : Value
cannot be null or empty string. (Parameter 'password')
[/builds/codefactory/myproject.csproj]
can I know the problem inside ?
Thank you

Environment variables are configured by your administrator to be protected by default.
https://gitlab.ipaidthat.io/help/ci/variables/index#protect-a-cicd-variable
Can you ensure your variable is not protected, or your branch is a protected branche?

Related

Telerik Nuget package restore in Azure build pipeline fails

I'm attempting to pull Nuget packages from Telerik's Nuget repository into an Azure build pipeline, however, none of the pipeline configuration attempts I've made seem to work. I either receive an error stating my nuget.config is not formatted correctly or a 401 error when connecting to the repository.
The below configuration section is from my build definition. I've tried using NuGetCommand as well as NuGetRestore:
- task: NuGetToolInstaller#1
inputs:
versionSpec: '5.0.2'
- task: NuGetAuthenticate#1
inputs:
nuGetServiceConnections: 'Telerik_v3'
- task: NuGetCommand#2
inputs:
command: 'restore'
restoreSolution: '**/*.sln'
feedsToUse: 'config'
nugetConfigPath: './XXXXXXX/nuget.config'
#'$(System.DefaultWorkingDirectory)/XXXXXXX/NuGet.config'
externalFeedCredentials: 'Telerik_v3'
# - task: NuGetRestore#1
# inputs:
# solution: '**/*.sln'
# selectOrConfig: 'config'
# nugetConfigPath: './XXXXXXX/nuget.config'
Here is my nuget.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="NuGet" value="https://api.nuget.org/v3/index.json" protocolVersion="3"/>
<add key="Telerik_NuGet" value="https://nuget.telerik.com/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
Here is the error I receive:
NuGet.Protocol.Core.Types.FatalProtocolException: Unable to load the service index for source https://nuget.telerik.com/v3/index.json. ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
Tweaking the configuration slightly I get this error:
##[error]The nuget command failed with exit code(1) and error(NuGet.Configuration.NuGetConfigurationException: NuGet.Config is not valid XML. Path: 'D:\a\1\Nuget\tempNuGet_966.config'. ---> System.Xml.XmlException: An error occurred while parsing EntityName. Line 10, position 46.
I based my build tasks on the NuGetCommandv2 and NuGetRestore v1 documentation as well as what I found on Telerik's site.
When we want to use private nuget packages like Telerik we need to specify credentials in Nuget.config_
Connection name
Feed URL
Username
Password
NOTE:
In order to add a reference to this NuGet package to your project, firstly need to add Telerik credentials into Visual Studio.
example:
dotnet nuget update source "Telerik" --source "https://nuget.telerik.com/v3/index.json" --configfile "nuget.config" --username '*************' --password '***********' --store-password-in-clear-text
Here I found one reference github by Lance McCarthy

Azure build pipeline unable to restore NuGet packages from private feed when using NuGet.Config file from solution

I am trying to build a C# project in DevOps build pipeline. The project is using a single Azure Artifacts private feed. The NuGet.config is as given by the Azure portal from the Nuget tab of connect feed page. The file is at the root of the solution.
NuGet.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositorypath" value=".\packages" />
<packageSources>
<clear />
<add key="<myfeedname>" value="https://pkgs.dev.azure.com/<myorg>/<myproject>/_packaging/<myfeedname>/nuget/v3/index.json" />
</packageSources>
</config>
</configuration>
I am having difficulty restoring packages when using NuGet.config from the solution. The log shows some evidence of using the azure org related URLs but not the project or feed. The restore cannot find packages. However, if I specify the feed and package folder in the NuGet restore task itself, it works fine and downloads all packages. I am expecting the task to find the private feed URL automatically from the NuGet.config file, authenticate (since inside pipeline), and download packages. The config file works fine in local builds asking for org user credentials (pop up) when using VS. The NuGet.config is checked into repo and hence there are no credentials used.
I also have NuGet Authenticate task just in case. (The working version doesn't even need this task).
- task: NuGetAuthenticate#0
What is wrong with the non-working version?
Working Version:
- task: NuGetCommand#2
displayName: "Step: Restore NuGet"
inputs:
command: 'restore'
feedsToUse: 'select'
vstsFeed: '<myproject>/<MyFeed>'
includeNuGetOrg: false
restoreSolution: '$(solution)'
restoreDirectory: "../../packages"
verbosityRestore: Detailed
continueOnError: true
Non-Working Version:
- task: NuGetCommand#2
displayName: "Step: Restore NuGet"
inputs:
command: 'restore'
restoreSolution: '$(solution)'
feedsToUse: 'config'
nugetConfigPath: "NuGet.config"
verbosityRestore: Detailed
continueOnError: true
From non-working version, the log shows that it is using the file as expected.
C:\hostedtoolcache\windows\NuGet\5.8.1\x64\nuget.exe restore D:\a\1\s\<solution-file-path> -Verbosity Detailed -NonInteractive -ConfigFile D:\a\1\s\NuGet.config
NuGet Version: 5.8.1.7021
Some errors:
WARNING: Unable to find version '6.0.0' of package 'Microsoft.CodeAnalysis.NetAnalyzers'.
NU1100: Unable to resolve 'AsyncFixer (>= 1.5.1)' for '.NETFramework,Version=v4.8'.
I also find some logs related to devops org for auth but no mention of project or feed name,
##[debug]Discovered URL prefixes: https://dev.azure.com/<my-org>/,.....others....
:
##[debug]Got auth token
..
:
##[error]Packages failed to restore
No mention of NuGet sources used in log. (System.Debug set to true in variables)
The working version shows the private feed as source used at the end.
NuGet Config files used:
D:\a\1\Nuget\tempNuGet_2852.config
Feeds used:
https://pkgs.dev.azure.com/<org>/<proj>/_packaging/<feed>/nuget/v3/index.json
However, I want to use the config version since it seems to be the right approach.

Error while pushing NuGet-package into GitLab Package Registry -> NuGet.Temporary.Package

I have trouble with pushing NuGet packages into our company GitLab package registry.
In my NuGet-Package I pack some binaries. Here is the Nuspec-file:
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata>
<id>test</id>
<version></version>
<authors>Company</authors>
<description>Demo Project</description>
</metadata>
<files>
<file src="../../__out/test/**" target="" />
</files>
</package>
I build the nuget packages which looks good in CI (I have downloaded and inspected the nuget-file)
After the build I push the NuGet-Package with this CI job into the registry.
Gitlab CI:
deploy:
stage: deployNuGet
image: mcr.microsoft.com/dotnet/sdk:5.0-buster-slim
script:
- dotnet nuget add source "$CI_SERVER_URL/api/v4/projects/$CI_PROJECT_ID/packages/nuget/index.json" --name gitlab --username gitlab-ci-token --password $CI_JOB_TOKEN --store-password-in-clear-text
- dotnet nuget push __out/package/test.0.0.1.nupkg" --source gitlab
This leads to following CI log:
$ dotnet nuget add source "$CI_SERVER_URL/api/v4/projects/$CI_PROJECT_ID/packages/nuget/index.json" --name gitlab --username gitlab-ci-token --password $CI_JOB_TOKEN --store-password-in-clear-text
Package source with Name: gitlab added successfully.
warn : No API Key was provided and no API Key could be found for 'https://XXXX/api/v4/projects/XXXX/packages/nuget'. To save an API Key for a source use the 'setApiKey' command.
Pushing test.0.0.1.nupkg to 'https://XXXXXX/api/v4/projects/XXXXX/packages/nuget'...
PUT https://XXXXX/api/v4/projects/XXXXXX/packages/nuget/
Created https://XXXXX/api/v4/projects/XXXXXX/packages/nuget/ 780ms
Your package was pushed.
This worked fine for quit a while, but now I get an different behavior.
The instead of the test package there is a NuGet.Temporary.Package.
When I move over the error sign it shows following message:
Invalide Package: failed metadate extraction
Does someone have an idea how to fix this issu?
Thank you!
Jan
This was a gitlab (14.0.x) error on self-managed AWS instances.
There is also an MR for fixing this.
Here is the issue with an sulution https://gitlab.com/gitlab-org/gitlab/-/issues/335488#note_623985000

GitHub Actions: automatically push NuGet package

I'm trying to configure my Github repository in order to automatically have a NuGet package built and pushed to both nuget.org and github.com. So what I want is that each time a commit is made on the master branch, or another branch is merged into the master, github publishes a new Nuget package of the head of the master to both Nuget and Github.
NuGet
On my nuget organization account, I generated an access token (username - API keys - Create)
On Github (select your organization - View organization - Settings tab - Secrets) I added a secret with the name PUBLISH_TO_NUGET_ORG and my nuget access token
Github
On my personal account, I generated an access token (Account - Settings - Developer settings - Personal access tokens - generate)
On Github I added a secret with the name PUBLISH_TO_GITHUB_COM and my github access token
These are the scopes for my Github access token:
Setup
In my github repository I've setup an action to restore, build, test, pack and publish:
name: .NET Core
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Setup .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: 3.1.301
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
# - name: Publish
# uses: brandedoutcast/publish-nuget#v2.5.2
# with:
# PROJECT_FILE_PATH: MintPlayer.SeasonChecker/MintPlayer.SeasonChecker.csproj
# NUGET_KEY: ${{secrets.PUBLISH_TO_NUGET_ORG}}
# INCLUDE_SYMBOLS: true
- name: Pack
run: dotnet pack --no-build --configuration Release MintPlayer.SeasonChecker/MintPlayer.SeasonChecker.csproj --output .
- name: PushNuget
run: dotnet nuget push *.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{secrets.PUBLISH_TO_NUGET_ORG}} --skip-duplicate
- name: AddGithubSource
run: dotnet nuget add source --username PieterjanDeClippel --password ${{secrets.PUBLISH_TO_GITHUB_COM}} --name github https://nuget.pkg.github.com/MintPlayer/index.json
- name: PushGithub
run: dotnet nuget push *.nupkg --source github --skip-duplicate
The push to nuget.org works fine, but the push to my GitHub feed fails with an Unauthorized error.
I've taken a look at some plugins like this one, and I want to embed this into my action in order not to build my project multiple times.
First take:
dotnet nuget push *.nupkg --source https://nuget.pkg.github.com/MintPlayer/index.json --api-key ${{secrets.PUBLISH_TO_GITHUB_COM}} --skip-duplicate
Result:
warn : Your request could not be authenticated by the GitHub Packages service. Please ensure your access token is valid and has the appropriate scopes configured.
Second take with multiple commands:
dotnet nuget add source --username PieterjanDeClippel --password ${{secrets.PUBLISH_TO_GITHUB_COM}} --name github https://nuget.pkg.github.com/MintPlayer/index.json
dotnet nuget push *.nupkg --source github --skip-duplicate
This one fails with the following (obvious) message:
error: Password encryption is not supported on .NET Core for this platform. The following feed try to use an encrypted password: 'github'. You can use a clear text password as a workaround.
error: Encryption is not supported on non-Windows platforms.
Does anyone have any experience with automated publishing of Nuget packages to Github?
Link to action configuration file
Edit
I tried sending a POST request:
Url: https://api.github.com/user
Authorization: Basic Auth
Username:
Password: <my-api-key>
And I'm getting my user information, so my access token definitely works.
Edit
I also tried running the command on my computer, replacing the token with my own and that as well does work.
Turns out I was missing a nuget.config file in my Solution
https://github.community/t/github-actions-automatically-push-nuget-package/128242/4
nuget.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
And my workflow file:
name: .NET Core
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Setup .NET Core
uses: actions/setup-dotnet#v1.5.0
with:
dotnet-version: 3.1.301
# Authenticates packages to push to GPR
source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
env:
NUGET_AUTH_TOKEN: '%NUGET_AUTH_TOKEN%'
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
- name: Pack
run: dotnet pack --no-build --configuration Release
- name: PushNuget
run: dotnet nuget push **/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.PUBLISH_TO_NUGET_ORG }} --skip-duplicate
- name: PushGithub
# The github token is automatically being pulled from the workflow
run: dotnet nuget push **/*.nupkg --no-symbols --skip-duplicate
env:
NUGET_AUTH_TOKEN: ${{ github.token }}
Per the github actions docs
<packageSourceCredentials>
<github>
<add key="Username" value="USERNAME" />
<add key="ClearTextPassword" value="TOKEN" />
</github>
</packageSourceCredentials>
So I think you just need to set -StorePasswordInClearText in your nuget add source command as you are currently encrypting the token
References:
Github Actions - https://docs.github.com/en/packages/using-github-packages-with-your-projects-ecosystem/configuring-dotnet-cli-for-use-with-github-packages#authenticating-to-github-packages
Nuget Config Docs - https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file#packagesourcecredentials

Deploying .NET Core 3.0 API to Azure Web App using Release Pipeline

I'm attempting to upgrade my API from .NET Core 2.2 to 3.0, but I cannot get the Azure Web App to actually run the application using 3.0.
My build pipeline configuration:
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
variables:
buildConfiguration: 'Release'
steps:
- task: UseDotNet#2
displayName: 'Use .NET Core 3'
inputs:
version: 3.x
- script: dotnet tool install --global dotnet-ef
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
- task: efcore-migration-script-generator-task#0
inputs:
projectpath: 'Models/Models.csproj'
databasecontexts: 'DataContext'
startupprojectpath: 'Api/Api.csproj'
targetfolder: '$(build.artifactstagingdirectory)/migrations'
- script: dotnet publish --output $(Build.ArtifactStagingDirectory)
displayName: 'dotnet publish $(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
I then have a multi-stage release pipeline that publishes the artifact to Azure using the Azure App Service Deploy Task. Everything runs without issue
I've installed the preview extension, as directed here, and running the powershell command Test-Path D:\home\SiteExtensions\AspNetCoreRuntime.3.0.x86\ returns true. I'm still seeing the following error though.
ANCM Failed to Find Native Dependencies
Back in Powershell, running dotnet --version and dotnet --list-runtimes shows that it only recognizes .NET Core 2 runtimes, despite the 3.0 runtime being present. As best I can tell, installing the site extension doesn't update the path to use the new dotnet version, and the Azure Devops deploy task doesn't seem to have any options to override the default. Has anyone managed to get a .NET Core 3 app deployed through Azure Devops Release Pipelines?
I don't consider it a good solution by any means, but the workaround I'm using right now that is working is to add a web.config file that passes the full path to the site extension version of dotnet
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="D:\home\SiteExtensions\AspNetCoreRuntime.3.0.x86\dotnet" arguments=".\Api.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
</system.webServer>
</location>
</configuration>
I'm still looking for a solution that isn't so error prone.

Resources