Integrating Chef with GitLab CI


#1

I’d like to be able keep a Chef Server’s cookbooks updated using GitLab.
It is my understanding that GitLab CI can perform this.
In short, I’d like that my team pushes cookbook updates or new cookbooks to GitLab and GitLab pushes the code to Chef or Chef pulls the new code from GitLab.
Anyone care to explain how can this be setup?
Thank you in advance.

Extra Info: for now I’d like to do this WITHOUT any sort of testing (workstation pushes code to gitlab, gitlab updates chef cookbooks)
Then I’ll worry about the testing before deployment.
Thanks


#2

Well, without testing, linting is mandatory (just to be sure there’s no error in your code before pushing it).

Then the upload task is as simple as doing a knife upload within your .gitlab-ci.yml.

My actual linting task for cookbooks without unit tests is as follow (using a docker image with chef-dk preinstalled and we’re not uploading to a chef-server for those cookbooks, so you’ll have to prepare your image or your gitlab-ci runner configure with chef-dk, a knife.rb and a user.pem file somewhere):

before_script:
  - /opt/chefdk/bin/chef shell-init bash

Linting:
  stage: test
  script:
  - /opt/chefdk/embedded/bin/chef --version
  - /opt/chefdk/embedded/bin/cookstyle --version
  - /opt/chefdk/embedded/bin/cookstyle
  - /opt/chefdk/embedded/bin/foodcritic --version
  - /opt/chefdk/embedded/bin/foodcritic . --exclude spec -P
  #- /opt/chefdk/embedded/bin/rspec spec

So the next step could be:

   Upload:
     stage: deploy
     script:
       - knife cookbook upload $CI_PROJECT_NAME -o ../

Assuming your project has the same name as the one in the cookbook’s metadata.rb


#3

Thank you for your reply.
My next question is: where are you running knife from? the GitLab CI server?
Do I need ChefDK on the GitLab server then?
Thanks, your reply was very helpful


#4

Actually I use docker instances for my builds, so the chef-dk is already installed in the docker image.

But you can use a shell runner and have chef-dk set up on the gitlab-ci runner (pay attention here the runner may not be the gitlab server itself)