#!/bin/sh
set -eo pipefail
output_file="test_envs"
jenkins_image="jenkins/jenkins:2.504.1-lts-alpine"
jenkins_container_name="mcp-jenkins-test-env"
jenkins_port="6211"
jenkins_user="testadmin"
jenkins_password="test"
jenkins_url="http://localhost:${jenkins_port}"
jenkins_data_dir="test_jenkins_data"
# Plugin configuration
folder_plugin_name="cloudbees-folder"
folder_plugin_hpi_url="https://updates.jenkins.io/latest/${folder_plugin_name}.hpi" # Reverted to .hpi
plugins_dir="${jenkins_data_dir}/plugins"
folder_plugin_hpi_path="${plugins_dir}/${folder_plugin_name}.hpi" # Reverted to .hpi
ionicons_api_plugin_name="ionicons-api"
ionicons_api_plugin_hpi_url="https://updates.jenkins.io/latest/${ionicons_api_plugin_name}.hpi" # Changed to .hpi
ionicons_api_plugin_hpi_path="${plugins_dir}/${ionicons_api_plugin_name}.hpi" # Changed to .hpi
# Function to run commands and check for errors
run_command() {
echo "Executing command: $*"
if ! "$@"; then
echo "Error: Command failed: $*" >&2
exit 1
fi
}
# Check if test environment already exists
if [ -f "${output_file}" ] && [ -d "${jenkins_data_dir}" ] && [ -n "$(ls -A "${jenkins_data_dir}")" ]; then
echo "--------------------------------------------------"
echo "Jenkins test environment already exists."
echo "Credentials in '${output_file}':"
cat "${output_file}"
echo ""
echo "To reinstall the test environment, remove the '${output_file}' file and the '${jenkins_data_dir}' directory."
echo "--------------------------------------------------"
exit 0
fi
# Attempt to stop and remove the container if it exists
echo "Attempting to stop and remove existing container '${jenkins_container_name}' if it exists..."
docker rm -f "${jenkins_container_name}" > /dev/null 2>&1 || true
echo "Ensured no conflicting container is running."
echo "Starting Jenkins test environment deployment..."
echo "Pulling Jenkins image (if not already present)..."
run_command docker pull "${jenkins_image}"
echo "Starting Jenkins container '${jenkins_container_name}' on port ${jenkins_port} using host network..."
# Create the secrets directory if it doesn't exist
mkdir -p "${jenkins_data_dir}/secrets"
echo "Creating plugins directory ${plugins_dir}..."
run_command mkdir -p "${plugins_dir}"
echo "Downloading Folder plugin (${folder_plugin_name}.hpi) to ${folder_plugin_hpi_path}..."
run_command curl -sSLf -o "${folder_plugin_hpi_path}" "${folder_plugin_hpi_url}"
echo "Downloading ionicons-api plugin (${ionicons_api_plugin_name}.hpi) to ${ionicons_api_plugin_hpi_path}..."
run_command curl -sSLf -o "${ionicons_api_plugin_hpi_path}" "${ionicons_api_plugin_hpi_url}"
run_command docker run -d --network=host --name "${jenkins_container_name}" \
-v "$(pwd)/${jenkins_data_dir}:/var/jenkins_home" \
-e "JAVA_OPTS=-Djenkins.install.runSetupWizard=false -Djenkins.security.SecurityRealm.noSecurityRealm=true -Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true" \
"${jenkins_image}" --httpPort=${jenkins_port}
echo "Waiting for Jenkins to start at ${jenkins_url} (this may take a few minutes)..."
max_retries=90
retry_count=0
while [ ${retry_count} -lt ${max_retries} ]; do
if curl -sL --fail -w "%{http_code}" "${jenkins_url}/login" -o /dev/null 2>/dev/null | grep -q "200"; then
echo "Jenkins is up and running!"
break
fi
retry_count=$((retry_count + 1))
if [ ${retry_count} -ge ${max_retries} ]; then
echo "Error: Jenkins did not start within the expected time ($((max_retries * 5)) seconds)." >&2
echo "Attempting to get logs from container '${jenkins_container_name}':" >&2
docker logs "${jenkins_container_name}" >&2 || true
exit 1
fi
echo "Jenkins not ready yet (attempt ${retry_count}/${max_retries}). Retrying in 5 seconds..."
sleep 5
done
echo "Allowing Jenkins an additional 20 seconds to initialize fully..."
sleep 20
echo "Creating initial '${output_file}'..."
mkdir -p "$(dirname "${output_file}")" || true
cat > "${output_file}" <<EOF
# Jenkins Test Environment Details
# Generated by script
export JENKINS_URL="${jenkins_url}"
# JENKINS_USER and JENKINS_PASSWORD are not set as authentication is disabled.
# JENKINS_API_TOKEN is not applicable as authentication is disabled.
EOF
echo "--------------------------------------------------"
echo "Jenkins deployment complete!"
echo "Jenkins is running at: ${jenkins_url}"
echo "Setup wizard and authentication have been disabled."
echo "Details saved in: $(realpath "${output_file}")"
echo ""
echo "To stop the Jenkins container: docker stop ${jenkins_container_name}"
echo "To remove the Jenkins container (after stopping): docker rm ${jenkins_container_name}"
echo "To view Jenkins logs: docker logs ${jenkins_container_name}"
echo "--------------------------------------------------"