<p>i am working on integrating <strong>jenkins</strong> on <strong>Google Kubernetes</strong>. Want a pipeline for my project which will perform following steps:</p>

<ul>
<li>Build JAR from maven </li>
<li>Build docker image and push to google registry</li>
<li><strong>kubectl apply -f</strong> commands or <strong>Helm commands</strong> to run for redeploys,upgrade, downgrade etc</li>
</ul>

<p>I am familiar with above commands individually but i am new to this type of pipeline where i can run them but everytime jenkins try to teach me that you are in a container, not on kubernetes lol</p>

<p>I have a google cloud shell and i cannot directly install on that machine. So i have to somehow find a way to integrate jenkins to pass those commands directly to GKE environment.</p>

<p>i just want a proper step by step guide for anyone not familiar with how to achieve following points:</p>

<blockquote>
  <ul>
  <li>Have GKE</li>
  <li>Have jenkins pod on it </li>
  <li>Know all commands to execute (docker, gcloud, kubectl, helm etc)</li>
  <li><strong>Just need an integration between jenkins and GKE to utilize all above</strong></li>
  </ul>
</blockquote>

<p>Kindly bear me if i am unable to explain that much. Ask anything else you need to resolve this. Thank</p>

<p>If your Jenkins pod is running inside GKE, just go for <a href="https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#accessing-the-api-from-a-pod" rel="nofollow noreferrer">accessing the api from a pod</a> and wrap your last step into small python/go script.</p>

<p>If you prefer to use <code>kubectl</code> you could:
set up correct rbac, for example (this one allow to list pods and servies):</p>

<pre><code>kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: list
rules:
- apiGroups: [""]
  resources: ["services", "pods"]
  verbs: ["get", "list"]

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: list
subjects:
- kind: ServiceAccount
  name: default
  namespace: default
roleRef:
  kind: ClusterRole
  name: svc-list
  apiGroup: rbac.authorization.k8s.io
</code></pre>

<p>and then</p>

<pre><code>kubectl run --restart=Never --rm -it ubuntu --image ubuntu:18.04
</code></pre>

<pre><code>{
   apt-get update
   apt-get install -y apt-transport-https curl gnupg
   curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg |  apt-key add -

  cat &lt;&lt;EOF | tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

   apt-get update
   apt-get install -y kubectl
}
</code></pre>

<p><code>kubectl get po,svc</code></p>

<pre><code>NAME         READY   STATUS    RESTARTS   AGE
pod/ubuntu   1/1     Running   0          16m

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    &lt;none&gt;        443/TCP   122d
</code></pre>
