The last time Hackerfall tried to access this page, it returned a not found error. A cached version of the page is below, or clickhereto continue anyway

Push-to-Deploy with Jenkins - Cloud Source Repositories — Google Cloud Platform

This tutorial shows how to set up Jenkins to build, test and deploy an App Engine application automatically when you push the application source code to a Google Cloud Source Repository.

This push-to-deploy configuration has the following parts:

When you push your application source code from the local or hosted Git repository to your Cloud Source Repository, Jenkins runs the steps required to build and test your application. If all tests pass, it deploys the application to the App Engine hosting environment.

Note: Compute Engine is a billable service of Google Cloud Platform. You must have an active billing account to use this configuration.

Contents

  1. Language support
    1. Java
    2. Go
    3. Python
    4. PHP
  2. Jenkins plugins
  3. Setup
    1. Prerequisites
    2. Create a Cloud project
    3. Connect your Git repository to the Cloud Source Repository
    4. Start the Jenkins instance
    5. Configure Jenkins
  4. Trigger deployment
  5. View build status and history
  6. View build logs and artifacts

Language support

You can set up this configuration for applications written in Java, Go, Python and PHP.

Java

The Jenkins instance provides both Maven and Gradle for building applications and running unit tests.

Maven

The Maven version is 3.1 with support for JUnit tests via the Maven Surefire plugin.

If you are using Maven to build and test your application, note the following:

For an example pom.xml file that works with the commands documented in this tutorial, see this sample file. For more information on using Maven to build and deploy App Engine applications, see Using Apache Maven.

Gradle

The Jenkins instance provides Gradle with support for JUnit tests via the Gradle Java plugin.

If you are using Gradle to build and test your application, note the following:

Go

The Jenkins image provides the App Engine Go SDK with the goapp command-line tool.

Python

The Jenkins image provides the nose testing framework for Python.

PHP

The Jenkins image provides the PHPUnit testing framework for PHP.

Jenkins plugins

Jenkins uses the following plugins to access the Cloud Source Repository and Google Cloud Storage:

These depend on the following additional plugins used for authentication, credentialing and handling of build metadata:

Setup

Prerequisites

You must have Git and the Google Cloud SDK installed on your local system.

Create a Cloud project

If you don't have a Cloud project for your application, you must

create

one now. If you don't have an active billing account,

add one

before you create your project.

Important: The name you use must be between 1 and 63 characters, with a lowercase letter as the first character. You can use a dash, lowercase letter, or digit for the remaining characters, but the last character cannot be a dash. Also, you should be aware that some resource identifiers (such as project IDs) might be retained beyond the life of your project. For this reason, avoid storing sensitive information in resource identifiers. ...see naming guidelines

Connect your Git repository to the Cloud Source Repository

Each Cloud project has an associated Cloud Source Repository. To deploy your application automatically using this configuration, you must add the Cloud Source Repository as a remote to your local Git repository, or connect a hosted repository.

Start the Jenkins instance

The next step is to start a Compute Engine instance running Jenkins from the Bitnami image provided by Google.

To start the Jenkins instance from the Bitnami image:

  1. Enable the Compute Engine and App Engine Admin APIs.

  2. On your local workstation, get the name and version number of the most recent Bitnami Jenkins image:

    gcloud compute images list --project bitnami-launchpad | grep jenkins
  3. Start the Jenkins instance using the Cloud SDK gcloud compute command, where:

    • <password> is a string of at least 12 characters that includes both letters and numbers,
    • <project-id> is your Cloud project ID,
    • <bitnami-image> is the name of the most recent Bitnami Jenkins image

    You will use the password to log in to the Jenkins Dashboard at a later step.

    $ PASSWORD=<password> # 12 or more chars, with letters and numbers $ PROJECT_ID=<project-id> $ BITNAMI_IMAGE=<bitnami-image> # e.g. bitnami-jenkins-1-606-0-linux-debian-7-x86-64

    $ gcloud compute \ instances create bitnami-jenkins \ --project ${PROJECT_ID} \ --image-project bitnami-launchpad \ --image ${BITNAMI_IMAGE} \ --zone us-central1-a \ --machine-type n1-standard-1 \ --metadata "bitnami-base-password=${PASSWORD},bitnami-default-user=user,bitnami-key=jenkins,bitnami-name=Jenkins,bitnami-url=//bitnami.com/stack/jenkins,bitnami-description=Jenkins,startup-script-url=https://dl.google.com/dl/jenkins/p2dsetup/setup-script.sh" \ --scopes "https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/devstorage.full_control,https://www.googleapis.com/auth/projecthosting,https://www.googleapis.com/auth/appengine.admin" \ --tags "bitnami-launchpad"

It may take several minutes for the Jenkins instance to complete the startup process.

Configure Jenkins

After you have started the Jenkins instance, you can use the Jenkins Dashboard to create a new job and define the build, test and deployment steps required for your application.

Log in to the Jenkins Dashboard

To log in:

  1. Navigate to your Jenkins instance in the Cloud Platform Console.

  2. Click the name of the Jenkins instance. In the page that opens, click Edit. In the Firewalls configuration group, select Allow HTTP Traffic and then click on save button at the bottom of the page. Navigate back to the VM Instances overview page and Click the IP address for the instance

    Note: For additional security, leave Allow HTTP Traffic unchecked and forward the necessary port via SSH with the following command:

    $ PROJECT_ID=<project-id> $ gcloud compute ssh --project ${PROJECT_ID} --zone us-central1-a bitnami-jenkins --ssh-flag="-L 8080:localhost:80"

    Then, on the machine where you ran gcloud, connect to http://localhost:8080.

  3. Click Access My Application. This opens the log-in page for the Jenkins Dashboard.

  4. Log in with username user and the password you specified when you started the Jenkins instance. If the Jenkins installation is still being set up following your initial deployment, you may see a message asking you to wait until this process is completed.

Create a new job

In Jenkins, a job specifies a set of steps required to perform an automated operation, typically the steps required to build an application. In this configuration, you use a Jenkins job to specify the build, test and deployment steps for your App Engine application. To create a new job:

  1. In the sidebar on the left of the Jenkins Dashboard, click New Item.

  2. Enter a name for the job.

  3. Select Freestyle Project.

  4. Click OK.

The Jenkins instance has several pre-defined tooling environments that specify the build tools and test frameworks required for App Engine applications. To choose the tooling environment for your job:

  1. In the current page in the Jenkins Dashboard, select Restrict Where This Project Can Be Run.

  2. Enter one of the following in the Label Expression field, depending on the language used for your application:

    • Java: cloud-dev-java
    • Go: cloud-dev-go
    • Python: cloud-dev-python
    • PHP: cloud-dev-php

Enable access to the Cloud Source Repository

Jenkins requires access to your Cloud Source Repository in order to get the source code for your application. To enable access to the Cloud Source Repository:

  1. In the current page in the Jenkins Dashboard, select Source Code Management > Git.

  2. Enter the Cloud Source Repository URL. The URL has the format https://source.developers.google.com/p/<project-id>.

    You may see an error message after entering the repository URL. This message will disappear when you select your credentials in the next step.

  3. Click the Add button on the Credentials line.

  4. In Kind select Google Service Account from metadata, the project name will be auto filled with your <project-id>, click Add in the Credential selection dialog.

  5. Select <project-id> Service Account from the Credentials drop-down list.

    The aforementioned error should now disappear.

Configure polling

You must also specify the interval at which Jenkins polls the Cloud Source Repository and runs the build, test and deployment steps if new changes have been committed. To set the polling interval:

  1. In the current page in the Jenkins Dashboard, select Build Triggers > Poll SCM.

  2. In the Schedule field, enter the polling interval in the format MINUTE HOUR DOM MONTH DOW. For example, enter H/5 * * * * to poll the Cloud Source Repository at five minute intervals. Click the Help button next to the Schedule field for more information about the syntax format.

Add a build and test step

Next, add a step to your Jenkins job to specify the build and test operations for your application. These operations vary by language and, for Java, by build tool. In the current page of the Jenkins Dashboard, click Add Build Step and then follow the instructions below.

Java and Maven

You can skip this step for Java and Maven and build, test and deploy in a single build step using mvn gcloud:deploy as described below.

Java and Gradle
  1. Select Invoke Gradle Script.

  2. Enter the following in the Goals text field:

    clean test assemble
Go

If you have implemented unit tests for your app, do the following:

  1. Select Execute Shell.

  2. Enter the following in the Command text field:

    #!/bin/bash -e goapp test -v | tee output.raw go2xunit -fail -input output.raw -output goapp.xml

You can also add the following command to resolve Go dependencies:

goapp get google.golang.org/appengine $(go-app-builder -print_extra_packages $(find -name '*.go') 2> /dev/null)
Python

If you have have implemented unit tests for your app, do the following:

  1. Select Execute Shell.

  2. Enter the following in the Command text field:

    nosetests --with-xunit --xunit-file=nosetests.xml
PHP

If you have implemented unit tests for your app, do the following:

  1. Select Execute Shell.

  2. Enter the following in the Command text field:

    phpunit <test_file_path.php>

Add a deployment step

Finally, add a step that specifies the deployment operation for your application and then save the Jenkins job. In the current page of the Jenkins Dashboard, click Add Build Step and then follow the instructions below.

Java
  1. Select Execute Shell.

  2. Enter the following in the Commands text field:

    mvn gcloud:deploy
  3. Click Save.

Go, Python and PHP
  1. Select Execute Shell.

  2. Enter the following in the Commands text field:

    gcloud --project=<project-id> preview app deploy -q app.yaml

Note that the application field in app.yaml must contain your project ID or be omitted in order to deploy successfully.

  1. Click Save.

Trigger deployment

To trigger the deployment, commit changes to your development Git repository and run the git push command to push the application source code to the master branch of your Cloud Source Repository.

If you are pushing directly to the Cloud Source Repository from a local Git repository, use the following command:

git push google master

If you are pushing to the Cloud Source Repository via a hosted repository, use the following:

git push <remote> master

where <remote> is the remote name for the repository on GitHub or Bitbucket.

If the build and test steps you specified in Jenkins succeed, the application is deployed to App Engine for serving. If the application is hosted on the default appspot.com domain, you can view it at http://<project-id>.appspot.com.

View build status and history

You can view build status and history in the Jenkins Dashboard. To view the status and history:

  1. The Build History panel in the sidebar on the left of the Dashboard shows a list of current and past builds and their status. Click on a build number to view more detailed information.

  2. Click Console Output in the sidebar on the left to view the output of the build, test and deployment steps.

View build logs and artifacts

When Jenkins runs build, test and deploy steps for your application, it uploads the logs and any build artifacts to Google Cloud Storage where you can view them. For Java applications, the build artifacts include the resulting WAR file named upload.war.

To view build logs and WAR files:

  1. Navigate to the storage browser in the Cloud Platform Console.

  2. Click the bucket whose name begins with rpl_ to display a list of folders. To see the build logs and WAR file for a specific commit ID, click on the folder whose name matches the ID.

Continue reading on developers.google.com