Set TravisCI to publish package only when push is on master

Hello,

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

  1. Restore the solution
  2. Build the solution
  3. Test the solution
  4. Generate the NuGet package
  5. 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, I have created a question on Stackoverflow, just in case if you also want to answer there.

Hey @emimontesdeoca,

Have you looked at build stages?

Hello @Montana!

Gave it a look and seems to be what I’m looking for, just one question, is it mandatory to use script for stages?

Something like this would work?

language:
    csharp
sudo: required
mono: none 
dotnet: 3.0

os:
  - linux

global:
  - DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true
  - DOTNET_CLI_TELEMETRY_OPTOUT=true


jobs:
  include:
    - stage: test
		before_script:
			- dotnet restore "solution.sln"
		
		script:
			- dotnet build "solution.sln" -c Release
			- dotnet test "solution.sln" -c Release -v n
    - stage: deploy
                script:
			- dotnet pack "solution.sln" -c Release
			- dotnet nuget push "bin\Release\*.nupkg" 

Thanks!

1 Like

Hey @emimontesdeoca,

Glad to hear this is what you were looking for. I would leave script.

-Montana