April 16, 2017

Covering a Simpler Jenkinsfile

If you don't want to use Docker, we'll see how to use a simpler Jenkinsfile that won't require it.

If we don't use Docker, then we'll end up running our build/test/deploy steps directly on the Jenkins server. This means we need to install PHP and Composer directly on the server.

Let's see the pieces we put together for a new Github repository with a Laravel application and a Jenkinsfile.

1. Jenkinsfile

Here's a simple Jenkinsfile that will build, test, and then "deploy" an application. We don't cover the complex topic of deployment here, however. In our case, we'll pretend we have a deploy step, which I'd likely do using an Ansible role.

#!/usr/bin/env groovy

node('master') {
    try {
        stage('build') {
            // Checkout the app at the given commit sha from the webhook
            checkout scm

            // Install dependencies, create a new .env file and generate a new key, just for testing
            sh "composer install"
            sh "cp .env.example .env"
            sh "php artisan key:generate"

            // Run any static asset building, if needed
            // sh "npm install && gulp --production"
        }

        stage('test') {
            // Run any testing suites
            sh "./vendor/bin/phpunit"
        }

        stage('deploy') {
            // If we had ansible installed on the server, setup to run an ansible playbook
            // sh "ansible-playbook -i ./ansible/hosts ./ansible/deploy.yml"
            sh "echo 'WE ARE DEPLOYING'"
        }
    } catch(error) {
        throw error
    } finally {
        // Any cleanup operations needed, whether we hit an error or not
    }

}

2. Blue Ocean

Back in the Blue Ocean interface, we instruct it to re-scan our organization for projects with a Jenkinsfile. It finds our new repository with it's Jenkinsfile.

3. Server Dependencies

The initial build fails, and we see it's because it has no composer nor any PHP to use. We'll install PHP and Composer, needed to run our Jenkinsfile.

# Install PHP 7.1
sudo apt-add-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php7.1-cli php7.1-curl php7.1-sqlite3 php7.1-mysql php7.1-xml php7.1-mbstring php7.1-iconv 

# Install Composer
## Become Root (easier to install with the next command)
sudo su

## Install Composer directly to a global location
php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer

## Go back to our normal user
exit
which composer # /usr/bin/composer

Once everything is in place, our simpler Jenkinsfile should run fine, following each stage as defined!


If you're interested in learning more about Docker and how I use Jenkins with a Docker workflow, check out the ? Shipping Docker series!

All Topics