Gitlab CI/CD yml file to build, package and deploy .net standard class library as nuget to nexus repository - nuget

I have a .net Standard (.Net Standard 2.0) class library which I want to deploy to nexus as nuget package. The private nexus repository is ready and I'm using Gitlab for code management.
In Gitlab I added the gitlab-ci.yml file which will trigger the build and the deployment but still without enough steps:
stages:
- build
- package
- deploy
build_image:
stage: build
only:
- master
script:
- echo "Restoring NuGet Packages…"
- RUN dotnet restore
- echo "Building solution…"
- RUN dotnet build --no-restore -c Release -o
package_dev:
stage: package
script:
-
deploy_dev:
stage: deploy
environment:
name: development
only:
- master
script:
-
My question is how to configure this file to trigger a build then perform packaging and deploy/push to nexus repo?
I don't know if I described it well as i'm totally new to this topic. I found some examples using MAVEN image but we are not using it.
Thanks in advance!

If I understand your question correctly, you want to create a nuget package out of your project and upload it to Nexus Repo. You can do it one step.
build-and-upload:
image: <dot-net image>
stage: build-and-upload
environment:
name: dev/test/prod
only:
- master
before_script:
- aws commands if you need to assume a deploy role.
script:
- ./scripts/nuget_publish.sh
And this is how your nuget_publish.sh will look like
dotnet build
dotnet pack
NEXUS_SOURCE=<Nexus_Source_Repo_Url>
NEXUS_API_KEY=<Nexus_Source_Repo_Api_Key>
dotnet nuget push <ProjectName>/bin/Debug/*.nupkg --source $NEXUS_SOURCE --api-key $NEXUS_API_KEY

I found a solution which works fine for me. Here is the script in case anybody faces the same issue/requirement:
In ci.yml on deploy stage:
stages:
- deploy
before_script:
- nuget restore mysolution.sln
deploy_mysolution:
stage: deploy
image:
name: crunchtime/dotnetcore-nuget-msbuild-docker
script:
- dotnet msbuild mysolution.sln /t:Clean,ReBuild /p:Configuration=Release;Platform="Any CPU"
- dotnet pack "mysolution/myproject.csproj" /p:Configuration=Release;Platform="Any CPU"
- PKGPATH=$(find myproject/bin/Release/*.nupkg)
- dotnet nuget push $PKGPATH -k $NUGET_PUSH_KEY -s https://nexus.xyz.com/repository/nuget/
only:
- master
where $NUGET_PUSH_KEY is the api key which is saved as an env variable which is applied to environments via the runner.

Related

How can I prevent my unit test project from being deployed to my Azure Web App along with my ASP.NET Core website project?

I have an ASP .NET Core website whose source code is stored in GitHub. This is automatically deployed to an Azure App Service (running on Linux) when I do a check-in to GitHub. (I set this up a while back using the Azure "wizard", but now I can't find any way to tinker with it).
Recently I added a unit test project to my solution, and when I checked that in the unit test project was deployed along with the web project - which I don't want. (Especially since the runtime host gets confused about which DLL it should run, and decides to run neither - so the website does not start!).
Is there a way to prevent the unit test project from being built or deployed? (I'm happy just to run these tests locally). I'm not even sure where to start - would I do this in GitHub, or in the Azure portal? I can't find any likely-looking knobs or levers either in GitHub or in the Azure portal.
Update: I've found a file that has been created in my repo in a .github/workflows folder. Here's the content:
name: Build and deploy ASP.Net Core app to Azure Web App - MyApp
on:
push:
branches:
- master
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: '5.0.x'
include-prerelease: true
- name: Build with dotnet
run: dotnet build --configuration Release
- name: dotnet publish
run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp
- name: Upload artifact for deployment job
uses: actions/upload-artifact#v2
with:
name: .net-app
path: ${{env.DOTNET_ROOT}}/myapp
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact#v2
with:
name: .net-app
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy#v2
with:
app-name: 'My-App'
slot-name: 'Production'
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_0C864EC866B247EABE271A962BE84378 }}
package: .
While that's progress, I still can't see where I could modify it to prevent it from including the unit test project, because it doesn't actually reference anything directly.
Here's what worked for me:
I changed:
run: dotnet build --configuration Release
...to:
run: dotnet build MyProject --configuration Release
...where MyProject is the name of the folder containing the website project. (dotnet build is run from the solution folder, and the default behavior is that it will build all projects in the solution; specifying a sub-folder name makes it build only the project(s) it finds in that folder - see the documentation here).
Similarly, I changed:
run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp
to:
run: dotnet publish MyProject -c Release -o ${{env.DOTNET_ROOT}}/myapp
Thanks to #LexLi for pointing me in the right direction.

GitHub Actions dotnet build Unable to find package

I have a .NET CI ins GitHub Actions. Before added Dapper.FluentMap to the project, the GitHub Action was working without any errors.
GitHub Actions fails on:
- name: Build with dotnet
run: dotnet build --configuration Release
How can I build on GitHub actions using a NuGet Dapper.FluentMap dependency ?
I am now getting an error in GitBub Actions:
D:\a\esta-uploadmanagement\esta-uploadmanagement\BLL\BLL.csproj : error NU1101: Unable to find package Dapper.FluentMap. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages [D:\a\esta-uploadmanagement\esta-uploadmanagement\esta-uploadmanagement.sln]
D:\a\esta-uploadmanagement\esta-uploadmanagement\WebApp\WebApp.csproj : error NU1101: Unable to find package Dapper.FluentMap. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages [D:\a\esta-uploadmanagement\esta-uploadmanagement\esta-uploadmanagement.sln]
Failed to restore D:\a\esta-uploadmanagement\esta-uploadmanagement\WebApp\WebApp.csproj (in 316 ms).
Failed to restore D:\a\esta-uploadmanagement\esta-uploadmanagement\BLL\BLL.csproj (in 313 ms).
D:\a\esta-uploadmanagement\esta-uploadmanagement\DAL\DAL.csproj : error NU1101: Unable to find package Dapper.FluentMap. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages [D:\a\esta-uploadmanagement\esta-uploadmanagement\esta-uploadmanagement.sln]
Failed to restore D:\a\esta-uploadmanagement\esta-uploadmanagement\DAL\DAL.csproj (in 1 ms).
My Yaml file
on:
push:
branches:
- main
workflow_dispatch:
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout#v2
- name: Set up .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: '5.0.x'
- name: Build with dotnet
run: dotnet build --configuration Release
- name: dotnet publish
run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp
This seems to be a recent bug / issue, as reported here and here on GitHub.
You can work around this issue by manually adding the nuget.org as source in a step:
[...]
- name: Set up .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: '5.0.x'
# new step
- name: Add nuget.org as nuget package source
run: dotnet nuget add source https://api.nuget.org/v3/index.json --name nuget.org
- name: Build with dotnet
run: dotnet build --configuration Release
[...]

GitHub action to publish a path

I have a build yaml file as follows:
jobs:
XYZBuildAndRelease:
strategy:
matrix:
BuildConfiguration:
- debug
- release
max-parallel: 2
runs-on: windows-2019
steps:
- name: List contents of a folder
run: dir
- name: dotnet publish graph updater
run: dotnet publish Service/XYZ/Hosts.FA/*.csproj --configuration Release --output updater_publish_output
On pushing this yaml file, I see the build failing - Project file does not exist
What am I missing?
Explicitly specifiy the project to publish
- name: dotnet publish graph updater
run: dotnet publish Service/GroupMembershipManagement/Hosts.GraphUpdater/Hosts.GraphUpdater.csproj --configuration Release --output updater_publish_output
Reason: you can't publish multiple projects at the same time, see https://github.com/dotnet/sdk/issues/7238.

Incorrect Path and No API errors when trying to publish to GitHub packages via GitHubActions

been stuck on this for a long time now, seems to be one problem after the other, just trying to get GitHub Actions to publish a nuGet package to GitHub packages, documentations is really hard to find and doesnt seem to give any clear examples
This was my previous question (just for context incase it helps)
I cant get gitHub actions to publish my package build to nuget.pkg.github
but now, I am getting the following:
Run dotnet nuget push "bin/Release/Project.1.0.0.nupkg" --source "github"
warn : No API Key was provided and no API Key could be found for 'https://nuget.pkg.github.com/name'. To save an API Key for a source use the 'setApiKey' command.
error: Could not find a part of the path '/home/runner/work/project/project/bin/Release'.
Here is my full yml
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.200
- 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: nuGet publish
run: dotnet nuget add source https://nuget.pkg.github.com/name/index.json -n github -u uname -p password123 --store-password-in-clear-text
- name: nuGet pack
run: dotnet pack --configuration Release
- name: publish
run: dotnet nuget push "bin/Release/EurekaShared.1.0.0.nupkg" --source "github"
this is the build result:
dotnet nuget sadly doesn't always work as expected, and also has a habit of returning misleading error messages. It's infuriating.
Try adding a nuget/setup-nuget#v1 step to your workflow and use nuget.exe push instead of dotnet nuget push. That might either fix the problem or at least give you a more helpful error message.
I think the error is related with the part of the path to nupkg file, or file name. It could be issue of case, since Linux is case-sensitive.
Moreover, this workflow file is to publish just one package, i.e., EurekaShared.1.0.0.nupkg it won't work if your package is EurekaShared.1.0.2.nupkg
Perhaps you can give it a try by updating the last line in your YML file by using wildcard ** as follows:
run: dotnet nuget push /**/*.nupkg --source "github"
I also suggest that you secure your username and password in workflow file with GitHub encrypted secrets https://docs.github.com/en/free-pro-team#latest/actions/reference/encrypted-secrets

I cant get gitHub actions to publish my package build to nuget.pkg.github

I have been trying for a few months now, just to get GitHub actions to push a build to the gitHub packages, everytime i come back, try something else, nothing seems to work and it isnt making any sense.
I have just seen a new link with more details added for gitActions here:
I got excited, clicked, and see more code than previous had:
// Step 1: Authenticate (if this is the first time) Note you must also pass --store-password-in-clear-text on non-Windows systems.
$ dotnet nuget add source https://nuget.pkg.github.com/xxxxx/index.json -n github -u xxxxx -p GH_TOKEN [--store-password-in-clear-text]
// Step 2: Pack
$ dotnet pack --configuration Release
// Step 3: Publish
$ dotnet nuget push "bin/Release/myproject.1.0.0.nupkg" --source "github"
so, i went to my yml page (all within GitHub) and noticed the format is a little different... they are missing the "- name: xxx" and "run: xxx"
so, i updated... here is my full yml
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.101
- 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: nuGet publish
run: dotnet nuget add source https://nuget.pkg.github.com/xxxx/index.json -n github -u xxxx-p GH_TOKEN [--store-password-in-clear-text]
- name: nuGet pack
run: dotnet pack --configuration Release
- name: publish
run: dotnet nuget push "bin/Release/projectname.1.0.0.nupkg" --source "github"
now, i thought maybe it would complain about password or something, but instead, im just getting this error: (i.e. "error: Unrecognized command or argument 'add'")
I am at a total loss and have no idea what and how to do this.... it builds fine all in gitHub, my package location is in GitHub, what am i doing so wrong?
EDIT:
thank you #ColinM after adjusting the yml above and just changin the version to 3.1.200, it now gets further, however, getting the following
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
when running this line
Run dotnet nuget add source nuget.pkg.github.com/myname/index.json -n github -u myname -p abc123"
As per the MSDN documentation for dotnet nuget source add, this is available only from SDK 3.1.200 onwards; whereas you're currently using 3.1.101.
Update your YAML file to install an SDK version equal to or greater than 3.1.200