Deploy to Servers and Kubernetes From Jenkins: SSH, Ansible, kubectl, Helm and Health Checks

CI CD Pipelines with Jenkins 40 min min read Updated: Nov 17, 2026 Intermediate
Deploy to Servers and Kubernetes From Jenkins: SSH, Ansible, kubectl, Helm and Health Checks
Intermediate Topic 1 of 3

The Deployment Stage

Deployment is where mistakes become outages. A good deploy stage is automatic, repeatable, observable and reversible: it uses the exact tested artifact, applies it, waits and checks health, and rolls back if the check fails.

text artifact/image (tested) ─► deploy to staging ─► smoke test ─► approval ─► deploy to production ─► smoke test ─► (rollback if failed)

Deploy to a Server Over SSH

groovy stage('Deploy to app servers') { steps { sshagent(credentials: ['deploy-ssh-key']) { sh ''' set -e for host in app1.example.com app2.example.com; do echo "== $host ==" scp -o StrictHostKeyChecking=accept-new target/shop-${VERSION}.jar deploy@$host:/opt/shop/releases/ ssh deploy@$host " ln -sfn /opt/shop/releases/shop-${VERSION}.jar /opt/shop/current.jar && sudo systemctl restart shop && for i in 1 2 3 4 5 6 7 8 9 10; do curl -fs http://localhost:8080/health && exit 0; sleep 3; done; exit 1" done ''' } } }
  • Deploy one server at a time so the others keep serving users (rolling)
  • A symlink to the active release makes rollback instant: point it back and restart
  • Keep the last few releases on disk
  • Check health in the same step. A deploy without a health check is a guess

Deploy With Ansible

groovy stage('Deploy with Ansible') { steps { withCredentials([sshUserPrivateKey(credentialsId: 'ansible-key', keyFileVariable: 'SSH_KEY')]) { sh ''' ansible-playbook -i inventories/${TARGET_ENV}/hosts.ini deploy.yml \ --private-key "$SSH_KEY" \ -e app_version=${VERSION} -e artifact_url=https://nexus.example.com/repository/releases/shop/${VERSION}/shop-${VERSION}.jar \ --diff ''' } } }

Ansible makes the deployment steps idempotent (safe to repeat) and keeps them in a playbook that you can test outside Jenkins. Jenkins supplies the version and the target.

Deploy to Kubernetes With kubectl

groovy stage('Deploy to Kubernetes') { agent { docker { image 'bitnami/kubectl:latest'; args '--entrypoint=""' } } steps { withCredentials([file(credentialsId: 'kubeconfig-staging', variable: 'KUBECONFIG')]) { sh ''' kubectl -n shop set image deployment/shop-api api=registry.example.com/team/shop:${TAG} kubectl -n shop annotate deployment/shop-api kubernetes.io/change-cause="build ${BUILD_NUMBER} commit ${GIT_COMMIT}" --overwrite kubectl -n shop rollout status deployment/shop-api --timeout=300s ''' } } post { failure { withCredentials([file(credentialsId: 'kubeconfig-staging', variable: 'KUBECONFIG')]) { sh 'kubectl -n shop rollout undo deployment/shop-api' } } } }
CommandPurpose
kubectl set imageChange the image of a container: triggers a rolling update
kubectl rollout status --timeoutWait until the new version is ready, or fail
kubectl rollout undoGo back to the previous ReplicaSet
kubectl apply -f k8s/Apply manifests from Git (declarative)
kubectl diff -f k8s/Preview changes before applying

Deploy With Helm

groovy stage('Helm deploy') { agent { docker { image 'alpine/helm:3.16.2'; args '--entrypoint=""' } } steps { withCredentials([file(credentialsId: "kubeconfig-${params.TARGET_ENV}", variable: 'KUBECONFIG')]) { sh ''' helm lint chart/ helm upgrade --install shop chart/ \ --namespace shop --create-namespace \ --values chart/values-${TARGET_ENV}.yaml \ --set image.repository=registry.example.com/team/shop \ --set image.tag=${TAG} \ --atomic --timeout 5m --wait helm history shop -n shop | tail -3 ''' } } }
Helm flagEffect
upgrade --installInstall if missing, upgrade if present (idempotent)
--atomicOn failure roll back automatically to the previous release
--wait, --timeoutWait until resources are ready
--values, --setPer-environment values and the image tag
helm rollback shop 3Manual rollback to revision 3

Smoke Tests After Deploy

groovy stage('Smoke test') { steps { retry(5) { sleep 5 sh 'curl -fsS https://staging.shop.example.com/health | grep -q "\"status\":\"ok\""' } sh 'newman run tests/smoke.postman_collection.json --env-var base_url=https://staging.shop.example.com' } }
  • Keep smoke tests short and read-only: login page loads, health endpoint, one key API call
  • Run heavier end-to-end tests in a separate stage or pipeline
  • Fail the deploy and roll back when the smoke test fails

Handle Secrets and Config

  • Do not bake environment settings into the image: use ConfigMaps, Secrets and Helm values per environment
  • Use a separate credential per environment (staging cannot touch production)
  • Prefer short-lived access (cloud OIDC or service account tokens) over a kubeconfig file with admin rights; give the deploy account only the namespace it needs
yaml # Minimal RBAC for a deploy account limited to one namespace apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: {name: deployer, namespace: shop} rules: - apiGroups: ["apps"] resources: ["deployments"] verbs: ["get", "list", "watch", "patch", "update"] - apiGroups: [""] resources: ["pods", "services", "configmaps"] verbs: ["get", "list", "watch"]

Practice Tasks

  1. Deploy a small app to a Linux VM over SSH with a symlinked release and a health check
  2. Deploy the same app to a lab Kubernetes cluster using kubectl set image and rollout status
  3. Change to helm upgrade --install --atomic and force a failure to see the automatic rollback
  4. Write a smoke test stage with retries
  5. Create a limited deploy account and prove it cannot touch another namespace

Conclusion

A deploy stage must apply the exact tested artifact, wait for readiness, run a smoke test and roll back on failure. Use SSH with symlink releases, Ansible for idempotent server changes, and kubectl or Helm with --atomic for Kubernetes, with separate, minimal credentials per environment.

Quick Summary: deploy SSH rolling+symlink; Ansible; kubectl set image/rollout status/undo; Helm upgrade --install --atomic; smoke tests; per-env creds + minimal RBAC.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators