Travis CI is an online, distributed continuous integration service in software development for building and testing code hosted on GitHub.

Travis does a good job of integrating with GitHub. Unlike Jenkis, Travis does not need to deploy its own services on the server and is highly integrated with GitHub, so it is very friendly for open source projects.

Register to configure Travis

Register, log in, and then add your own repo on GitHub

Select one or more of the projects you want to integrate and start build, which is the process of clicking on a cross to turn it into a check box.

Assuming that Travis is now enabled for a project, go to the Settings that are enabled by default and set them according to your own needs. If there are no special needs, the default Settings will work.

The next steps are clear, with an official caption:

Add. Travis. Yml

The next thing to do is to write the configuration file, because Travis does all the action based on the configuration file.

Depending on your language, the configuration will vary a lot. Since my blog uses Laravel, the popular PHP framework, as an example, the simplest configuration file for PHP is the following:

language: php

php:
  - 7.19.
  - nightly
Copy the code

Trigger a build

Next, as mentioned above, submit the.travis. Yml file to GitHub, and Travis will automatically trigger the build task.

I knew it wouldn’t be easy the first time. Failed…

This error was reported when phpUnit was executed:

PHP Warning: require(/home/travis/build/stephencode/super-admin/bootstrap/.. /vendor/autoload.php): failed to open stream: No such file or directoryin /home/travis/build/stephencode/super-admin/bootstrap/autoload.php on line 17
Copy the code

Autoload.php is not used to install composer. The composer package is not installed in autoload.php.

install:
  - composer install --prefer-dist --optimize-autoloader --quiet
Copy the code

This will not report the above error, and will report the next error…

1) Tests\Feature\RouteTest::testBasicTest
RuntimeException: No application encryption key has been specified.
Copy the code

In fact, this is because the phpUnit.xml in Laravel is not configured properly.

After five or six build failures, I finally succeeded.

Automatic deployment to a remote server

Now that you’re ready to build automatically, the next step is to deploy to a remote server. Travis provides after_success to accomplish this step.

Wait, we need to deploy to a remote server, so we need Travis to log in to a remote server, so how do we secure the login password? This is the first problem to be solved, and the plain text is definitely not going to work.

Encrypting the Login Password

It looks like we’ll have to solve this problem first. There are Encrypting Files in Travis Docs that help me consider this unavoidable problem.

Let’s put it into practice:

First install Travis via Ruby’s gem

gem install travis
Copy the code

Alas, after trying several times, I found that after knocking this shell, it seemed to sink into the sea. Even with the agent on, there’s no way out but to change the mirror.

$ gem sources -l

*** CURRENT SOURCES ***

https://rubygems.org/
Copy the code

Look at the current mirror, this product (Rubygems) domestic surprisingly difficult to access, a search for domestic mirror source, Ruby China should be very obvious ~

$ gem update --system
$ gem sources --add https://gems.ruby-china.org/
Copy the code

Then check the gem image to make sure you only have the Ruby China gem source.

Ok, now you can happily install Travis

$ sudo gem install travis
Copy the code

Let’s start by logging in to Travis from the command line

$ travis login

We need your GitHub login to identify you.
This information will not be sent to Travis CI, only to api.github.com.
The password will not be displayed.

Try running with --github-token or --auto if you don't want to enter your password anyway. Username: [email protected] Password for [email protected]: *** Successfully logged in as demo!Copy the code

It will ask you to input your GitHub account password. This is a GitHub service, so there is no need to worry about password leakage.

Switch the directory to the project root directory, which is.travis. Yml. Since we need Travis to log in to our own server remotely, we need to encrypt the SSH private key stored locally. (By default, you can also log in through SSH without password, please refer to my article on SSH Without password.)

$ travis encrypt-file ~/.ssh/id_rsa --add

Detected repository as xxx/xxx, is this correct? |yes| yes
encrypting ~/.ssh/id_rsa for xxx/xxx
storing result as id_rsa.enc
storing secure env variables for decryption

Make sure to add id_rsa.enc to the git repository.
Make sure not to add ~/.ssh/id_rsa to the git repository.
Commit all changes to your .travis.yml.
Copy the code

Look at.travis. Yml in the current directory at this point, and there are a few more lines

before_install:
  - openssl aes-256-cbc -K $encrypted_d89376f3278d_key -iv $encrypted_d89376f3278d_iv
  -in id_rsa.enc -out ~\/.ssh/id_rsa -d
Copy the code

To ensure normal permissions, add another line of shell setting permissions

before_install:
  - openssl aes-256-cbc -K $encrypted_d89376f3278d_key -iv $encrypted_d89376f3278d_iv
    -in id_rsa.enc -out ~/.ssh/id_rsa -d
  - chmod 600 ~/.ssh/id_rsa
Copy the code

This may also be useful because Travis’s first login to the remote server will result in SSH host authentication and a host trust issue. The official solution is to add addons configuration:

addons:
  ssh_known_hosts: your-ip
Copy the code

At this point, Travis is able to log in to his remote server without having to log in confidentially

Automatic deployment

Now that you can log in to the server confidential-free, write a deployment script, execute it at login, and leave it as it is

Writing deployment Scripts

My level of writing Shell scripts is very limited, so here is a minimal Demo for reference:

#! /bin/bash
cd /path/to/your-project
git pull origin master
echo 'travis build done! '
Copy the code

Executing the deployment Script

Write two lines in the.travis. Yml configuration file:

after_success:
  - ssh your-user@your-ip "./your-shell-script"
Copy the code

Replace your-user, your-ip, and your-shell-script with your own.

High sign

Hard struggle for a day, always hope others to see their labor results, in addition to writing this article accident can do what? It’s natural to give your project a nice build:passing sign in readme. md on GitHub, like this:

conclusion

In this process, I learned a lot of new things and found some shortcomings, such as writing shell scripts.

Finally, post my own.travis. Yml, where I will comment and say:

language: php

php:
  - 7.19.
  - nightly

env:
  - APP_DEBUG=false

before_install:
  - openssl aes-256-cbc -K $encrypted_d89376f3278d_key -iv $encrypted_d89376f3278d_iv
    -in id_rsa.enc -out ~/.ssh/id_rsa -d
  - chmod 600 ~/.ssh/id_rsa

install:
  - composer install --prefer-dist --optimize-autoloader --quiet

notifications:
  email:
    recipients:
    - [email protected]
    on_success: always
    on_failure: always

script:
  - phpunit -c phpunit.xml --coverage-text

after_success:
  - ssh [email protected] "./travis_build" Please replace with your login IP address and login user

addons:
  ssh_known_hosts: xxxx.xxxx.xxxx.xxxx Please replace with your own server IP address

Copy the code

References:

  • Use Travis for continuous integration
  • The Jekyll + Travis CI Automated deployment blog