In this lab we will setup GitOps for CloudBees CI Configuration as Code (CasC) so that any CloudBeees CI configuration changes you make in source control will be made available to reload in your CloudBees CI managed controller (Jenkins instance).
In this lab you will:
cloudbees-ci-config-bundle repository with some CasC changes and add the necessary Pipeline Template marker file to the main branch, so it will trigger the main branch job for your config-bundle-ops Mutlibranch project.kubectl to copy files to the Operations Center pod, by publishing a notification event.



version of the bundle.yaml file was updated to 2, this is no longer required to trigger a reload of the configuration bundle from CloudBees CI Operations Center to your managed controller, but it is useful for tracking bundle changes.items.yaml was added to the list of files in the bundle.yaml file and the items.yaml file that is being added includes the configuration for the template-jobs folder and the config-bundle-ops Pipeline Template Catalog job. 
cloudbees-pipeline-policies plugin, that we will need for the next lab, was added to the plugins.yaml file.jenksin.yaml to provide ephemeral Kubernetes agents for maven tasks.
A job was created for the main branch of your copy of the cloudbees-ci-config-bundle repository because when you merged the pull request it added the .markerfile to your main branch and that triggered the config-bundle-ops Multibranch Pipeline template to create the job.
Error from server (Forbidden): pods "cjoc-0" is forbidden: User "system:serviceaccount:controllers:jenkins" cannot get resource "pods" in API group "" in the namespace "cbci"
namespace than Operations Center and no agent pod in the controllers namespace will have the permissions to copy files with kubectl (a CLI tool for Kubernetes) to the Operations Center Kubernetes pod. To fix this, you must update the CloudBees CI Configuration Bundle Pipeline Catalog template to trigger a job on another controller that is able to use kubectl to copy updated bundle files to Operations Center.Provisioning controllers and agents in a different namespace than Operations Center provides additional isolation and more security for Operations Center. By default, when controllers are created in the same namespace as Operations Center and agents, they can provision an agent that can run the pod exec command against any other pod in the namespace - including the Operations Center’s pod.
pipeline-template-catalog repository in your workshop GitHub Organization and open the Jenkinsfile for the CloudBees CI Configuration Bundle Pipeline Catalog template in the templates/casc-bundle/ directory. 
main branch:library 'pipeline-library'
pipeline {
agent none
options {
buildDiscarder(logRotator(numToKeepStr: '2'))
timeout(time: 60, unit: 'MINUTES')
}
stages {
stage('Publish CasC Bundle Update Event') {
agent { label 'default' }
when {
beforeAgent true
branch 'main'
}
environment { CASC_UPDATE_SECRET = credentials('casc-update-secret') }
steps {
gitHubParseOriginUrl()
publishEvent event:jsonEvent("""
{
'controller':{'name':'${BUNDLE_ID}','action':'casc_bundle_update','bundle_id':'${BUNDLE_ID}'},
'github':{'organization':'${GITHUB_ORG}','repository':'${GITHUB_REPO}'},
'secret':'${CASC_UPDATE_SECRET}',
'casc':{'auto_reload':'false'}
}
"""), verbose: true
}
}
}
}
Note that we replaced the previous steps with the publishEvent step (along with the gitHubParseOriginUrl pipeline library utility step). The publishEvent step with send a notification to a message bus on Operations Center and result in the triggering of any job that is configured to listen for that event. The configuration for the job that it triggers is available here.

After you commit the changes to the main branch, a GitHub webhook will trigger the updating of the CloudBees CI Workshop Template Catalog on your controller. This may take longer than it takes you to re-trigger the job, so if it fails again, just wait a few seconds and click Build Now again.

If you don’t see the new version available then click the Check for Updates button. Also, once you click Yes it may take a few minutes for the bundle update to reload.


For instructor led workshops please return to the workshop slides