Set TravisCI to publish package only when push is on master - travis-ci

I'm looking for a way to make an autodeploy of NuGet packages after the compilation. So far I've successfully set the CI/CD on TravisCI but I want to publish the NuGet package only from the master branch and not from the pull request branch.
This is what the travis.yml should look like to make the CI/CD with the deploy.
language:
csharp
sudo: required
mono: none
dotnet: 3.0
os:
- linux
global:
- DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true
- DOTNET_CLI_TELEMETRY_OPTOUT=true
before_script:
- dotnet restore solution.csproj
- dotnet restore solutionTest.csproj
script:
- dotnet build solution.csproj --configuration Release
- dotnet test solutionTest.csproj --configuration Release
after_script:
- dotnet pack solution.csproj --configuration Release
- dotnet nuget push bin\Release\*.nupkg -s "some-source"
As you can see the process is pretty simple
Restore the solution
Build the solution
Test the solution
Generate the NuGet package
Push the package to the source
What do I want
I want this to only happen in the master branch after the pull request is completed.
What happens now
When I do a PR to the master branch, in order to be able to be merged it needs to build, so the TravisCI builds everything which builds, test, packs the solution, and also includes the package publication, that is what I don't want.
Remarks
Can I set the scripts to be run only in a specific branch? Something like the following:
[ommited code travis.yml]
branch:
master:
after_script:
- dotnet pack solution.csproj --configuration Release
- dotnet nuget push bin\Release\*.nupkg -s "some-source"
Hopefully this is understandable.

Related

Yarn 2.0 zero installs production vs development deployment?

So we are in the process of moving from yarn 1.x to yarn 2 (yarn 3.1.1) and I'm getting a little confused on how to configure yarn in my CI/CD config. As of right now our pipeline does the following to deploy to our kubernetes cluster:
On branch PR:
Obtain branch repo in gitlab runner
Lint
Run jest
Build with environment variables, dependencies, and devdependencies
Publish image to container registry with tag test
a. If success, allow merge to main
Kubernetes watches for updates to test and deploys a test pod to cluster
On merge to main:
Obtain main repo in gitlab runner
Lint
Run jest
Build with environment variables and dependencies
Publish image to container registry with tag latest
Kubernetes watches for updates to latest and deploys a staging pod to cluster
(NOTE: For full-blown production releases we will be using the release feature to manually deploy releases to the production server)
The issue is that we are using yarn 2 with zero installs and in the past we have been able prevent the production environment from using any dev dependencies by running yarn install --production. In yarn 2 this command is deprecated.
Is there any ideal solution to prevent dev dependencies from being installed on production? I've seen some posts mention using workspaces but that seems to be more tailored towards mono-repos where there are more than one application.
Thanks in advance for any help!
I had the same question and came to the same conclusion as you. I could not find an easy way to perform a production build on yarn 2. Yarn Workspaces comes closest but I did find the paragraph below in the documentation:
Note that this command is only very moderately useful when using zero-installs, since the cache will contain all the packages anyway - meaning that the only difference between a full install and a focused install would just be a few extra lines in the .pnp.cjs file, at the cost of introducing an extra complexity.
From: https://yarnpkg.com/cli/workspaces/focus#options-production
Does that mean that there essentially is no production install? It would be nice if that was officially addressed somewhere but this was the closest I could find.
Personally, I am using NextJS and upgraded my project to Yarn 2. The features of Yarn 2 seem to work (no node_modules folder) but I can still use yarn build from NextJS to create a production build with output in the .next folder.

Artifacts for ASP.NET MVC project in GITLAB

I have a asp.net mvc project, which is already there in GitLab repository.
I have configured a specific windows runner for my project.
I have installed visual studio in the runner machine.
Now I am able to build my code whenever pipeline runs.
I want to create an artifact package and use to deploy to my azure app service in next stage in pipelne.
How to create the artifact after build, and where these artifacts getting stored (runner machine or git repo)?
Also It would be great if someone can help with script to web deploy (instaed of FTP) the app service in deploy stage
Thanks in advance.
Regards,
Naresh Ede.
I have written the YAML file like below to build and generate artifacts.
The solution build was successful when i'm seeing the log, but getting a warning at generating artifacts
stages:
- build
solution-build:
stage: build
script:
- MSBuild.exe 'my-awesome-app.sln' /p:OutputPath="c:\Builds\awesome-
project"
artifacts:
paths:
- artifacts/
Job Log:
Uploading artifacts...
108 Runtime platform arch=386 os=windows pid=19556 revision=577f813d version=12.5.0
109 WARNING: binaries/: no matching files
110 ERROR: No files to upload

How to publish binaries and copy to server in TFS 2017

We were using xaml builds earlier now we migrated to TFS2017. I am not able to publish the build files and how can we copy them.
My Build runs file it does not publish files. I see only the code files.
You need to add Copy Files task and Publish Build Artifacts task in your build definition, to copy and publish files:
Maybe you may create the publish profile for you web project and use it in build process. I use this way for the web site project:
Create the publish profile for project to package: https://msdn.microsoft.com/en-us/library/dd465323(v=vs.110).aspx
Add publish args for the build step. In my case:
/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\Test" /p:PublishProfile="shdemowebtest - Web Deploy"
Then copy drop artifact
In your case you may create profile to publish on the file system. Then add build args to publish and redirect the destination folder: MsBuild publish website without using publish profile
Steps for publish to local folder (on VSTS and local build agent):
Add new profile to your project (destination bin\release\PublishOutput):
I use first build step to build additional project with tests.
Add step to publish with default folder path.
MS Build Args:
/p:DeployOnBuild=true /p:PublishProfile="FolderProfile"
3.1. Then I use standard step to copy results to staged directory:
3.2. And publish to drop artifact:
3.3. Then you can use that artifact to deploy with release management:
Add step to publish to custom path:
MS Build Args:
/p:DeployOnBuild=true /p:PublishProfile="FolderProfile" /p:publishUrl="C:\temp\Output"
4.1. The result of this step:

Running dotnet ef database update on tfs build agent

I'm trying to make a CD setup which migrates my target database on release in TFS 2017 (update 3). My artifacts only contains my website assemblies, with DB context and controllers etc. If I update my database during build (and not during release) I can run
dotnet restore
dotnet ef database update
and it works nicely, but this ofcourse does not work during release, as the source code is not available in the artifacts, and dotnet restore requires the .csproj file.
Anyone have suggestion of how to set up migrations during release? Do I need to include my source in the artifacts?
I'm using .net core 2.0 and ef core 2.0
If you run dotnet ef database update with the --verbose option, you'll see a call to dotnet exec ef.dll. This is the command you can use directly on the binaries without needing the source code.

Problems Building .Net Core on TFS

I have a solution containing multiple .Net Core projects, some of which are dependent on other projects in the same solution. The whole solution is hosted in a Git repository of an on-premises TFS server.
I want to set up an automated build process on the TFS server. For lack of better knowledge, I use the Visual Studio build template and leave all the settings on their defaults. When I run the build process, it fails on the "Build solution" step with the error message
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets(262,5): error : Project MyProject does not have a lock file.
Please run "dotnet restore" to generate a new lock file. [C:\TFSBuildAgent_work\6\s\MyProject\MyProject.xproj]
So I go ahead and create a PowerShell script with the content dotnet restore that I run before "Build solution" but after "Nuget restore". This also fails, this time on step "Dotnet restore" with the error message
Errors in C:\TFSBuildAgent_work\6\s\MyProject\project.json
Unable to resolve 'ProjectDependency (>= 1.0.0)' for '.NETFramework,Version=v4.6.1'.
When I run dotnet restore on my local machine, it completes without any issues.
Any help or hints are greatly appreciated!
I helped myself now in packaging the project dependency and uploading it to my inhouse package server. dotnet restore will then pull it from the server and build the whole project. Not the nicest way, but at least it's working.

Resources