Skip to content

Commit

Permalink
add levant
Browse files Browse the repository at this point in the history
  • Loading branch information
qazz92 committed Nov 2, 2020
1 parent 20e6de2 commit 938f53e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 43 deletions.
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
FROM hendrikmaus/nomad-cli:0.12.7

RUN apk update && apk add bash gettext
RUN apk update && apk add bash gettext curl

RUN curl -L https://github.com/hashicorp/levant/releases/download/0.2.9/linux-amd64-levant -o /usr/bin/levant

RUN chmod +x /usr/bin/levant

WORKDIR /nomad-deploy
COPY deploy /nomad-deploy/deploy
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Small GitHub Action that runs `nomad job run ...` with the Nomad CLI.
Straightforward checkout then publish

```yaml
name: Run Tests & Publish Image
name: Deploy Nomad Job
on: [push]
jobs:
deploy:
Expand All @@ -23,18 +23,17 @@ jobs:
token: '{{ secrets.YOUR_NOMAD_SECRET }}'
address: '{{ secrets.YOUR_NOMAD_SERVER }}'
job: path/to/your/nomad/job/file
# optional:
additional_vars: "SOME_IMAGE=some-value REGISTRY_AUTH_TOKEN=some-other-value"
config: path/to/your/levant/config/file
```
## Parameters
* `token`: passed as `-token=` to the `nomad job run` command ([see here how to get one](https://www.nomadproject.io/guides/security/acl.html#acl-tokens))
* `address`: public address of a nomad server (passed as `-address=`)
* `job`: path to the nomad job specification to run ([defined according to the spec here](https://www.nomadproject.io/docs/job-specification/index.html))
* `additional_vars`: a string of env vars passed through to the nomad command that you can interpolate in your nomad job file ([see here](https://www.nomadproject.io/docs/runtime/interpolation.html)). Should be set as a string as if you would set them before the command normally (see example snippet above)
* `config`: path to the levant config file

## Todo

- [x] Verify it works
- [ ] Probably something else
- [ ] Validation config file & job file
13 changes: 6 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'Run Nomad Job'
description: 'Handles running of a given nomad job'
name: 'Nomad Deploy'
description: 'nomad deploy with levant'
inputs:
token:
description: 'Token used to authenticate with a nomad server'
Expand All @@ -10,18 +10,17 @@ inputs:
job:
description: 'Path to the nomad job file'
required: true
additional_vars:
description: 'Additional vars to set for running job run'
required: false
default: ''
config:
description: 'Path to the levant config file'
required: true
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.token }}
- ${{ inputs.address }}
- ${{ inputs.job }}
- ${{ inputs.additional_vars }}
- ${{ inputs.config }}
branding:
icon: 'upload-cloud'
color: 'green'
32 changes: 2 additions & 30 deletions deploy
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,12 @@
nomad_token=$1
nomad_address=$2
nomad_job_file_path=$3
replaced_nomad_job_file_path=/tmp/nomad-deploy/jobs/$(basename "$3")
additional_vars=$4

produce_replaced_nomad_file() {
# Make the temporary directory and create the new job file
mkdir -p /tmp/nomad-deploy/jobs
touch "$replaced_nomad_job_file_path"
if [[ $additional_vars == "" ]]
then
echo 'No additional vars found'
# Just copy the job file straight across, no replacement of env vars
cat "$nomad_job_file_path" > "$replaced_nomad_job_file_path"
else
local additional_vars_array=("$additional_vars")
local keys_to_replace=()
for pair in ${additional_vars_array[*]}
do
# collect the keys_to_replace of each additional var in an array
keys_to_replace+=("\$${pair%=*}")
done
# replace the contents of the job file with the required env vars
envsubst "${keys_to_replace[*]}" > "$replaced_nomad_job_file_path" < "$nomad_job_file_path"
fi
}
config_file_path=$4

if [ -f "$nomad_job_file_path" ]; then
echo "$nomad_job_file_path exists"
# export each of the additional_vars
for pair in $additional_vars; do export "${pair?}"; done
produce_replaced_nomad_file
export NOMAD_ADDR="$nomad_address"
export NOMAD_TOKEN="$nomad_token"
nomad job plan "$replaced_nomad_job_file_path"
nomad job run "$replaced_nomad_job_file_path"
levant deploy -var-file "$config_file_path" "$nomad_job_file_path"
else
echo "$nomad_job_file_path does not exist"
exit 1
Expand Down

0 comments on commit 938f53e

Please sign in to comment.