├── .gitattributes ├── LICENSE ├── README.md ├── jobs ├── atlantis │ ├── Jenkinsfile │ ├── atlantis.groovy │ └── atlantis.nomad ├── fabio │ ├── Jenkinsfile │ ├── fabio.groovy │ └── fabio.nomad ├── hashi-ui │ ├── Jenkinsfile │ ├── hashi-ui.groovy │ └── hashi-ui.nomad └── replicator │ ├── Jenkinsfile │ ├── replicator.groovy │ └── replicator.nomad ├── util └── stop │ ├── Jenkinsfile │ └── stop.groovy └── variables ├── nonprod ├── atlantis.yaml ├── fabio.yaml ├── hashi-ui.yaml └── replicator.yaml └── prod ├── atlantis.yaml ├── fabio.yaml ├── hashi-ui.yaml └── replicator.yaml /.gitattributes: -------------------------------------------------------------------------------- 1 | *.nomad linguist-language=HCL 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 James Rasell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nomadfiles 2 | 3 | Continuous delivery examples for Nomad jobs. 4 | 5 | This is a repository to hold the various [Nomad](https://www.nomadproject.io/) job files I create and use. Included is also the corresponding [Jenkinsfile](https://jenkins.io/doc/book/pipeline/jenkinsfile/) for automated deployments and the [Jenkins JobDSL](https://github.com/jenkinsci/job-dsl-plugin) file for configuring the Jenkins job. 6 | 7 | This project sacrifices [DRY](https://en.wikipedia.org/wiki/Don't_repeat_yourself) in order to make the structure and usage clearer. 8 | 9 | ## Levant 10 | 11 | This example repository is configured to use [Levant](https://github.com/jrasell/levant), an open source templating and deployment tool for HashiCorp Nomad jobs that provides realtime feedback and detailed failure messages upon deployment issues. Levant was written because Nomad does not support some important templating and deployment features, and this repository represents a simple way to use the tool in a large scale, multi-environment setup. 12 | 13 | ## Directory Structure 14 | 15 | The directory structure is designed to be easy to navigate, yet highly descriptive of your Nomad deployments throughout you environments. 16 | 17 | ### Jobs Directory 18 | 19 | The `/jobs` holds the Nomad job specification and deployment scripts. The directory is split into sub-directories named by the Nomad job. 20 | 21 | * **job-name.groovy** The `.groovy` files are Jenkins JobDSL which can be used to configure the deployment job correctly and in an automated, codified manner. Details about the JobDSL plugin and basic usage information can be found on the [GitHub](https://github.com/jenkinsci/job-dsl-plugin) page. 22 | 23 | * **job-name.nomad** The Nomad job specification template file. 24 | 25 | * **Jenkinsfile** A Jenkinsfile is a text definition of the unit of work which the Jenkins deployment job will undertake. 26 | 27 | ### Variables Directory 28 | 29 | The `/variables` directory holds environment specific variables for each Nomad job. The directory can be split to better suit your needs, in this example, it is split by environment like `/variables/prod`. Within the subdirectory sits the variables files which correspond to a job that is held within the `/jobs` directory. 30 | 31 | The variables configured for the nonprod environment should work in a local development setup when Nomad is run with `nomad agent -dev`. 32 | 33 | ### Util Directory 34 | 35 | The `/util` directory contains utility scripts which can be used from CI infrastructure to run common tasks on the desired job such as stop. 36 | 37 | ## Contributing 38 | 39 | Any contributions are much appreciated. Please submit Pull Requests and Issues to the [nomadfiles project on GitHub](https://github.com/jrasell/nomadfiles). 40 | -------------------------------------------------------------------------------- /jobs/atlantis/Jenkinsfile: -------------------------------------------------------------------------------- 1 | node { 2 | ansiColor('xterm') { 3 | git url: 'git@github.com:jrasell/nomadfiles.git' 4 | def levant_docker = docker.image('jrasell/levant:0.0.4') 5 | levant_docker.pull() 6 | 7 | def nomad_job = "${env.JOB_NAME}".tokenize( '_' )[3] 8 | def nomad_env = "${env.JOB_NAME}".tokenize( '_' )[2] 9 | 10 | stage 'run job' 11 | levant_docker.inside { 12 | sh """ 13 | levant deploy\ 14 | -address=http://${nomad_url}:4646\ 15 | -var-file=./variables/${nomad_env}/atlantis.yaml\ 16 | ./jobs/${nomad_job}/${nomad_job}.nomad 17 | """ 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jobs/atlantis/atlantis.groovy: -------------------------------------------------------------------------------- 1 | def atlantis_envs = [ 2 | 'prod':'nomad.jrasell.com', 3 | 'nonprod':'nomad.np.jrasell.com' 4 | ] 5 | 6 | for (env in atlantis_envs) { 7 | pipelineJob("nomad_deploy_${env.key}_atlantis") { 8 | parameters { 9 | stringParam("nomad_url", "${env.value}", 'the URL of the Nomad HTTP API endpoint') 10 | } 11 | 12 | definition{ 13 | cpsScm { 14 | scm { 15 | git { 16 | branches('master') 17 | remote { 18 | url('https://github.com/jrasell/nomadfiles.git') 19 | } 20 | } 21 | } 22 | scriptPath("./jobs/atlantis/Jenkinsfile") 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /jobs/atlantis/atlantis.nomad: -------------------------------------------------------------------------------- 1 | job "atlantis" { 2 | datacenters = ["[[.datacenter]]"] 3 | region = "[[.region]]" 4 | type = "service" 5 | 6 | group "server" { 7 | 8 | count = 1 9 | 10 | task "server" { 11 | 12 | vault { 13 | policies = ["[[.vault_policy]]"] 14 | change_mode = "restart" 15 | } 16 | 17 | template { 18 | data = <