Bird
Raised Fist0
Jenkinsdevops~10 mins

Script approval and sandbox in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Script approval and sandbox
User writes Groovy script
↓
Script runs in Jenkins sandbox?
No→Script requires approval
|Yes↓
Script executes safely
↓
If unapproved, admin reviews script
↓
Admin approves or rejects script
This flow shows how Jenkins checks if a Groovy script runs in a safe sandbox or needs admin approval before execution.
Execution Sample
Jenkins
node {
  def result = sh(script: 'echo Hello', returnStdout: true)
  echo(result)
}
A simple Jenkins pipeline script that runs a shell command and prints the output.
Process Table
StepActionSandbox CheckApproval NeededResult
1Start script executionYesNoProceed to run in sandbox
2Run shell command 'echo Hello'YesNoCommand runs, output 'Hello' captured
3Print output with echoYesNoOutput 'Hello' printed in console
4Script completesYesNoPipeline finishes successfully
💡 Script runs fully in sandbox, no approval needed because commands are safe
Status Tracker
VariableStartAfter Step 2After Step 3Final
resultundefined'Hello\n''Hello\n''Hello\n'
Key Moments - 2 Insights
Why does Jenkins ask for script approval sometimes?
If the script uses commands or methods outside the sandbox, Jenkins blocks it until an admin approves it, as shown by 'Approval Needed' in the execution table.
What is the sandbox in Jenkins script execution?
The sandbox is a safe environment that restricts scripts to only allowed commands, preventing unsafe operations. In the table, 'Sandbox Check' shows if the script runs inside it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the shell command executed?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Check the 'Action' column in the execution table for when 'Run shell command' happens.
According to the variable tracker, what is the value of 'result' after step 3?
Aundefined
B'Hello\n'
Cempty string
Dnull
💡 Hint
Look at the 'After Step 3' column for 'result' in the variable tracker.
If the script used an unapproved method, what would change in the execution table?
A'Approval Needed' would be 'No'
B'Sandbox Check' would be 'Yes'
C'Approval Needed' would be 'Yes' and script would stop before running
DScript would run normally without interruption
💡 Hint
Refer to the 'Approval Needed' column and the exit note in the execution table.
Concept Snapshot
Jenkins runs Groovy scripts in a sandbox to keep them safe.
If a script uses unsafe commands, Jenkins blocks it and asks an admin for approval.
Approved scripts run normally next time.
Sandbox restricts scripts to safe operations only.
Admins manage script approvals in Jenkins settings.
Full Transcript
In Jenkins, when you write a Groovy script, it first checks if the script can run safely inside a sandbox. The sandbox limits what the script can do to keep the system safe. If the script uses commands or methods that are not allowed in the sandbox, Jenkins will stop the script and ask an administrator to approve it. Once approved, the script can run fully. In the example, a simple shell command runs inside the sandbox without needing approval. The variable 'result' captures the output of the shell command, which is then printed. This process helps keep Jenkins secure while allowing flexible scripting.

Practice

(1/5)
1. What is the main purpose of script approval in Jenkins?
easy
A. To automatically run all scripts without restrictions
B. To protect Jenkins by allowing admins to approve unsafe scripts before execution
C. To speed up the build process by skipping script checks
D. To disable all scripts in Jenkins

Solution

  1. Step 1: Understand script approval role

    Script approval is designed to protect Jenkins by controlling which scripts can run.
  2. Step 2: Identify admin involvement

    Admins review and approve scripts to prevent unsafe code from running.
  3. Final Answer:

    To protect Jenkins by allowing admins to approve unsafe scripts before execution -> Option B
  4. Quick Check:

    Script approval = Admin review for safety [OK]
Hint: Script approval means admin checks scripts before running [OK]
Common Mistakes:
  • Thinking scripts run automatically without checks
  • Confusing script approval with disabling scripts
  • Assuming script approval speeds up builds
2. Which of the following is the correct way to enable sandbox mode in a Jenkins Pipeline script?
easy
A. Check the 'Use Groovy Sandbox' option in the Pipeline script configuration
B. Use @Sandbox annotation at the top of the script
C. Add sandbox=true inside the Jenkinsfile
D. Enable sandbox mode in Jenkins global settings only

Solution

  1. Step 1: Identify sandbox enabling method

    Sandbox mode is enabled by checking the 'Use Groovy Sandbox' box in the Pipeline script configuration.
  2. Step 2: Verify other options

    Annotations or code inside Jenkinsfile do not enable sandbox; global settings do not control this per script.
  3. Final Answer:

    Check the 'Use Groovy Sandbox' option in the Pipeline script configuration -> Option A
  4. Quick Check:

    Sandbox enabled via checkbox in script config [OK]
Hint: Sandbox mode is a checkbox in Pipeline script config [OK]
Common Mistakes:
  • Trying to enable sandbox with code annotations
  • Assuming sandbox is a global-only setting
  • Adding sandbox flags inside Jenkinsfile
3. Given this Jenkins Pipeline snippet running with sandbox enabled:
node {
  def result = sh(script: 'echo Hello', returnStdout: true).trim()
  echo result
}

What will be the output?
medium
A. No output because sandbox blocks shell commands
B. Syntax error due to sandbox
C. result
D. Hello

Solution

  1. Step 1: Analyze the sh step

    sh(script: 'echo Hello', returnStdout: true).trim() executes 'echo Hello', captures stdout, trims to 'Hello', assigns to result.
  2. Step 2: Analyze echo step

    echo result prints the value of result ('Hello'). In Jenkins Pipeline, the echo step prints variable values directly. Sandbox allows whitelisted steps like sh and echo.
  3. Final Answer:

    Hello -> Option D
  4. Quick Check:

    sh captures stdout, echo prints var value [OK]
Hint: In Pipeline, echo result prints result's value [OK]
Common Mistakes:
  • Thinking echo prints literal 'result' like shell
  • Believing sandbox blocks whitelisted sh commands
  • Expecting syntax error from sandbox
4. You wrote a Jenkins Pipeline script that uses a method not whitelisted in the sandbox. The build fails with a script approval error. What should you do to fix this?
medium
A. Restart Jenkins to clear the error
B. Remove sandbox mode and run the script without restrictions
C. Ask the Jenkins admin to approve the method in the script approval page
D. Rewrite the script to avoid using any methods

Solution

  1. Step 1: Understand script approval error cause

    The error occurs because the method is not approved in Jenkins script approval.
  2. Step 2: Fix by admin approval

    The correct fix is to have the Jenkins admin approve the method via the script approval UI.
  3. Final Answer:

    Ask the Jenkins admin to approve the method in the script approval page -> Option C
  4. Quick Check:

    Approval needed for unapproved methods [OK]
Hint: Unapproved methods need admin approval, not disabling sandbox [OK]
Common Mistakes:
  • Disabling sandbox instead of approving scripts
  • Trying to remove methods instead of approval
  • Restarting Jenkins does not fix approval errors
5. You want to run a custom Groovy script in Jenkins Pipeline that uses a non-whitelisted method and also requires sandbox protection. Which approach correctly balances safety and functionality?
hard
A. Run the script with sandbox enabled and request admin approval for the method
B. Disable sandbox and run the script directly to avoid approval delays
C. Rewrite the script to avoid all non-whitelisted methods, ignoring sandbox
D. Run the script outside Jenkins and import results to avoid sandbox issues

Solution

  1. Step 1: Understand sandbox and approval roles

    Sandbox protects Jenkins by restricting unsafe code; approval allows safe exceptions.
  2. Step 2: Balance safety and functionality

    Enabling sandbox with admin approval keeps Jenkins safe while allowing needed methods.
  3. Final Answer:

    Run the script with sandbox enabled and request admin approval for the method -> Option A
  4. Quick Check:

    Sandbox + approval = safe and functional scripts [OK]
Hint: Use sandbox plus admin approval for safe custom scripts [OK]
Common Mistakes:
  • Disabling sandbox risking Jenkins security
  • Avoiding needed methods by rewriting scripts unnecessarily
  • Running scripts outside Jenkins complicates workflow
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard