|
1 | | -#!/bin/bash |
| 1 | +#!/usr/bin/env bash |
2 | 2 | # GitHub Codespaces setup script for Jenkins Quickstart Tutorials |
3 | 3 | # This script configures the Codespace environment and prepares Jenkins URL configuration |
4 | 4 |
|
5 | | -set -e # Exit on error |
| 5 | +set -Eeuo pipefail # Exit on error, undefined variables, pipe failures |
6 | 6 |
|
7 | 7 | echo "================================" |
8 | 8 | echo "Setting up Jenkins Tutorials Environment" |
9 | 9 | echo "================================" |
10 | 10 |
|
11 | 11 | # Install yq (YAML processor) - required for JCasc configuration |
12 | 12 | 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 | + |
14 | 26 | sudo chmod a+x /usr/local/bin/yq |
15 | 27 | yq --version |
16 | 28 |
|
@@ -65,36 +77,45 @@ To build locally: |
65 | 77 | WELCOME_EOF |
66 | 78 |
|
67 | 79 | # 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}" |
71 | 83 | 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}" |
74 | 86 | fi |
75 | 87 |
|
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}" |
80 | 92 |
|
81 | 93 | # Display the welcome message |
82 | | -cat "$WELCOME_FILE" |
| 94 | +cat "${WELCOME_FILE}" |
83 | 95 |
|
84 | | -echo "✅ Setup Complete! Welcome message saved to $WELCOME_FILE" |
| 96 | +echo "✅ Setup Complete! Welcome message saved to ${WELCOME_FILE}" |
85 | 97 | echo "" |
86 | 98 |
|
87 | 99 | # 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 | + |
88 | 104 | if ! grep -q "Jenkins Quickstart Tutorials Welcome" ~/.bashrc; then |
89 | 105 | echo "" >> ~/.bashrc |
90 | 106 | 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 |
93 | 109 | echo "fi" >> ~/.bashrc |
94 | 110 | fi |
95 | 111 |
|
96 | 112 | # Set port 8080 visibility to public using gh CLI (if in Codespaces) |
97 | | -if [ -n "$CODESPACE_NAME" ]; then |
| 113 | +if [ -n "${CODESPACE_NAME:-}" ]; then |
98 | 114 | 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 |
100 | 121 | fi |
0 commit comments