Skip to content

Commit aad8386

Browse files
committed
refactor: improve code quality and reproducibility
- Pin yq version to v4.44.3 with curl fallback - Pin devcontainer features (Docker 27.0, gh CLI 2.62) - Add shell safety flags (set -Eeuo pipefail) - Replace hardcoded paths with git rev-parse - Improve variable quoting throughout scripts - Add validation checks (yq exists, gh authenticated) - Enhance error handling in all scripts Addresses all valid suggestions from AI reviewers (CodeRabbitAI and Gemini).
1 parent b9ffb0d commit aad8386

File tree

3 files changed

+70
-41
lines changed

3 files changed

+70
-41
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
"features": {
66
"ghcr.io/devcontainers/features/docker-in-docker:2": {
7-
"version": "latest",
7+
"version": "27.0",
88
"dockerDashComposeVersion": "v2"
99
},
1010
"ghcr.io/devcontainers/features/github-cli:1": {
11-
"version": "latest"
11+
"version": "2.62"
1212
}
1313
},
1414

.devcontainer/setup.sh

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
# GitHub Codespaces setup script for Jenkins Quickstart Tutorials
33
# This script configures the Codespace environment and prepares Jenkins URL configuration
44

5-
set -e # Exit on error
5+
set -Eeuo pipefail # Exit on error, undefined variables, pipe failures
66

77
echo "================================"
88
echo "Setting up Jenkins Tutorials Environment"
99
echo "================================"
1010

1111
# Install yq (YAML processor) - required for JCasc configuration
1212
echo "📦 Installing yq YAML processor..."
13-
sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
13+
YQ_VERSION="${YQ_VERSION:-v4.44.3}"
14+
YQ_URL="https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64"
15+
16+
# Try wget first, fall back to curl if unavailable
17+
if command -v wget &> /dev/null; then
18+
sudo wget -qO /usr/local/bin/yq "${YQ_URL}"
19+
elif command -v curl &> /dev/null; then
20+
sudo curl -fsSL -o /usr/local/bin/yq "${YQ_URL}"
21+
else
22+
echo "❌ Error: Neither wget nor curl found. Cannot download yq."
23+
exit 1
24+
fi
25+
1426
sudo chmod a+x /usr/local/bin/yq
1527
yq --version
1628

@@ -65,36 +77,45 @@ To build locally:
6577
WELCOME_EOF
6678

6779
# Add Jenkins URL based on environment
68-
if [ -n "$CODESPACE_NAME" ] && [ -n "$GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN" ]; then
69-
echo "Jenkins will be accessible at:" >> "$WELCOME_FILE"
70-
echo " https://${CODESPACE_NAME}-8080.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}" >> "$WELCOME_FILE"
80+
if [ -n "${CODESPACE_NAME:-}" ] && [ -n "${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN:-}" ]; then
81+
echo "Jenkins will be accessible at:" >> "${WELCOME_FILE}"
82+
echo " https://${CODESPACE_NAME}-8080.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}" >> "${WELCOME_FILE}"
7183
else
72-
echo "Jenkins will be accessible at:" >> "$WELCOME_FILE"
73-
echo " http://localhost:8080" >> "$WELCOME_FILE"
84+
echo "Jenkins will be accessible at:" >> "${WELCOME_FILE}"
85+
echo " http://localhost:8080" >> "${WELCOME_FILE}"
7486
fi
7587

76-
echo "" >> "$WELCOME_FILE"
77-
echo "Default credentials: admin/admin" >> "$WELCOME_FILE"
78-
echo "================================" >> "$WELCOME_FILE"
79-
echo "" >> "$WELCOME_FILE"
88+
echo "" >> "${WELCOME_FILE}"
89+
echo "Default credentials: admin/admin" >> "${WELCOME_FILE}"
90+
echo "================================" >> "${WELCOME_FILE}"
91+
echo "" >> "${WELCOME_FILE}"
8092

8193
# Display the welcome message
82-
cat "$WELCOME_FILE"
94+
cat "${WELCOME_FILE}"
8395

84-
echo "✅ Setup Complete! Welcome message saved to $WELCOME_FILE"
96+
echo "✅ Setup Complete! Welcome message saved to ${WELCOME_FILE}"
8597
echo ""
8698

8799
# Add welcome message to .bashrc so it shows on every new terminal
100+
# Use git rev-parse to find repo root dynamically instead of hardcoding path
101+
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
102+
WELCOME_PATH="${REPO_ROOT}/.devcontainer/welcome.txt"
103+
88104
if ! grep -q "Jenkins Quickstart Tutorials Welcome" ~/.bashrc; then
89105
echo "" >> ~/.bashrc
90106
echo "# Jenkins Quickstart Tutorials Welcome" >> ~/.bashrc
91-
echo "if [ -f /workspaces/quickstart-tutorials/.devcontainer/welcome.txt ]; then" >> ~/.bashrc
92-
echo " cat /workspaces/quickstart-tutorials/.devcontainer/welcome.txt" >> ~/.bashrc
107+
echo "if [ -f \"${WELCOME_PATH}\" ]; then" >> ~/.bashrc
108+
echo " cat \"${WELCOME_PATH}\"" >> ~/.bashrc
93109
echo "fi" >> ~/.bashrc
94110
fi
95111

96112
# Set port 8080 visibility to public using gh CLI (if in Codespaces)
97-
if [ -n "$CODESPACE_NAME" ]; then
113+
if [ -n "${CODESPACE_NAME:-}" ]; then
98114
echo "🔓 Setting port 8080 visibility to public..."
99-
gh codespace ports visibility 8080:public -c "$CODESPACE_NAME" 2>/dev/null || echo "⚠️ Could not set port visibility automatically. Please set port 8080 to public manually in the PORTS panel."
115+
# Check if gh CLI is authenticated before attempting to set port visibility
116+
if gh auth status &>/dev/null; then
117+
gh codespace ports visibility 8080:public -c "${CODESPACE_NAME}" 2>/dev/null || echo "⚠️ Could not set port visibility automatically. Please set port 8080 to public manually in the PORTS panel."
118+
else
119+
echo "⚠️ gh CLI not authenticated. Please set port 8080 to public manually in the PORTS panel."
120+
fi
100121
fi

dockerfiles/codespacesURL.sh

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
# GitHub Codespaces URL configuration script for Jenkins
33
# This script configures Jenkins URLs to work with Codespaces port forwarding
44

5-
# Set the path to the configuration file
6-
config_file="/workspaces/quickstart-tutorials/dockerfiles/jenkins.yaml"
5+
set -Eeuo pipefail # Exit on error, undefined variables, pipe failures
6+
7+
# Resolve repo root and config file dynamically
8+
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
9+
config_file="${REPO_ROOT}/dockerfiles/jenkins.yaml"
10+
11+
# Port configuration (default 8080)
12+
PORT="${PORT:-8080}"
713

814
# Check if running in GitHub Codespaces
9-
if [ -n "$CODESPACE_NAME" ] && [ -n "$GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN" ]; then
15+
if [ -n "${CODESPACE_NAME:-}" ] && [ -n "${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN:-}" ]; then
1016
# Build Codespaces URL from environment variables
11-
service_url="https://${CODESPACE_NAME}-8080.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"
17+
service_url="https://${CODESPACE_NAME}-${PORT}.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"
1218
echo "✅ Detected GitHub Codespaces environment"
1319
else
1420
# Fallback for local development
15-
service_url="http://127.0.0.1:8080"
21+
service_url="http://127.0.0.1:${PORT}"
1622
echo "ℹ️ Running in local environment"
1723
fi
1824

@@ -24,7 +30,7 @@ message="📚 Available tutorial profiles: "
2430

2531
# Build the profiles list message
2632
for target in "${targets[@]}"; do
27-
message+="\033[36m$target\033[0m, "
33+
message+="\033[36m${target}\033[0m, "
2834
done
2935

3036
# Remove the trailing comma and space
@@ -36,37 +42,39 @@ echo "🚀 Jenkins Quickstart Tutorials Setup"
3642
echo "======================================"
3743
echo ""
3844
echo "Once you run \033[42;30mdocker compose --profile <profile-name> up\033[0m,"
39-
echo "Jenkins will be accessible at: \033[36m$service_url\033[0m"
45+
echo "Jenkins will be accessible at: \033[36m${service_url}\033[0m"
4046
echo ""
41-
echo -e "$message"
47+
echo -e "${message}"
4248
echo ""
4349
echo "Quick start commands:"
4450
for target in "${targets[@]}"; do
45-
echo " • \033[42;30mdocker compose --profile $target up -d\033[0m - Start $target tutorial"
51+
echo " • \033[42;30mdocker compose --profile ${target} up -d\033[0m - Start ${target} tutorial"
4652
done
4753
echo ""
4854

4955
# Check if jenkins.yaml exists
50-
if [ ! -f "$config_file" ]; then
51-
echo "⚠️ Warning: Jenkins configuration file not found at $config_file"
56+
if [ ! -f "${config_file}" ]; then
57+
echo "⚠️ Warning: Jenkins configuration file not found at ${config_file}"
5258
echo " Configuration will be done when Jenkins starts."
5359
exit 0
5460
fi
5561

5662
echo "🔧 Updating Jenkins configuration..."
5763

64+
# Verify yq is available
65+
if ! command -v yq &> /dev/null; then
66+
echo "❌ Error: yq not found. Please install yq to update Jenkins configuration."
67+
echo " Jenkins URL may need manual configuration."
68+
exit 1
69+
fi
70+
5871
# Use yq to update the Jenkins location URL in the configuration file
59-
if command -v yq &> /dev/null; then
60-
yq eval ".unclassified.location.url = \"$service_url/\"" "$config_file" > "$config_file.tmp" && mv "$config_file.tmp" "$config_file"
72+
yq -i ".unclassified.location.url = \"${service_url}/\"" "${config_file}"
6173

62-
# Suppress the Reverse Proxy setup warning for Codespaces
63-
yq e -i ".jenkins.disabledAdministrativeMonitors = [\"hudson.diagnosis.ReverseProxySetupMonitor\"]" "$config_file"
74+
# Suppress the Reverse Proxy setup warning for Codespaces
75+
yq -i '.jenkins.disabledAdministrativeMonitors = ["hudson.diagnosis.ReverseProxySetupMonitor"]' "${config_file}"
6476

65-
echo "✅ Jenkins configuration updated successfully"
66-
else
67-
echo "⚠️ Warning: yq not found, skipping configuration update"
68-
echo " Jenkins URL may need manual configuration"
69-
fi
77+
echo "✅ Jenkins configuration updated successfully"
7078

7179
echo ""
7280
echo "🎉 Setup complete! You're ready to start a tutorial."

0 commit comments

Comments
 (0)