Workflow for consuming a NuGet package

:hugs: Please help fill in this template with all the details to help others help you more efficiently. Use formatting blocks for code, config, logs and ensure to remove sensitive data.

Problem to solve

I have build a NuGet package and pushed it to its package registry. Now i have another project where i want to use that package.
The NuGet sources on the Runner are persistent.

So i have to add the nuget source in a case, that every other user can consume it. While pushing the package i used user “gitlab-ci-token” and as password “$CI_JOB_TOKEN”. That isn’t possible to use for adding a source, because the $CI_JOB_TOKEN is just usable in the special job.

So what would be the right way to add sources inside CI/CD pipeline for using it and not getting 401? Exists a internal user who can used for adding sources with their UserName and Token?

Steps to reproduce

See next paragraph.

Configuration

variables:
  EXE_RELEASE_FOLDER: '7815_Datenaustausch\bin\Release'
  NUGET_SOURCE_NAME: 'IT-Entwicklung'
  NUGET_SOURCE_URL: 'http://gitlab01.infas-intern.int/api/v4/groups/5/-/packages/nuget/index.json'

stages:
  - build
  - test
  - deploy

build-job:
  stage: build
  tags:
    - dotnet
  before_script:
    - |
     Write-Host "Entfernen und Hinzufügen der NuGet-Quellen..."
     if (-not $(Get-PackageSource -Name $NUGET_SOURCE_NAME -ProviderName NuGet -ErrorAction Ignore)) {
     nuget source Add -Name $NUGET_SOURCE_NAME -Source $NUGET_SOURCE_URL -UserName gitlab-ci-token -Password $CI_JOB_TOKEN -StorePasswordInClearText
     Write-Host "Die NuGet-Quelle '$NUGET_SOURCE_NAME' wurde hinzugefügt."
     } else {
     Write-Host "Die NuGet-Quelle '$NUGET_SOURCE_NAME' ist bereits vorhanden."
     dotnet nuget remove source $NUGET_SOURCE_NAME
     nuget source Add -Name $NUGET_SOURCE_NAME -Source $NUGET_SOURCE_URL -UserName gitlab-ci-token -Password $CI_JOB_TOKEN -StorePasswordInClearText
     }
  script:
    - |  
     echo "Compiling the code..."
     dotnet restore
     dotnet build -c Release
     echo "Compile complete."

unit-test-job:
  stage: test
  tags:
    - dotnet
  script:
    - echo "Running unit tests... This will take about 60 seconds."
    - echo "Unit testing complete"

deploy-job: 
  stage: deploy
  tags:
    - dotnet
  environment: production
  script: 
   - |
    echo "Deploying application..."
    dotnet restore
    dotnet build -c Release
    dotnet publish /p:PublishProfile=Properties\PublishProfiles\FolderProfile.pubxml
    echo "Application successfully deployed."

Versions

Please select whether options apply, and add the version information.

  • Self-managed 16.10.1-ee
  • GitLab.com SaaS
  • Self-hosted Runners 16.11.0