Skip to content

Instantly share code, notes, and snippets.

@remyd1
Created December 2, 2021 09:14
Show Gist options
  • Save remyd1/628666d96a762da093f3cea8e600800a to your computer and use it in GitHub Desktop.
Save remyd1/628666d96a762da093f3cea8e600800a to your computer and use it in GitHub Desktop.
gitlab-ci saltstack masters test+synchronization
---
sync:saltmaster1:
extends: synchronize
tags:
- saltmaster1
sync:saltmaster2:
extends: synchronize
tags:
- saltmaster2
sync:saltmaster3:
extends: synchronize
tags:
- saltmaster3
sync:saltmaster4:
extends: synchronize
tags:
- saltmaster4
only:
variables:
- $CI_COMMIT_MESSAGE =~ /master4/
sync:saltmaster5:
extends: synchronize
tags:
- saltmaster5
only:
variables:
- $CI_COMMIT_MESSAGE =~ /master5/
---
stages:
- requirements
- test
- synchronize
- cleaning
before_script:
# set environment for Linux
- export PATH="/root/.local/bin/:$PATH"
variables:
TMPDIR: "/tmp/salt-test"
GITSRC: "git@<url>:<path/to/saltstack_gitfs.git"
PRODDIR: "/opt/salt"
requirements:
tags:
- saltstack-internal
script:
- git --version
- pip3 install --upgrade --user yamllint
- pip3 install --upgrade --user salt-lint
- yamllint --version
- salt-lint --version
test:
tags:
- saltstack-internal
timeout: 5 minutes
dependencies:
- requirements
#allow_failure: true
script:
- >
rm -rf $TMPDIR &&
git clone $GITSRC $TMPDIR &&
find $TMPDIR -type f -name "*.sls" |
xargs --no-run-if-empty salt-lint --severity |
grep -A2 HIGH || [[ $? == 1 ]] &&
find $TMPDIR -type f -name "*.yml" |
xargs --no-run-if-empty yamllint --no-warnings
synchronize:
tags:
- saltstack-internal
timeout: 5 minutes
script:
- cd $PRODDIR # From /usr/local/sbin/clear.sh
- git pull
- salt-run fileserver.clear_cache backend=git
- salt-run cache.clear_all
- salt-run fileserver.update backend=git
- salt '*' saltutil.sync_all --async
- service salt-master restart
only: # Only run on main branch
variables:
- $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
cleaning:
tags:
- saltstack-internal
script:
- rm -rf $TMPDIR
dependencies:
- test
include: 'CI/.gitlab-ci-servers.yml'

This gist allows you synchronize your saltstack masters at each push (or conditionnaly based on gitlab-runners tags and commit messages), and test your salt (.sls files) and yaml (.yml files) code.

gitlab-runner needs to be installed on each master and configure to the right gitfs saltstack repository.

yamllint and salt-lint are needed on the master, but are installed in the requirements stage. It can failed in the firsts jobs/pipelines if it runs on a master where it is not installed yet (on the sync extension stage (look at .gitlab-ci-servers.yml)).

I am using it in a closed environment. Use it at your own risks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment