Skip to content

Setup Jenkins on Google Cloud

jasonkuster edited this page Apr 11, 2017 · 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
    
  4. 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
  4. You can get instance ip by following command
    gcloud compute instances list
    

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

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. To access Jenkins you need to add firewall rule to open port in gce-networks

    1. Select Network Select Network
    2. Add firewall rule to allow Jenkins port Add firewall rule to allow Jenkins port
  4. Start Jenkins: run sudo /etc/init.d/jenkins start

  5. Setting Security

  6. 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

  7. Select Jenkins's own user database as the security realm.

  8. Place a check mark next to Allow users to sign up

  9. Select Matrix-based security as the authorization

  10. Give anonymous user the read access or uncheck all.

  11. In the text box below the table, type in your user name (you'd be creating this later) and click "add"

  12. Give yourself a full access by checking the entire row for your user name

  13. Scroll all the way to the bottom, click save

  14. The configuration should look like the picture below: 1. Jenkins-security-metrix

  15. At this point you will be take back to the top page, and Jenkins is successfully secured.

  16. 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).

  17. Now you need to create an user for yourself

  18. click login at the top right portion of the page

  19. Choose create an account

  20. Use the username you've used in the above step and fill in the rest

  21. 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

  2. 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.

  3. Configuring SSH Servers

  4. Jenkins > Configure System ex: https://<jenkins-server>.shoppinpal.com/configure

  5. In Publish Over SSH add SSH Server

  6. Select server Name

  7. Hostname, IP, Domain

  8. 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.

  9. Set remote directory where artifacts will be uploaded.

  10. In advanced setting check Use password authentication, or use a different key and give user password or key.

  11. You can create ssh-keys following instructions here

  12. Test Connection

  13. Add ssh-server

Setup NodeJS

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

  2. Confirm Installation

    # Confirm node and npm installations
    node -v
    npm -v
    
  3. Install forever module:

  • npm install forever -g

Setup Ruby

  1. Update packages:
  • sudo apt-get update
  1. Install RVM:
  • \curl -L https://get.rvm.io | bash -s stable
  1. 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
    
  2. 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  <your-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  <your-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
    
  2. Change ssh directory

    cd ~/.ssh
    
  3. Generate ssh-key

    ssh-keygen -t rsa
    
  4. Choose the default file name

  5. Do not set passphrase, leave it empty, because Jenkins cannot properly work with it, even when the correct passphrase is supplied to its configuration

  6. Add public key in authorized keys for your target servers where your jenkins machine will publish artifacts

  7. Copy public key you just created (id_rsa.pub)

  8. Add that key in end of authorized keys sudo su && vi ~/.ssh/authorized_keys

  9. You can copy and insert public key in authorized keys via this command (if you moved the entire file over): cat id_rsa.pub >> authorized_keys

Clone this wiki locally