- Run Freestyle jobs on an external agent using labels.
- Pin specific pipeline stages to a particular agent.
- Inspect workspace locations to verify where each stage or job executed.

1) Freestyle Job on an External Agent
Create a new Freestyle job — for example, name itfreestyle-external-agent.

- Add a build step “Execute shell” and include commands to print OS and Node/NPM versions:
- Under “Restrict where this project can be run”, paste the external agent’s label (for example
ubuntu-docker-jdk17-node20) so the job executes on that agent.

Running Freestyle jobs on labeled agents is an easy way to direct platform-specific or dependency-heavy builds to machines prepared with the required environment.
2) Pipeline Job with Stage-Level Agent Selection
Next, demonstrate the same checks using a Declarative Pipeline and show how to override the pipeline-level agent for a specific stage. Import or create a repository in your Git hosting (Gitea in this example) and include a Jenkinsfile in the repo. In the UI, create a Pipeline job (for examplepipeline-external-agent) and point its Pipeline script to the Jenkinsfile stored in the repository.


Defining an
agent inside a stage overrides the pipeline-level agent for that stage — useful for running platform-specific builds or tests on dedicated machines.

- Checkout and stage S1 run on the pipeline-level (global) agent — often the controller when
agent anyis satisfied there. Workspace example:/var/lib/jenkins/workspace/pipeline-external-agent.
- Stage S2 uses the stage-level agent and runs on the specified Ubuntu agent. Workspace example on the agent:
/home/jenkins-agent/workspace/pipeline-external-agent.
Workspace and Agent Comparison
| Agent scope | Where it runs | Example workspace path | Use case |
|---|---|---|---|
Pipeline-level (agent any) | Controller or any available agent | /var/lib/jenkins/workspace/pipeline-external-agent | General pipeline steps and SCM checkout |
Stage-level (stage { agent { label '...' } }) | Specified external agent | /home/jenkins-agent/workspace/pipeline-external-agent | Platform-specific build/test or dependency-heavy tasks |
| Freestyle job with label | Specified agent | /home/jenkins-agent/workspace/freestyle-external-agent | Jobs targeted to agents with required tools |
Avoid running long-running or resource-intensive builds on the controller (master). Use labels to route those builds to dedicated agents to keep the controller responsive.
Summary
- Use “Restrict where this project can be run” to pin Freestyle jobs to an agent by label.
- Use a stage-level
agentin Declarative Pipeline to run a single stage on a specific node without affecting other stages. - Verify where stages executed by inspecting console logs and the workspace paths shown for each node.
- Jenkins Documentation — Pipeline Syntax
- Jenkins — Distributed builds and agents
- Gitea — Self-hosted Git service