Monday, April 27, 2015

Installing Jenkins on AWS with java play, sbt and assembla

I recently tried to install a jenkins server in amazon cloud for our company's projects.
It took me a while but I wrote it all down for reference.
If you are having trouble setting Jenkins up this might help you:

Installing Jenkins on Ubuntu with Java Plat and Assembla on AWS
1)      Start a new ec2 machine with Basic Linux AMI
2)      In the security groups set a rule for custom TCP rule with port number 8080
3)      Installation steps from: http://sanketdangi.com/post/62715793234/install-configure-jenkins-on-amazon-linux (replace yum with apt-get if Ubuntu)

            a.       Login to your Amazon Linux instance
            b.      Become root using “sudo su -” command
c.       Install jenkins
                                                               i.      On red hat
1.       yum update
2.       wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
3.       rpm --import http://pkg.jenkins-ci.org/redhat-stable/jenkins-ci.org.key or sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key (whatever works with no errors)
4.       yum install Jenkins
                                                             ii.      on ubuntu
1.       use apt-get (TODO complete)
d.      service Jenkins start
e.      chkconfig Jenkins on
4)      open browser and go to the server url port 8080 (http://<url>:8080)
5)      go to manage Jenkins à manage plugins à available tab (for all plugins click install without restart if possible)
a.       install git plugin
b.      install assembla plugin
c.       install sbt plugin
d.      ssh agent plugin
e.      ssh credentials plugin
f.        play-plugin
6)      restart Jenkins using service Jenkins restart
7)      create an assembla webhook (for running build for every commit)
a.       login to your assembla account
b.      go to assembla à admin (you need admin privilages) à tools à more à webhooks à add
c.       go to webhooks settings
d.      click configure a webhook
e.      In Webhook, enter the following settings:
                                                               i.      Title=Anything
                                                             ii.      Enabled=true
                                                            iii.      Authentication type=basic
                                                           iv.      External url=http://<server URL>:8080/git/notifyCommit?url={repository_url}&branch={branch} 
                                                             v.      Http method=GET
                                                           vi.      Context type=text/plain
                                                          vii.      Post updates about=check "Code Commits"
                                                        viii.      Click Add


8)      Login as Jenkins : sudo su jenkins -s /bin/bash
9)      Make sure you have a ssh key or generate a new one:
First, we need to check for existing SSH keys on your computer. Open Git Bash and enter:
ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
Check the directory listing to see if you already have a public SSH key. By default, the filenames of the public keys are one of the following:
id_dsa.pub
id_ecdsa.pub
id_ed25519.pub
id_rsa.pub

Step 2: Generate a new SSH key

With Git Bash still open, copy and paste the text below. Make sure you substitute in your GitHub email address.
ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.
We strongly suggest keeping the default settings as they are, so when you're prompted to "Enter a file in which to save the key", just press Enter to continue.
# Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
You'll be asked to enter a passphrase.
# Enter passphrase (empty for no passphrase): [Type a passphrase]
# Enter same passphrase again: [Type passphrase again]
Tip: We strongly recommend a very good, secure passphrase. For more information, see "Working with SSH key passphrases".
After you enter a passphrase, you'll be given the fingerprint, or id, of your SSH key. It will look something like this:
# Your identification has been saved in /Users/you/.ssh/id_rsa.
# Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com

Copy the generated key to /var/lib/Jenkins/.ssh (you need admin priviledges) if it is not already there you can also generate a dsa key: ssh-keygen -t dsa


10)   Configure credentials
a.       Go to Jenkins->Manage Jenkins->Manage Credentials
b.      Use the global domain (domains are used to supply a set of rules to when we can use credentials)
c.       Scope=Global
d.      Username=jenkins
e.      Private Key=From the Jenkins master ~/.ssh or enter the location
f.        Click advance tab and enter your passphrase
g.       Click save
11)   Add the ssh key to assembla:
a.       Go to user menu (top left side) à profile à ssh keys from sidebar
b.      Connect to the server and enter: cat .ssh/id_rsa.pub
c.       Copy the output to clipboard and paste it to the text box (if using windows make sure there are no newlines cause windows terminal sucks)
d.      Click add key
12)   Configure git globals:
a.       git config —global user.name “Your Name”
b.      git config —global user.email email@email.com (email used for assembla)
13)   download sbt launcher using wget (you can use this link: http://www.scala-sbt.org/0.13/tutorial/Manual-Installation.html) save the file in /usr/bin            
14)   configure SBT
               a. Press the Manage Jenkins link and then the Configure System
               b. Go to sbt section
               c. Click add sbt
               d. Select the path to the launcher
15)   Install junit
a.       Redhat
                                                               i.      Yum install junit
b.      Debian/Ubuntu
                                                               i.      Sudo apt-get install junit
16)   Configure ssh-agent by creating the .profile file on the Jenkins home directory and copy-paste the following into it:


# Note: ~/.ssh/environment should not be used, as it already has a different purpose in SSH.
env=~/.ssh/agent.env
# Note: Don't bother checking SSH_AGENT_PID. It's not used
# by SSH itself, and it might even be incorrect
# (for example, when using agent-forwarding over SSH).
agent_is_running() {

if [ "$SSH_AUTH_SOCK" ]; then
# ssh-add returns:
# 0 = agent running, has keys
# 1 = agent running, no keys
# 2 = agent not running
  ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]
else
  false
fi
}
agent_has_keys() {
  ssh-add -l >/dev/null 2>&1
}

agent_load_env() {
  . "$env" >/dev/null
}

agent_start() {
  (umask 077; ssh-agent >"$env")
  . "$env" >/dev/null
}
if ! agent_is_running; then
  agent_load_env
fi

if ! agent_is_running; then
  agent_start
  ssh-add
elif ! agent_has_keys; then
  ssh-add
fi

unset env

17)   Install Play!:
b.      unzip typesafe-activator-1.3.2-minimal.zip
c.       export PATH=$PATH:/var/lib/jenkins/activator-1.3.2-minimal
d.      activator
e.      go to manage jenkine à configure Jenkins
f.        add Play!
g.       Set path to activator
18)   for running scripts:
a.       login as root (sudo su -)
b.      vi /etc/sudoers
c.       comment out Defaults    requiretty
d.      vi /etc/sudoers.d/cloud-conf
e.      add jenkins ALL=(ALL) NOPASSWD: ALL
19)   project configuration
a.       go to dashboard
b.      click new item
c.       enter name
d.      select freestyle project
e.      click ok
f.        set SCM
                                                               i.      select source code management à git
                                                             ii.      enter repository url
                                                            iii.      branches to build set it to blank (will build all branches)
g.       build triggers
                                                               i.      poll SCM
                                                             ii.      keep schedule blank

                  h. build:
                               i. add build step
                               ii. using sbt:
                                      1. select build using sbt
                                      2. in action enter compile test
                                      3. TODO – how to use build.sbt
                               iii. Using activator (java play!)
                                      1. Select Play!
                                      2. Add compile and test
                              iv. Using a script:
                                      1. Write a shell script in the workspace directory
                                      2. Chmod 777 <name_of_script>
                                      3. Select shell script
                                      4. Name of script
                    i. post build actions
                                      1. select publish Junit result report
                                      2. select e-mail notification
                                              i. check both boxes
                                              ii. enter moderators e-mails

No comments:

Post a Comment