Streamline Your Development Workflow with Travis CI

Are you tired of manual testing and deployment processes slowing down your development cycle? Look no further! In this article, we’ll explore how to set up a continuous integration and continuous deployment (CI/CD) pipeline using Travis CI, a popular tool for automating testing and deployment.

Getting Started

To follow along, you’ll need:

  • A server with SSH access (e.g., DigitalOcean)
  • Git installed on your local machine
  • A GitHub account
  • A Travis CI account
  • Travis CLI installed on your local machine

Setting Up Travis CI

First, create a Travis CI account using your GitHub account. This will synchronize your repositories with Travis CI. Select the repository you want to enable Travis CI for and click on settings to view the customizations for this repository. The default settings are good to go, so let’s move on.

Generating an SSH Key

To generate an SSH key, log into your server via SSH and run the following command:

ssh-keygen -t rsa -b 4096

This will create an SSH key for the current user. Save the private key as deploy_key and add it to your .gitignore file to prevent accidental commits.

Encrypting the Private Key

Next, create a .travis.yml file that defines how Travis will handle your integration and deployment process. Run the following command to encrypt the private key:

travis encrypt-file deploy_key --add

This will update your .travis.yml file with the encrypted private key.

Configuring Travis CI

Now, let’s configure Travis CI to test and deploy our Laravel application. Update your .travis.yml file with the following content:

“`
language: php
php:
– 7.2

services:
– mysql

before_script:
– composer install
– cp.env.example.env
– php artisan key:generate
– php artisan migrate

script:
– vendor/bin/phpunit

afterscript:
– ssh -o “StrictHostKeyChecking=no” $SERVER
IP “cd /var/www/travis-ci-article && git pull origin master”
“`

This configuration defines the language and version, sets up MySQL, installs dependencies, creates a new database, and runs the tests.

Testing the Build and Deploy Process

Commit your changes and push to your master branch. Travis will receive the information about your push and start the build and deploy process. If everything is successful, you’ll see a green indication.

Testing for Failure

Let’s test the failure scenario by modifying the home.blade.php file to cause the test to fail. When the test fails, the build process will halt, and you’ll see a non-zero exit code.

Conclusion

In this article, we’ve demonstrated how to set up a CI/CD pipeline using Travis CI for a Laravel application. By automating testing and deployment, you can streamline your development workflow, reduce errors, and ensure that your code is always up-to-date. Try it out today and see the benefits for yourself!

Leave a Reply

Your email address will not be published. Required fields are marked *