Over a million developers have joined DZone.

Build a Swift Pipeline with Jenkins Workflow

How to use Docker and the Jenkins Workflow plugin to run Apple's open-source Swift language.

· DevOps Zone

Download your copy of Continuous Delivery with Jenkins and learn how you can deliver better, software faster. Brought to you in partnership with CloudBees.

Jenkins Workflow is awesome. Just add a Jenkinsfile alongside your source and you can easily build a pipeline for your project. With Workflow, CloudBees and the Jenkins community are providing us with a first class tool to enable continuous delivery pipelines with Jenkins.

If you haven’t already heard, Apple open sourced Swift recently (Dec. 3, 2015). The Swift language is now available to download on GitHub. In today’s post we are going to take a quick look at running Apple Swift in a Jenkins Workflow.

But wait… don’t we need an OS X build node with XCode to do that? What a hassle, right? Well, actually you don’t need XCode or OS X anymore to build and run Swift code. You can read about the details on the Apple Swift GitHub page, but Swift is now officially supported on Ubuntu Linux. OK, so you can build and run Swift on Ubuntu, but that means that I need to configure a Ubuntu Linux build node with Swift. I don’t have time for that.

Now, here’s the awesome part: Jenkins Workflow has excellent support for Docker; hopefully you have already been using it (if not you can read more about Jenkins and Docker on CloudBees website). And there is already a Docker image available for Ubuntu Linux with Swift installed. It only took me a few minutes to:

  1. Create a new GitHub repo 
  2. Create a Jenkinsfile 
  3. Run a new ‘Folder Computation’ on my Jenkins GitHub Organization project (please see this great post by Kishore Bhatia regarding the new Branch Source and Multibranch Workflow features for Jenkins)
  4. Watch the job execute a trivial Swift command

Here is the Jenkinsfile (Disclaimer: I’m not a Swift developer, yet):

//run this job on a docker enabled build node
node('docker-cloud') {
  //this will pull the swift image, run it, and execute the following block of steps
  docker.image('swiftdocker/swift:latest').inside {
    //trivial, yes - but it works
    sh 'swift --version'
  }
}

Here is part of the console output from running the Jenkins job:

Status: Downloaded newer image for swiftdocker/swift:latest
[Workflow] Run build steps inside a Docker container : Start
$ docker run -t -d -u 1000:1000 -w /home/jenkins/workspace/team-productivity/beedemo-sa/apple-swift-on-linux/master -v /home/jenkins/workspace/team-productivity/beedemo-sa/apple-swift-on-linux/master:/home/jenkins/workspace/team-productivity/beedemo-sa/apple-swift-on-linux/master:rw -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** swiftdocker/swift:latest cat
[Workflow] withDockerContainer {
[Workflow] sh
[master] Running shell script
+ swift --version
Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c)
Target: x86_64-unknown-linux-gnu
[Workflow] } //withDockerContainer
$ docker stop 0326b40b0bf1cb1ae5c23595e9892a86a422a1c4cb8e9489465c56b552cb6b50
$ docker rm -f 0326b40b0bf1cb1ae5c23595e9892a86a422a1c4cb8e9489465c56b552cb6b50
[Workflow] Run build steps inside a Docker container : End
[Workflow] } //node
[Workflow] Allocate node : End
[Workflow] End of Workflow

GitHub has been notified of this commit’s build result

Finished: SUCCESS

I know this is a trivial Swift example, but the fact that you can so easily and quickly stand up a Swift build environment for Jenkins, well, that is pretty awesome!

Note: If you aren’t already using Docker with Jenkins and/or Workflow, here are some resources that will help you get started:

  • http://jenkins-ci.org/node/644
  • https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Docker+Workflow+Plugin
  • https://github.com/jenkinsci/docker-workflow-plugin
  • https://www.cloudbees.com/blog/orchestrating-workflows-jenkins-and-docker
  • Discover how Docker, Jenkins and DevOps are coming together to accelerate innovation. Brought to you in partnership with CloudBees.

    Topics:
    devops ,continuous delivery ,continuous integration ,docker ,jenkins ,swift ,mobile

    Published at DZone with permission of Heidi Gilmore, DZone MVB. See the original article here.

    Opinions expressed by DZone contributors are their own.

    The best of DZone straight to your inbox.

    SEE AN EXAMPLE
    Please provide a valid email address.

    Thanks for subscribing!

    Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
    Subscribe

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}