voxpupuli / puppet-r10k

Setup and configure r10k for use with git based environments in puppet
https://forge.puppet.com/puppet/r10k
Apache License 2.0
98 stars 168 forks source link

webhook pulling wrong version of git repo #551

Open jmcdonal opened 3 years ago

jmcdonal commented 3 years ago

Affected Puppet, Ruby, OS and module versions/distributions

How to reproduce (e.g Puppet code you use)

Stock rpm install, out of the box configuration.

What are you seeing

The github webhook is sent and received by the puppet webhook and r10k is deploying the environment but it is using an older copy of the repo. e.g. : Mar 10 13:14:37 hi-automation bundle: 2021-03-10T19:14:37.498Z 1319 TID-hnuin Deploy::EnvironmentWorker JID-e1230c37c97b53a50a57978c INFO: Environment production is now at 24a5fdedf2e6d655624dbded615394cebb645340

but the head is at: 72c9b6322aee2531e13c2796f8127baa34b492d6 $ git log commit 72c9b6322aee2531e13c2796f8127baa34b492d6 (HEAD -> production, origin/production, origin/HEAD) Author: Date: Wed Mar 10 13:14:30 2021 -0600

test

commit 24a5fdedf2e6d655624dbded615394cebb645340 Author: Date: Wed Mar 10 11:05:06 2021 -0600

test

What behaviour did you expect instead

I was expecting that the system would pull the latest version of the repo, not the previous version. It also seems to be stuck on the older version, so if I make a series of commits in quick secession, the repo is 'stuck' at this version.

Is there something I need to purge on the system after each run?

Output log

Any additional information you'd like to impart

alexjfisher commented 3 years ago

Sounds like you might have environment caching enabled?

If so, you can configure r10k with a postrun script that calls the puppetserver admin api to clear the cache after each deployment.

Here's an example from a profile::r10k class.

$postrun_script = '/usr/local/bin/r10k_postrun'
file { $postrun_script:
  ensure  => file,
  owner   => 'puppet',
  group   => 'puppet',
  mode    => '0700',
  content => template('profile/r10k/r10k_postrun.erb'),
}

class { 'r10k':
  version => '3.1.0',
  sources => {
    'puppet' => {
      'remote'  => 'git@gitlab.example.com:puppet/control-repo.git',    
      'basedir' => '/etc/puppetlabs/code/environments',
    },
 },
 postrun => [
   $postrun_script,
   '$modifiedenvs', # Occurrences of the string $modifiedenvs in the postrun command will be replaced with the current environment(s) being deployed.
  ],
}

The postrun script is a template. (My version also pokes Foreman, but I've removed those lines for clarity). (If you're not already, you definitely want to be calling generate types as well as clearing the environment cache.)

#!/bin/bash

# R10k calls this script with a list of environments it has updated.
# For each environment, `puppet generate types is called` and the environment cache invalidated.

for environment in "$@"
do
  /opt/puppetlabs/bin/puppet generate types --codedir /etc/puppetlabs/code --environment $environment
  /bin/curl --noproxy localhost -i -k --cert /etc/puppetlabs/puppet/ssl/certs/<%= @facts['fqdn'] %>.pem --key /etc/puppetlabs/puppet/ssl/private_keys/<%= @facts['fqdn'] %>.pem --cacert /etc/puppetlabs/puppet/ssl/certs/ca.pem -X DELETE "https://localhost:8140/puppet-admin-api/v1/environment-cache?environment=${environment}"
done