Skip to content

Setup Jenkins on Google Cloud

Faisal Ali Rabbani edited this page May 29, 2015 · 58 revisions

Install the gcloud command line tool

Mac/Linux

  1. Download and install Google Cloud SDK by running the following command in your shell or Terminal: curl https://sdk.cloud.google.com | bash
  2. Restart your shell or Terminal

Windows

  1. Download the SDK
  2. Launch the downloaded GoogleCloudSDKInstaller.exe installer and follow the prompts
  3. When the installation finishes, open a command prompt (cmd.exe) or the Google Cloud SDK Shell from Desktop or Start Menu

Spin up a VM on Google Compute Engine (GCE)

  1. Create an instance with following specs:

    Machine Type: n1-standard-1 (1 vCPU, 3.75 GB Memory)
    image:	ubuntu-1404-trusty-v20150316
    network: <fill it in>
    
  2. Set up the following environment variables for your terminal:

    export YOUR_PROJECT_ID=<fill it in>
    export YOUR_ZONE=<fill it in>
    
  3. To see a list of all available zones:

```

$ gcloud compute zones list NAME REGION STATUS NEXT_MAINTENANCE TURNDOWN_DATE asia-east1-b asia-east1 UP asia-east1-c asia-east1 UP asia-east1-a asia-east1 UP europe-west1-d europe-west1 UP europe-west1-b europe-west1 UP europe-west1-c europe-west1 UP us-central1-a us-central1 UP us-central1-b us-central1 UP us-central1-c us-central1 UP us-central1-f us-central1 UP

1. Create instance via following command 

    ```
    gcloud compute instances create NAME <name> --image "ubuntu-1404-trusty-v20150316" --machine-type "n1-standard-1" --network "<fill it in>" --zone "<zone_id>"
    ```
It's ok to leave out the --network part, GCE will assign a "default" network which works fine. 

# Connect to your VM instance

1. In your local terminal run `gcloud auth login` which will kick off your browser and take you through an interactive login process
2. Set your default project, and then all your commands will run in context of that project, moving forward: `gcloud config set project $YOUR_PROJECT_ID`
3. Use `ssh` to connect directly with the VM instance you created earlier: `gcloud compute ssh $INSTANCE_NAME --zone $YOUR_ZONE`

## Install Basic Packages
1. On bare metal we will need some basic packages to start us off:

    ```
    sudo apt-get install curl 
    sudo apt-get install git
    ```

## Optional - Install `gcloud` on your VM

It only makes sense to do this if you will be moving the build bits around via gcloud. In our Jenkins we don't use gcloud for file transfers. Here are the instructions for something like CodeShip, which are more convoluted than what they should be for an instance running on GCE because GCE instances are provisioned with proper gcloud auth credentials. [Install gcloud on CodeShip](Install-gcloud-on-CodeShip)

## Setup Jenkins
1. Install

    ```
wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
    ```
2. Check if 8080 is being used on your VM, if not there's no need to change the port. Otherwise change Port as follows: run `vim /etc/default/jenkins` and edit the file as follows (note: on some systems this file may be readonly and you may need to use sudo for the edit command):

    ```
    # port for HTTP connector (default 8080; disable with -1)
    HTTP_PORT=9090
    ````
3. Start Jenkins: run `sudo /etc/init.d/jenkins start`
4. Setting Security
  1. Go to the `Configure Global Security` screen (http://server/jenkins/configureSecurity/) and choose `enable security`. An alternate URL to try is http://server:8080/configureSecurity
  1. Select `Jenkins's own user database` as the security realm. 
  1. Place a check mark next to `Allow users to sign up`
  1. Select `Matrix-based security` as the authorization
  1. Give anonymous user the read access or uncheck all. 
  1. In the text box below the table, type in your user name (you'd be creating this later) and click "add"
  1. Give yourself a full access by checking the entire row for your user name
  1. Scroll all the way to the bottom, click `save`
  1. The configuration should look like the picture below:
    1. ![Jenkins-security-metrix](https://github.com/ShoppinPal/gcp-scripts/blob/master/images/jenkins-security.png)
  1. At this point you will be take back to the top page, and Jenkins is successfully secured.
5. Restart Jenkins: run `service jenkins restart` or `/etc/init.d/jenkins start` (may need to run prefixed with sudo if the system complains about only being able to run this as super user). 
6. Now you need to create an user for yourself
  1. click `login` at the top right portion of the page
  1. Choose `create an account`
  1. Use the username you've used in the above step and fill in the rest
7. If everything works smoothly, you are now logged on as yourself with full permissions.

## Jenkins Plugins
1. Install a plugin so that we can publish Over SSH
  1. Go to `https://<jenkins-server>/pluginManager/available` (alternately click the `Manage` button on left navigation > Plugin Manager), find the plugin "Publish over SSH plugin" and choose the option to Install without Restart at the bottom.  
2. Configuring SSH Servers
  1. `Jenkins > Configure System` ex: `https://<jenkins-server>.shoppinpal.com/configure`
  1. In `Publish Over SSH` add `SSH Server`
  1. Select server Name
  1. Hostname, IP, Domain
  1. User who will be running shell script (root)
    1. In our application node server generates configuration files and forever module does not detect these files if forever process is run by any other user then root. For application run by node command we can use any user. 

  1. Set remote directory where artifacts will be uploaded.
  1. In advanced setting check `Use password authentication, or use a different key` and give user password or key.
  1. You can create ssh-keys following instructions [here](https://github.com/ShoppinPal/gcp-scripts/wiki/Setup-Jenkins-on-Google-Cloud#create-ssh-key)
  1. Test Connection
  1. ![Add ssh-server](https://github.com/ShoppinPal/gcp-scripts/blob/master/images/ssh-server.png)

# Setup NodeJS
1. Install PPA
  1. You need to install the PPA in order to get access to its contents:
    * `curl -sL https://deb.nodesource.com/setup | sudo bash -`
2. Install NodeJS:
  * `sudo apt-get install nodejs`
3. Install build-essential: `sudo apt-get install build-essential`
4.  Confirm Installation

    ```
    # Confirm node and npm installations
    node -v
    npm -v
    ```
5. Install `forever` module:
  * `npm install forever -g`

# Setup Ruby
1. Update packages:
  * `sudo apt-get update`
2. Install RVM:
  * `\curl -L https://get.rvm.io | bash -s stable`
3. Add GPG signature:

    ```
    sudo gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
    \curl -L https://get.rvm.io | bash -s stable
    source /etc/profile.d/rvm.sh
    rvm requirements
  1. Install Ruby:
  • rvm install ruby
  1. Set Default:
  • rvm use ruby --default
  1. Install RubGems:
  • rvm rubygems current
  1. Install Rails:
  • gem install rails

Save a Jenkins VM image

  1. Use this command to save image

gcloud compute images create jenkins --source-disk <instance-disk(here jenkins-image)> --source-disk-zone ```

Load Jenkins Instance from Image

  1. Select recently saved image and use following command to create instance

    gcloud compute instances create NAME <name> --image "jenkins" --machine-type "n1-standard-1" --zone "<zone_id>"
    
  2. Configure ssl certificate following instructions here

  3. Restart nginx

/etc/init.d/nginx restart ```

4/ Start Jenkins /etc/init.d/jenkins restart

Save a Node VM image

  1. Use this command to save image

gcloud compute images create node --source-disk <instance-disk(here node-image)> --source-disk-zone ```

Load a VM from image

  1. Select recently saved image and use following command to create instance

    gcloud compute instances create NAME <name> --image "node" --machine-type "n1-standard-1" --zone "<zone_id>"
    
  2. Configure ssl certificate following instructions here

  3. Restart nginx

    /etc/init.d/nginx restart
    

Create ssh-key

To create ssh keys for user root run follow these instructions

  1. Switch user root

sudo su ```

  1. Change ssh directory

cd ~/.ssh 3. Generate ssh-key ssh-keygen -t rsa ``` 4. Enter file name 5. Enter passphrase (empty for no passphrase) 6. Add public key in authorized keys

```

cat id_rsa.pub >> authorized_keys ```

Clone this wiki locally