This lab will guide you through building a "BASIC" continuous delivery pipeline using Jenkins, Artifactory, and Cloud Foundry.
-
FORK (using the GitHub UI) and clone the [citytest repo](https://github.com/cqueiroz-pivotal/citytest):
$ git clone https://github.com/<YOUR_GIT_USERNAME>/citytest $ cd citytest
Login to your Jenkins instance at CloudBees. (if using Cloud Jenkins).
Navigate to Manage Jenkins > Manage Plugins > Available.
Install the following plugins:
Gradle
Mask Passwords
Extensible Choice Parameter Plugin
Artifactory Plugin
Parameterized Trigger Plugin
When you do, tell Jenkins to restart after plugin install.
-
Login to your Aritfactory Online instance.
-
Navigate to Admin > Security > General.
-
Activate encryption for passwords.
-
Next navigate to Admin > Security > Users.
-
Create a new user named deployer with a password of your choosing.
-
Once created, navigate to Admin > Security > Permissions.
-
Edit the Anything permissions target.
-
Select the Users tab, and check all permissions for your new user.
-
Logout, and then log back in as your new user.
-
Click on the account name in the top right hand corner of the screen.
-
Enter your password and click Unlock.
-
Copy your encrypted password and paste it somewhere safe.
-
Next, send your artifactory details to the CF Jenkins admin (Carlos Queiroz).
- URL
-
https://<<your artifactory hostname">[your" class="bare">https://<<your artifactory hostname].artifactoryonline.com/[your artifactory hostname]
(e.g. https://cqueiroz.artifactoryonline.com/cqueiroz) - Username
-
the user you created in artifactory
- Password
-
the encrypted password you copied a moment ago
-
Navigate back to Jenkins Home (http://pivotal-cloudbees.apj.fe.pivotal.io).
-
Login with your PCF account.
-
Click New Job, give it the name citytest-uniquetoken and select ``Build a free-style software project.'' Then click OK. Replace in the script below uniquetoken with something like your username!
-
Under Source Code Management, select Git, and supply your repository URL (e.g.
https://github.com/<YOUR_GIT_USERNAME>/citytest
). -
Under Build Triggers, select Poll SCM and provide the string * * * * *.
-
Under Build Environment, select Gradle-Artifactory Integration.
-
Select your Artifactory server.
-
Select libs-releases_local as the Publishing Repository.
-
Ensure the following are checked:
-
Capture and publish build info
-
Allow promotion of non-staged builds
-
Publish artifacts to Artifactory
-
Publish Ivy descriptors
-
-
-
Under Build, add a Invoke Gradle Script build step.
- Gradle Version
-
gradle-latest
- Build Step Description
-
build environment agnostic artifact
- Switches
-
-Pbuildversion=$BUILD_NUMBER
- Tasks
-
clean assemble
-
Save the config and try running the build by clicking ``Build Now''. Ensure that you see the artifact in Artifactory.
-
Navigate back to Jenkins Home (http://pivotal-cloudbees.apj.fe.pivotal.io).
-
Login with your PCF account.
-
Click New Job, give it the name citytest-uniquetoken-deploy and select ``Build a free-style software project.'' Then click OK.
-
Check This build is parameterized.
-
Click Add Parameter and choose Extensible Choice. Make sure to click on the arrow pointing down to allow multiple lines
- Name
-
BUILD_VERSION
- Description
-
The citytest build to promote.
- Choice Provider
-
System Groovy Choice Parameter
- Groovy System Script
-
import jenkins.model.* import hudson.model.* def getAllBuildNumbers(Job job) { def buildNumbers = [] (job.getBuilds()).each { build -> buildNumbers.add(build.getDisplayName().substring(1)) } return buildNumbers } def buildJob = Jenkins.instance.getItemByFullName('citytest-uniquetoken'); return getAllBuildNumbers(buildJob)
-
Under Build Environment, select Generic-Artifactory Integration.
-
Select your Artifactory server.
-
Select ext-releases_local as the Target Repository.
- Resolved Artifacts
-
libs-releases-local:citytest-uniquetoken/${BUILD_VERSION}/*⇒artifacts
-
Ensure Capture and Publish Build Info is checked.
-
-
Check Mask Passwords, then Add:
- Name
-
CF_PASSWORD
- Password
-
Your Pivotal Web Services Password
-
Under Build, add a Execute Shell build step. Remember to replace in the script below uniquetoken with something like your username!
- Command
-
#!/bin/bash PIVOTAL_WEB_SERVICE_USERNAME="<<<YOUR PWS Username >>>" PIVOTAL_WEB_SERVICE_ORGANIZATION="<<<YOUR PWS Organization>>>" PIVOTAL_WEB_SERVICE_SPACE="<<<YOUR PWS Space>>>" YOUR_UNIQUE_TOKEN="<<< Give a nice unique name for your App>>>" cf --version cf login -a https://api.run.pivotal.io -u ${PIVOTAL_WEB_SERVICE_USERNAME} -p ${CF_PASSWORD} -o ${PIVOTAL_WEB_SERVICE_ORGANIZATION} -s ${PIVOTAL_WEB_SERVICE_SPACE} DEPLOYED_VERSION_CMD=$(CF_COLOR=false cf apps | grep 'cities-${YOUR_UNIQUE_TOKEN}-' | cut -d" " -f1) DEPLOYED_VERSION="$DEPLOYED_VERSION_CMD" ROUTE_VERSION=$(echo "${BUILD_VERSION}" | cut -d"." -f1-3 | tr '.' '-') echo "Deployed Version: $DEPLOYED_VERSION" echo "Route Version: $ROUTE_VERSION" cf push "cities-${YOUR_UNIQUE_TOKEN}-$BUILD_VERSION" -i 1 -m 512M -n "cities-${YOUR_UNIQUE_TOKEN}-$ROUTE_VERSION" -d cfapps.io -p artifacts/citytest-${BUILD_VERSION}.jar --no-manifest echo "Is artifacts/citytest-${BUILD_VERSION}.jar exists?" pwd ls -l artifacts/citytest-${BUILD_VERSION}.jar cf map-route "cities-${YOUR_UNIQUE_TOKEN}-${BUILD_VERSION}" cfapps.io -n cities-${YOUR_UNIQUE_TOKEN} cf scale cities-${YOUR_UNIQUE_TOKEN}-${BUILD_VERSION} -i 2 if [ ! -z "$DEPLOYED_VERSION" -a "$DEPLOYED_VERSION" != " " -a "$DEPLOYED_VERSION" != "cities-${YOUR_UNIQUE_TOKEN}-${BUILD_VERSION}" ]; then echo "Performing zero-downtime cutover to $BUILD_VERSION" while read line do if [ ! -z "$line" -a "$line" != " " -a "$line" != "cities-${YOUR_UNIQUE_TOKEN}-${BUILD_VERSION}" ]; then echo "Scaling down, unmapping and removing $line" cf scale "$line" -i 1 cf unmap-route "$line" cfapps.io -n cities-${YOUR_UNIQUE_TOKEN} cf delete "$line" -f else echo "Skipping $line" fi done <<< "$DEPLOYED_VERSION" fi
-
Save the config and try running the build by clicking ``Build With Parameters''. Select the build you created in the previous step from the drop list. You should see the build deploy to Cloud Foundry.
-
Return to the citytest-unitoken project and click Configure.
-
Under Post Build Actions add a post-build action, selecting Trigger parameterized build on other projects.
- Projects to build
-
citytest-unitoken-deploy
- Predefined parameters
-
BUILD_VERSION=$BUILD_NUMBER
-
Save the config and try running the build by clicking ``Build Now''. You should see both builds executed coupled with a zero-downtime deploy of the app to Cloud Foundry.
-
In your local clone of the citytest project, open src/main/java/org/example/cities/VersionController.java in an editor.
-
Change the version number in the string.
-
Execute git commit -am "change version number".
-
Execute git push origin master.
-
You should see both builds executed coupled with a zero-downtime deploy of the app to Cloud Foundry!
-
Congrats! You’ve reached the end of the lab.