SSH to a different server (not a jenkins server) to build

I want to SSH to a different machine that is not a build machine (not in Jenkins nodes) and then clone my repo there and then perform all the build steps there.

How do I achieve this?

In Pipeline use the ssh pipeline steps plugin

my project is a freestyle job.

really recommend moving from freestyle to pipeline, it’ll make things more managable and let you manage configuration by code instead of random UI stuff.

SSH Agent works fine with freestyle

then add a shell step of ssh $server "cd /path/to/place; git clone url; make build"

If you want a little more of the Jenkins capabilities in your job and if the target machine has rsync, then you might consider some additional steps:

  1. Checkout from SCM on the Jenkins agent
    (shows what changes from one build to the next)
  2. Copy workspace from Jenkins agent to target machine
    (rsync -a --delete . target-machine:/home/target-user/.)
  3. Run the task on the target machine
    (ssh target-machine make all)
  4. Copy workspace from target machine to Jenkins agent
    (rsync -a --delete target-machine:/home/target-user/..)

That provides a few more of the benefits of Jenkins jobs without requiring that the target is able to run a Jenkins agent.