📜 ⬆️ ⬇️

How to configure Travis CI for a .NET Core + PostgreSQL project

I will talk about how to configure automatic launch of unit tests in the Travis CI service for a .NET Core project that uses PostgreSQL .


You can use this article as an example for a quick start.



I have a hobby project - a tool for versioning database migration to .NET Core. It can work with several DBMS, including PostgreSQL. There are a number of tests in the project ( xUnit ), which also need PostgreSQL to work.


I heard a lot about Travis CI and have long wanted to configure it to automatically run tests, but two things stopped me:



After spending half a day studying the documentation and experiments, I set up tests and I want to tell you about it.


What is Travis CI


Travis CI is a continuous integration service for projects on Github. When you commit something to the repository, Travis CI can automatically perform various useful actions. For example, it can run unit tests and code linters . I will call these efficiencies the word "build".


To configure Travis CI for your repository, you must specify the repository address in the Travis CI web interface and put the .travis.yml file with the build settings in the project root.


Configure Travis CI


The first thing to do is to log in at https://travis-ci.org using your GitHub account. After that you will see a list of all your repositories. Click the switch next to the repository for which you want to enable Travis integration:



Next, go to the settings of the selected repository. Here you can configure in which cases you need to run the assembly. I indicated that the assembly should be run at every Push operation in the repository, as well as when creating or changing the Pull request. In addition, I have indicated that the assembly should be run only if there is a configuration file .travis.yml in the root of the repository.



Example .travis.yml for .NET Core


The next step is to add a .travis.yml file with the build settings to the repository. To build a project on a .NET Core file, the .travis.yml file will look something like this:


 language: csharp sudo: required dist: trusty mono: none dotnet: 2.0.0-preview2-006497 before_script: - dotnet restore script: - dotnet test ./ThinkingHome.Migrator.Tests -c Release -f netcoreapp2.0 

Let's see what is written here:



Before committing a file with settings to the repository, make sure that the commands in the script and before_script locally executed without errors. After that, commit the .travis.yml file and push the changes to the remote server.


 git add .travis.yml git commit -m "Add travis config file" git push 

After a few seconds, we see that Travis saw our changes and started the build. You can see the detailed information about the assembly, including everything that was displayed in the console. We see that .NET Core has been installed and tests are running.



We also see that the tests have dropped, because they refer to a database that is not available to them.




PostgreSQL connection


Let's connect PostgreSQL to our build. To do this, add the services section with the postgresql entry to .travis.yml , and also add the commands for creating the database for tests using the psql utility to the .travis.yml section.


 services: - postgresql before_script: - psql -c "CREATE DATABASE migrations;" -U postgres ... 

By default, a postgres database is available, to which you can connect as a postgres user without a password. In order not to depend on these default settings, we will create an individual user and a database for tests. If you need to do something with the new database, do not forget to specify its name in the -d parameter.


The full text of the .travis.yml file .travis.yml shown below:


 language: csharp sudo: required mono: none dotnet: 2.0.0-preview2-006497 dist: trusty services: - postgresql before_script: - psql -c "CREATE DATABASE migrations;" -U postgres - psql -c "CREATE USER migrator WITH PASSWORD '123';" -U postgres - psql -c 'CREATE SCHEMA "Moo" AUTHORIZATION migrator;' -U postgres -d migrations - dotnet restore script: - dotnet test ./ThinkingHome.Migrator.Tests -c Release -f netcoreapp2.0 

Commit the modified file and push to the remote server. TA-dah!!! This time the tests were successful!



Conclusion


Fine! We figured out how to configure Travis CI for our project on .NET Core using PostgreSQL. Now we can add a tag to the readme of our project that shows the result of the last build using the Shields.io service.


 [![Travis](https://img.shields.io/travis/thinking-home/migrator.svg)](https://travis-ci.org/thinking-home/migrator) 


Thanks for attention!


')

Source: https://habr.com/ru/post/334576/


All Articles