Docs cipi.yml Web panel API MCP Ecosystem Guides Compare GitHub Telegram channel
  1. Home
  2. Guides
  3. Deploy Laravel from Jenkins to your own server
Laravel

Deploy Laravel from Jenkins to your own server

A Jenkinsfile that runs your Laravel tests, deploys with Cipi over SSH or the API, checks the release and rolls back only when it should, plus approvals and a one-click rollback job.

By Updated 4 min read

Your company already runs Jenkins, and you want Laravel releases to follow the same path as everything else: tests in Jenkins, then a zero-downtime deploy on your own server. With Cipi on the server, the Jenkins side is one short stage: Cipi does the deploy, Jenkins gives the order and reads the result.

Key ideas, in plain words#

TermMeaning
JenkinsfileA file in your repository that describes the pipeline: stages, steps, conditions
Multibranch PipelineA Jenkins job that finds every branch with a Jenkinsfile and builds it
CredentialA secret stored in Jenkins (a key, a token, a file), used by ID in the Jenkinsfile
Health URLA page that answers 200 when the app works, like Laravel's /up

Before you start#

  • A Laravel app already deployed with Cipi (follow the 15-minute guide first).
  • Jenkins 2.x with the suggested plugins, plus SSH Agent (for the SSH way).
  • A Jenkins agent with ssh and curl, and Docker or PHP + Composer for the tests.
  • A health URL: Laravel 11+ serves /up when health: '/up' is set in bootstrap/app.php.
Step 1 of 6Choose

Decide how Jenkins reaches the server. SSH is quicker to set up; the API gives Jenkins less power.

1. Choose SSH or the API#

SSHAPI
Jenkins runsssh cipi@server "sudo cipi deploy shop"cipi-cli prod deploy shop
The secret canRun any cipi command as rootOnly deploy, roll back, unlock, read status
Server needsPort 22 reachable from Jenkinscipi api installed on a hostname
Jenkins agent needssshcipi-cli

For production, prefer the API. For a first try, SSH works in five minutes. Both are shown below.

2. Create the secret#

SSH way. On your laptop, make a key used only by Jenkins and authorize it on the server:

bash
ssh-keygen -t ed25519 -C "jenkins-deploy" -f ./jenkins_deploy -N ""
ssh cipi@your-server "sudo cipi ssh add '$(cat ./jenkins_deploy.pub)'"
ssh-keyscan -t ed25519 your-server > ./known_hosts     # the server's host key

Compare the fingerprint with the server's before trusting it: ssh-keygen -lf ./known_hosts on your laptop, ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub on the server. They must match.

API way. On the server:

bash
sudo cipi api token create --name=jenkins-shop --abilities=deploy-manage --expires=2027-12-31

Copy the token now: it's shown only once.

3. Store it in Jenkins#

Manage Jenkins → Credentials → System → Global credentials → Add credentials:

IDKindWhat to paste
cipi-ci-keySSH Username with private keyUsername cipi; private key: the content of ./jenkins_deploy
cipi-known-hostsSecret fileUpload ./known_hosts
cipi-api-tokenSecret textThe API token (API way)

Then delete the local copies: rm ./jenkins_deploy ./jenkins_deploy.pub ./known_hosts.

For the API way, install cipi-cli on the Jenkins agent once:

bash
base=https://github.com/cipi-sh/cli/releases/latest/download
curl -fsSLO "$base/cipi-cli-linux-amd64" && curl -fsSLO "$base/checksums.txt"
sha256sum --check --ignore-missing checksums.txt
sudo install -m 0755 cipi-cli-linux-amd64 /usr/local/bin/cipi-cli

4. Create the job#

New Item → Multibranch Pipeline, name it shop. Under Branch Sources, add your Git server and the repository (with a read credential if it's private). Build Configuration: by Jenkinsfile. Save.

A Multibranch job builds every branch and pull request, so tests run everywhere, while the deploy stage below runs only on main.

5. Add the Jenkinsfile#

At the root of the repository:

groovy
pipeline {
    agent any

    options {
        disableConcurrentBuilds()            // never two builds of the same branch at once
        timeout(time: 30, unit: 'MINUTES')
    }

    environment {
        APP        = 'shop'
        SERVER     = 'cipi@203.0.113.10'
        HEALTH_URL = 'https://shop.example.com/up'
    }

    stages {
        stage('Test') {
            agent {
                docker {
                    image 'composer:2'      // or any image with PHP and Composer
                    reuseNode true
                }
            }
            steps {
                sh 'composer install --no-interaction --prefer-dist'
                sh 'cp .env.example .env && php artisan key:generate'
                sh 'php artisan test'
            }
        }

        stage('Deploy') {
            when { branch 'main' }
            steps {
                withCredentials([file(credentialsId: 'cipi-known-hosts', variable: 'KNOWN_HOSTS')]) {
                    sshagent(credentials: ['cipi-ci-key']) {
                        sh '''
                          ssh -o UserKnownHostsFile="$KNOWN_HOSTS" "$SERVER" \
                            "sudo cipi deploy $APP && { curl -fsS --retry 5 --retry-delay 5 --retry-all-errors $HEALTH_URL >/dev/null || { sudo cipi deploy $APP --rollback; exit 1; }; }"
                        '''
                    }
                }
            }
        }
    }
}

The deploy line, piece by piece:

PieceRuns whenDoes
sudo cipi deploy $APPAlwaysBuilds and publishes the new release. If it fails, nothing was published: the build turns red here
curl … $HEALTH_URLThe deploy succeededCalls the app up to six times, five seconds apart
sudo cipi deploy $APP --rollback; exit 1The app doesn't answerGoes back to the previous release and turns the build red

API way: replace the Deploy stage with this one.

groovy
        stage('Deploy') {
            when { branch 'main' }
            environment {
                CIPI_API_URL   = 'https://api.example.com'
                CIPI_API_TOKEN = credentials('cipi-api-token')
            }
            steps {
                sh '''
                  export HOME="$(mktemp -d)"      # the cipi-cli profile lives only for this build
                  cipi-cli profiles add prod --endpoint "$CIPI_API_URL" --token "$CIPI_API_TOKEN"
                  cipi-cli prod deploy "$APP"
                  curl -fsS --retry 5 --retry-delay 5 --retry-all-errors "$HEALTH_URL" >/dev/null \
                    || { cipi-cli prod deploy rollback "$APP" -y; exit 1; }
                '''
            }
        }

Keep the ''' single quotes. The shell reads $APP and the secrets, not Groovy, so credentials never pass through Groovy string interpolation and Jenkins can mask them in the log.

6. Start it on every push and turn Cipi's webhook off#

Make your Git server notify Jenkins:

Git serverWhat to do
GitHubGitHub plugin in Jenkins; repository webhook to https://jenkins.example.com/github-webhook/, event push
GitLabGitLab plugin; project webhook to the URL shown in the job's triggers
OthersTheir Jenkins plugin, or Scan Multibranch Pipeline Triggers → Periodically every 1–2 minutes

Then turn Cipi's webhook off for this app, otherwise each push would also deploy directly. In GitHub: Settings → Webhooks → the hook ending in /cipi/webhook → untick Active. The Jenkins hook and the Cipi hook are two different entries: keep the first, turn off the second.

7. Run the first deploy#

Push a small change to main and watch the build. On the server:

bash
sudo cipi deploy shop --status      # what's running now
sudo cipi deploy shop --audit       # the newest entry: origin cipi-cli (SSH) or panel (API)
sudo cipi app logs shop --type=deploy

To test the safety net, deploy a commit where /up returns 500 on purpose (on staging): the build should turn red and --audit should show the rollback.

8. Add approvals and a rollback button#

An approval before production. Add this stage before Deploy:

groovy
        stage('Approve') {
            when { branch 'main' }
            steps {
                timeout(time: 1, unit: 'HOURS') {
                    input message: 'Deploy shop to production?', ok: 'Deploy'
                }
            }
        }

A rollback button. Create a separate Pipeline job, "shop – rollback", with this script:

groovy
pipeline {
    agent any
    stages {
        stage('Rollback') {
            steps {
                input message: 'Roll shop back to the previous release?', ok: 'Roll back'
                withCredentials([file(credentialsId: 'cipi-known-hosts', variable: 'KNOWN_HOSTS')]) {
                    sshagent(credentials: ['cipi-ci-key']) {
                        sh 'ssh -o UserKnownHostsFile="$KNOWN_HOSTS" cipi@203.0.113.10 "sudo cipi deploy shop --rollback"'
                    }
                }
            }
        }
    }
}

A rollback switches the code back in a second. Database migrations stay as they are, so write migrations that the previous release can live with, or turn on pre-deploy snapshots.

Troubleshooting#

ProblemCauseFix
No such DSL method 'sshagent'SSH Agent plugin missingManage Jenkins → Plugins → install SSH Agent
Host key verification failedKnown-hosts credential empty or from another serverRecreate it with ssh-keyscan and compare the fingerprint
Permission denied (publickey)Key not authorizedsudo cipi ssh add "<public key>" on the server
sudo: a terminal is requiredConnected as a user other than cipiUse cipi@server
Deploy stage always skippedClassic Pipeline job, not MultibranchUse a Multibranch Pipeline, or remove when { branch 'main' }
Two deploys per pushCipi's webhook still activeUntick Active on the /cipi/webhook hook
"Deploy locked"Two builds at once or an interrupted deployKeep disableConcurrentBuilds(); sudo cipi deploy shop --unlock if nothing runs
cipi-cli: command not foundNot installed on that Jenkins agentInstall it (step 3) or pin the stage to an agent label that has it
401 or 403 from the APIToken wrong, expired, or IP not allowedsudo cipi api token list; sudo cipi api ip-whitelist show

Questions

Does Jenkins copy the code to the server?

No. Jenkins runs the tests and then asks the server to deploy. The server pulls the code from Git with its own read-only deploy key and builds the release itself.

SSH or API from Jenkins?

The API with a token limited to deploy-manage is the safer choice: the token can only deploy, roll back, unlock and read the status. Over SSH, the cipi user can run any Cipi command.

Can I keep my existing Jenkins stages?

Yes. Keep your test, lint and build stages and add the Deploy stage at the end, guarded by when { branch 'main' }.

Why does the deploy line look so long?

It does three things in order: deploy; if the deploy worked, check the app answers; only if it doesn't, roll back. A deploy that fails while building publishes nothing, so it must not be rolled back.

Can Jenkins deploy to several Cipi servers?

Yes. Add one credential (or one cipi-cli profile) per server and deploy them one after the other in the same stage.

Do I need the Cipi agent package?

No. The agent is only needed for push-to-deploy webhooks. Jenkins works over SSH or the API.

What if a step fails?

Read the troubleshooting table above first. Then check the Jenkins docs page, open a GitHub issue, or ask in the community.

Can my team roll back without SSH access?

Yes: the rollback job in step 8 gives them a button in Jenkins that asks for confirmation.

Try it on your own server

Free and open source. One command on a fresh Ubuntu 24.04 or 26.04 VPS.

wget -O - https://cipi.sh/setup.sh | bash
Read the quickstart