Files
dropshell-templates/cloudflare-tunnel/start.sh
Your Name 8e6b00bfee
All checks were successful
Test and Publish Templates / test-and-publish (push) Successful in 35s
Update versions.json
2025-09-06 14:17:56 +12:00

46 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
# shellcheck disable=SC1091
source "${AGENT_PATH}/common.sh"
_check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "TUNNEL_TOKEN"
# Check if tunnel token is set
if [ -z "$TUNNEL_TOKEN" ] || [ "$TUNNEL_TOKEN" = "" ]; then
_die "TUNNEL_TOKEN is not set in config/service.env! Please add your Cloudflare Tunnel token."
fi
echo "Starting Cloudflare Tunnel..."
# Build the docker run command
DOCKER_RUN_CMD="docker run -d \
--restart unless-stopped \
--name ${CONTAINER_NAME} \
--network host \
${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG} \
tunnel --no-autoupdate run --token ${TUNNEL_TOKEN}"
# Add extra arguments if specified
if [ -n "$EXTRA_ARGS" ]; then
DOCKER_RUN_CMD="${DOCKER_RUN_CMD} ${EXTRA_ARGS}"
fi
# Create and start the container
if ! _create_and_start_container "$DOCKER_RUN_CMD" "$CONTAINER_NAME"; then
_die "Failed to start Cloudflare Tunnel container"
fi
# Give it a moment to connect
sleep 2
# Check if the container is still running (didn't crash immediately)
if ! _is_container_running "$CONTAINER_NAME"; then
echo "Container failed to start. Checking logs..."
docker logs "$CONTAINER_NAME" 2>&1 | tail -20
_die "Cloudflare Tunnel container exited unexpectedly. Check the TUNNEL_TOKEN and logs above."
fi
echo ""
echo "Cloudflare Tunnel started successfully!"
echo "Container: ${CONTAINER_NAME}"
echo ""
echo "The tunnel should appear as 'Active' in your Cloudflare dashboard within 30 seconds."
echo "Configure routes at: https://one.dash.cloudflare.com/ -> Zero Trust -> Access -> Tunnels"