Exemple simple de déploiement Nomad à partir de Gitlab.
Le gitlab-ci.yml
stages:
- build_doc
- build_image
- deploy
image: docker:latest
variables:
NOMAD_VERSION: 1.8.3
IMAGE_NAME: node-app1
NOMAD_TEMPLATE_NAME: app1.tpl.hcl
build_doc:
stage: build_doc
image: node:latest
tags:
- docker
script:
- npm run build
artifacts:
paths:
- build
expire_in: 1 days
cache:
key: "$CI_COMMIT_REF_NAME"
paths:
- ./
policy: pull
build_image:
stage: build_image
tags:
- docker
cache:
key: "$CI_COMMIT_REF_NAME"
paths:
- ./
policy: pull
script:
- docker login -u $REGISTRY_USER -p $REGISTRY_PASSWORD $REGISTRY_URL
- docker build -q --tag $REGISTRY_URL/$IMAGE_NAME:$CI_COMMIT_SHA .
- docker push -q $REGISTRY_URL/$IMAGE_NAME:$CI_COMMIT_SHA
deploy:
stage: deploy
tags:
- shell
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
before_script:
- export NOMAD_TOKEN=$NODE1_BOOTSTRAP_TOKEN
- export NOMAD_ADDR=https://$NODE1_URL
- wget https://releases.hashicorp.com/nomad/${NOMAD_VERSION}/nomad_${NOMAD_VERSION}_linux_amd64.zip
- unzip nomad_${NOMAD_VERSION}_linux_amd64.zip
script:
- envsubst '${REGISTRY_USER} ${REGISTRY_PASSWORD} $REGISTRY_URL}/${IMAGE_NAME} ${CI_COMMIT_SHA}' < ${NOMAD_TEMPLATE_NAME} > ${NOMAD_TEMPLATE_NAME/.tpl/}.hcl
- ./nomad run ${NOMAD_TEMPLATE_NAME/.tpl/}.hcl
Le template nomad
job "app1" {
datacenters = ["*"]
type = "service"
group "app1_web_grp" {
count = 1
network {
port "app1" {
to = 80
}
}
task "app1_web" {
driver = "docker"
service {
name = "app1"
provider = "consul"
port = "app1"
check {
name = "app1_probe"
type = "tcp"
interval = "10s"
timeout = "1s"
}
tags = [
"traefik.enable=true",
"traefik.http.routers.app1.rule=Host(`app.example.com`)"
]
}
env {
STATIC_FILES_ROOT = "/usr/share/nginx/html"
}
config {
image = "${REGISTRY_URL}/${IMAGE_NAME}:${CI_COMMIT_SHA}"
force_pull = false
auth {
username = "${REGISTRY_USER}"
password = "${REGISTRY_PASSWORD}"
}
ports = ["app1"]
volumes = [
"local/nginx-default.conf:/etc/nginx/conf.d/default.conf",
]
}
template {
data = <<EOH
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html/;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
EOH
change_mode = "restart"
destination = "local/nginx-default.conf"
env = false
}
}
}
}