Kohei Nozaki's blog 

Configuring automatic push by successfully build


Posted on Sunday Mar 01, 2015 at 11:19PM in Jenkins


Making Jenkins to push to an another remote repository if build finishes successfully.

Recipe

  1. Create a bare repository

    $ mkdir trader-stable.git
    $ cd trader-stable.git
    $ git init --bare
    Initialized empty Git repository in /Users/Shared/trader-stable.git/
    $
  2. Click Add Repository

    4a49f571 4135 43a8 915f e96db6eb902b
  3. Enter Repository URL

    e33f505c a52b 4752 83f9 44bf45b9db47
  4. Click Advanced…​

    507dc8b1 d86b 4cde b24c b59da15e84ed
  5. Enter stable to Name

    0f54d0a9 5317 40f5 9c60 ee13804a9904
  6. Click Add post-build actionGit Publisher

    44eeab12 4cf2 4c4a 81a9 785b99514659
  7. Check Push Only If Build Succeeds

  8. Click Add Tag

    60946cca e837 4245 a2c8 8e74c98c1f23
  9. Enter $BUILD_NUMBER to Tag to push

  10. Check Create new tag

  11. Enter stable to Target remote name

  12. Click Save

    2a9e7146 7663 414f 9499 91b65e028a43

Test

$ pwd
/Users/kyle/tmp/trader
$ echo 'push if succeeds test' >> hi.txt
$ git commit -am 'push if succeeds test'
$ git push origin master

Jenkins said Pushing tag 8 to repo stable

3e1e0e9a 2732 4054 8a20 82da0f67a7ab
$ git remote add stable /Users/Shared/trader-stable.git
$ git fetch stable
remote: Counting objects: 1, done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (1/1), done.
From /Users/Shared/trader-stable
 * [new tag]         8          -> 8
$


Configuring automatic build by push for git repository


Posted on Sunday Mar 01, 2015 at 07:15PM in Jenkins


We can start the build by send request to following URL.

http://YOURHOST/jenkins/job/PROJECTNAME/build

Recipe

In my case, Jenkins is deployed in /, and the server is running on port 18080. so the command to start a build of the job named trader will be:

curl http://localhost:18080/job/trader/build

So put following shell script into $ORIGIN_BARE_REPOSITORY/hooks/post-receive and execute chmod +x post-receive.

#!/bin/sh
curl http://localhost:18080/job/trader/build

Testing

Push some modification as follows:

$ echo hook test >> hi.txt
$ git add hi.txt
$ git commit -m 'hook test'
$ git push origin master

Check the build was run automatically as expected, and the output.

b0731e82 30d0 4638 b412 1e1bba91d3c9


Creating a simplest Jenkins job against a git repository


Posted on Sunday Mar 01, 2015 at 05:45PM in Jenkins


  1. Click New Item

    ecdf88ac ba06 4f30 aaa6 889b72d7a6be
  2. Enter trader in Item name and select Build a free-style software project

    81952af0 a4ae 40d7 a387 e02647d31f23
  3. Select Git and enter Repository URL

    9ac2c2af 2a4a 4b4d 90b4 7eed0a73433e
  4. Click Add build step - Execute shell

    dc7e02f0 9e6f 47ed 8049 e7a1a3078c11
  5. Enter cat hi.txt into Command

    95537b80 e9a0 4e42 89df d8dfb548de90
  6. Click Save

    659ed88d 0455 400c 91e4 f4b3ab714ea3
  7. Click Build Now

    9d4ef446 7b26 4a7a a2a2 010268e05e84
  8. Click a Build History which just created by build

    44f32959 4d18 4a89 8895 c35c1861be51
  9. Click Console Output

    f3da0b60 f9a2 4963 a12f 4fc3a8c1f392


Deploying after integration test with Jenkins


Posted on Wednesday Mar 05, 2014 at 04:09PM in Jenkins


Requirements

  • I want to deploy the production resource after integration test.
  • But some resources are environment-specific.
  • So, after integration test succeeded, I want to replace such resources for production version.

How to achieve it?

  • The deployment job is need to be a parameterized job.
  • After integration test completed, then another mvn starts at post-build step of Jenkins.
    • This mvn does only package goal with no tests.
  • Deployment will be executed by SSH deploy plugin at post-build action of Jenkins.

How to apply this way to a Jenkins job?

  1. Create a parameterized job that can integration-test and deployment be done successfully.
  2. Go to job configuration page
  3. Click “Add post-build step”
  4. Click “Invoke top-level Maven targets”
  5. Select “Run only if build succeeds”
  6. Select Maven Version “Default”
    • Not (Default) which one with enclosed in parentheses. it won't work.
  7. Enter “Goals”
  8. Click “Advanced…”
  9. Enter “POM”, “Properties”.
    • “hogeApp.environmentId=prod” triggers to Maven to include production resources in my pom.xml.
  10. Ensure that there is a post-build action which deploys the artifact to the production environment.
  11. Click “Save” of bottom of the page.

Log

Jenkins job output

...
[INFO] --- maven-failsafe-plugin:2.16:verify (default) @ switch ---
[INFO] Failsafe report directory: /Users/Shared/Jenkins/Home/jobs/SwitchDeploy/workspace/switch/target/failsafe-reports
[JENKINS] テスト結果の記録
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.169s
[INFO] Finished at: Wed Mar 05 16:25:53 JST 2014
[INFO] Final Memory: 22M/318M
[INFO] ------------------------------------------------------------------------
[JENKINS] Archiving /Users/Shared/Jenkins/Home/jobs/SwitchDeploy/workspace/switch/pom.xml to org.nailedtothex/switch/0.0.1-SNAPSHOT/switch-0.0.1-SNAPSHOT.pom
[JENKINS] Archiving /Users/Shared/Jenkins/Home/jobs/SwitchDeploy/workspace/switch/target/switch.war to org.nailedtothex/switch/0.0.1-SNAPSHOT/switch-0.0.1-SNAPSHOT.war
channel stopped
[workspace] $ /Users/kyle/apps/apache-maven-3.1.1/bin/mvn -f switch/pom.xml -Dtag=v0.1 -Dmaven.test.skip=true -DhogeApp.environmentId=prod clean package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building switch 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ switch ---
[INFO] Deleting /Users/Shared/Jenkins/Home/jobs/SwitchDeploy/workspace/switch/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ switch ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ switch ---
[INFO] Compiling 1 source file to /Users/Shared/Jenkins/Home/jobs/SwitchDeploy/workspace/switch/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ switch ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ switch ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.16:test (default-test) @ switch ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:2.4:war (default-war) @ switch ---
[INFO] Packaging webapp
[INFO] Assembling webapp [switch] in [/Users/Shared/Jenkins/Home/jobs/SwitchDeploy/workspace/switch/target/switch]
[INFO] Processing war project
[INFO] Copying webapp webResources [/Users/Shared/Jenkins/Home/jobs/SwitchDeploy/workspace/switch/src/main/webapp/_prod] to [/Users/Shared/Jenkins/Home/jobs/SwitchDeploy/workspace/switch/target/switch]
[INFO] Copying webapp resources [/Users/Shared/Jenkins/Home/jobs/SwitchDeploy/workspace/switch/src/main/webapp]
[INFO] Webapp assembled in [46 msecs]
[INFO] Building war: /Users/Shared/Jenkins/Home/jobs/SwitchDeploy/workspace/switch/target/switch.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.595s
[INFO] Finished at: Wed Mar 05 16:26:01 JST 2014
[INFO] Final Memory: 15M/245M
[INFO] ------------------------------------------------------------------------
SSH: Connecting from host [kyle-no-MacBook.local]
SSH: Connecting with configuration [osxserver] ...
SSH: EXEC: STDOUT/STDERR from command [/Users/kyle/wildfly-8.0.0.Final/bin/jboss-cli.sh --connect --controller=localhost:49990 --command="deploy switch.war --force"] ...
SSH: EXEC: completed after 3,256 ms
SSH: Disconnecting configuration [osxserver] ...
SSH: Transferred 1 file(s)
Finished: SUCCESS
  • Deployment succeeded as expectedly.
  • After integration-test completed, another mvn was kicked, and it builds the package for production without testing once again.

Check the deployed application

kyle-osxserver:~ kyle$ telnet localhost 48080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /switch/ HTTP/1.0

HTTP/1.0 200 OK
Connection: close
X-Powered-By: Undertow 1
X-Powered-By: JSP/2.2
Set-Cookie: JSESSIONID=gLpwDJ37tu-Uoxd0r9pWD2wH.kyle-osxserver; path=/switch
Server: Wildfly 8
Content-Type: text/plain;charset=UTF-8
Date: Wed, 05 Mar 2014 07:27:29 GMT

This value came from context-param defined in /src/main/webapp/_prod/WEB-INF/web.xml
This value came from /src/main/resources/common.properties
This value came from /src/main/resources/_prod/env-specific.properties
Connection closed by foreign host.
kyle-osxserver:~ kyle$ 

References

  1. Jenkins executing maven from incorrect path - Stack Overflow
  2. java - Maven packaging without test (skip tests) - Stack Overflow
  3. Jenkins users - pre-build maven step not working with default maven?
  4. [#JENKINS-755] Default JDK meaning in project options is confusing. - Jenkins JIRA


Deploying with Jenkins Publish Over SSH Plugin


Posted on Friday Feb 28, 2014 at 09:28AM in Jenkins


Environment

  • WildFly 8.0.0.Final
  • Publish Over SSH Plugin 1.11
  • Jenkins 1.551
  • OS X 10.9.2

Requirements

  • Resources are available in git repository
  • The job is parametarized and can specify the tag to be processed
  • The job will build a WAR and deploy it to the remote application server through ssh

Install the plugin

  • Install “Publish Over SSH Plugin 1.11” at Plug-in page.

Create a key-pair

kyle-no-MacBook:~ jenkins$ whoami
jenkins
kyle-no-MacBook:~ jenkins$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Shared/Jenkins/.ssh/id_rsa): 
Created directory '/Users/Shared/Jenkins/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/Shared/Jenkins/.ssh/id_rsa.
Your public key has been saved in /Users/Shared/Jenkins/.ssh/id_rsa.pub.
The key fingerprint is:
[...] 
The key's randomart image is:
[...]
kyle-no-MacBook:~ jenkins$ ls -l .ssh
total 16
-rw-------  1 jenkins  jenkins  1679 Feb 28 11:55 id_rsa
-rw-r--r--  1 jenkins  jenkins   411 Feb 28 11:55 id_rsa.pub
kyle-no-MacBook:~ jenkins$ 

Put the public-key to the server

kyle-osxserver:.ssh kyle$ cat >> authorized_keys << EOF
> ssh-rsa [...] jenkins@kyle-no-MacBook.local
> EOF

Configure

  1. Click “Manage Jenkins”
  2. Click “Configure System”
  3. Go to “Publish over SSH” section
  4. Enter “/Users/Shared/Jenkins/.ssh/id_rsa” to “Path to Key”
  5. Click “Add” at “SSH Servers”
  6. Enter any logical name to “Name”
  7. Enter IP Address or Hostname of the server to “Hostname”
  8. Enter the user name to login to “Username”
  9. Enter any directory to “Remote Directory”
  10. Click “Test Configuration”
  11. Click “Save” at bottom of the page

Create a job

  1. Create or copy a job that can build the WAR correctly.
  2. As I wrote in How to specify a Git tag to be processed, make a job to can specify a tag to be processed.
  3. Click “Add post-build action”
  4. Click “Send build artifacts over SSH”
  5. Enter “Source files”
  6. Enter “Remove prefix”
  7. Enter “Exec command”

WildFly deploy command example:

/Users/kyle/wildfly-8.0.0.Final/bin/jboss-cli.sh --connect --controller=localhost:49990 --command="deploy hoge-0.0.1-SNAPSHOT.war --force"

Create a tag

kyle-no-MacBook:stock kyle$ git tag v0.1
kyle-no-MacBook:stock kyle$ git tag
v0.1
kyle-no-MacBook:stock kyle$ git show v0.1
commit 87a93c8039bd77b8eb8cbf8fbb522705c6451f1e
[...]

Run

Run the job that created with the parameter of name of tag.

  1. Click “Build with Parameters”

  2. Select a tag to be processed and Click “Build”

Log

...
[JENKINS] Archiving /Users/Shared/Jenkins/Home/jobs/HogeDeploy/workspace/hoge/pom.xml to org.nailedtothex/hoge/0.0.1-SNAPSHOT/hoge-0.0.1-SNAPSHOT.pom
[JENKINS] Archiving /Users/Shared/Jenkins/Home/jobs/HogeDeploy/workspace/hoge/target/hoge-0.0.1-SNAPSHOT.war to org.nailedtothex/hoge/0.0.1-SNAPSHOT/hoge-0.0.1-SNAPSHOT.war
channel stopped
SSH: Connecting from host [kyle-no-MacBook.local]
SSH: Connecting with configuration [osxserver] ...
SSH: EXEC: STDOUT/STDERR from command [/Users/kyle/wildfly-8.0.0.Final/bin/jboss-cli.sh --connect --controller=localhost:49990 --command="deploy /Users/kyle/hoge-0.0.1-SNAPSHOT.war --force"] ...
SSH: EXEC: completed after 4,357 ms
SSH: Disconnecting configuration [osxserver] ...
SSH: Transferred 1 file(s)
Email was triggered for: Always
Sending email for trigger: Always
Sending email to: kyle@example.com
Finished: SUCCESS

References

  1. Publish Over - Jenkins - Jenkins Wiki
  2. jenkinsによるWebSphereへのEARファイルデプロイ - 遅れてやってきたプログラマーの小言
  3. Jenkins、Webから再起動する
  4. Git - タグ
  5. Deploy to WildFly using jboss-cli (Tech Tip #11) | Miles to go 2.0 … | Planet JBoss Community