From ce68f3b1674cd2bddc8ae099d749a3ab04ad0a12 Mon Sep 17 00:00:00 2001 From: Ferenc Hammerl <31069338+fhammerl@users.noreply.github.com> Date: Mon, 9 Aug 2021 10:22:19 +0200 Subject: [PATCH] Allow the use of flags in scripts/create-latest-svc.sh in a backwards compatible way (#1220) * Use flags in svc creation script * Refactor regex and add comments * Fix indentation and typo in user matching * Consistency use flags in automation scripts * Update documentation to reflect new usage * Make example more readable * Remove test echos from script * Remove test echo * Format scripts and remove test script * Remove tar * Use getopts and single letter flags * Update docs to show flag usage * Update usage of create svc * Revert svc to not use flags * Revert delete script * Update docs * Readd deleted comments --- docs/automate.md | 17 ++++++++ scripts/create-latest-svc.sh | 82 +++++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 25 deletions(-) diff --git a/docs/automate.md b/docs/automate.md index c0255e986..adac2151a 100644 --- a/docs/automate.md +++ b/docs/automate.md @@ -26,6 +26,23 @@ Run as a one-liner. NOTE: replace with yourorg/yourrepo (repo level) or just you curl -s https://raw.githubusercontent.com/actions/runner/main/scripts/create-latest-svc.sh | bash -s yourorg/yourrepo ``` +You can call the script with additional arguments: +```bash +# Usage: +# export RUNNER_CFG_PAT= +# ./create-latest-svc -s scope -g [ghe_domain] -n [name] -u [user] -l [labels] +# -s required scope: repo (:owner/:repo) or org (:organization) +# -g optional ghe_hostname: the fully qualified domain name of your GitHub Enterprise Server deployment +# -n optional name of the runner, defaults to hostname +# -u optional user svc will run as, defaults to current +# -l optional list of labels (split by comma) applied on the runner" +``` + +Use `--` to pass any number of optional named parameters: + +``` +curl -s https://raw.githubusercontent.com/actions/runner/main/scripts/create-latest-svc.sh | bash -s -- -s myorg/myrepo -n myname -l label1,label2 +``` ### Why can't I use a container? The runner is installed as a service using `systemd` and `systemctl`. Docker does not support `systemd` for service configuration on a container. diff --git a/scripts/create-latest-svc.sh b/scripts/create-latest-svc.sh index dc008b83c..7f1cf96f4 100755 --- a/scripts/create-latest-svc.sh +++ b/scripts/create-latest-svc.sh @@ -2,36 +2,68 @@ set -e -# -# Downloads latest releases (not pre-release) runner -# Configures as a service -# -# Examples: -# RUNNER_CFG_PAT= ./create-latest-svc.sh myuser/myrepo my.ghe.deployment.net -# RUNNER_CFG_PAT= ./create-latest-svc.sh myorg my.ghe.deployment.net -# -# Usage: -# export RUNNER_CFG_PAT= -# ./create-latest-svc scope [ghe_domain] [name] [user] [labels] -# -# scope required repo (:owner/:repo) or org (:organization) -# ghe_domain optional the fully qualified domain name of your GitHub Enterprise Server deployment -# name optional defaults to hostname -# user optional user svc will run as. defaults to current -# labels optional list of labels (split by comma) applied on the runner -# # Notes: # PATS over envvars are more secure +# Downloads latest runner release (not pre-release) +# Configures it as a service more secure # Should be used on VMs and not containers # Works on OSX and Linux # Assumes x64 arch -# +# See EXAMPLES below -runner_scope=${1} -ghe_hostname=${2} -runner_name=${3:-$(hostname)} -svc_user=${4:-$USER} -labels=${5} +flags_found=false + +while getopts 's:g:n:u:l:' opt; do + flags_found=true + + case $opt in + s) + runner_scope=$OPTARG + ;; + g) + ghe_hostname=$OPTARG + ;; + n) + runner_name=$OPTARG + ;; + u) + svc_user=$OPTARG + ;; + l) + labels=$OPTARG + ;; + *) + echo " +Runner Service Installer +Examples: +RUNNER_CFG_PAT= ./create-latest-svc.sh myuser/myrepo my.ghe.deployment.net +RUNNER_CFG_PAT= ./create-latest-svc.sh -s myorg -u user_name -l label1,label2 +Usage: + export RUNNER_CFG_PAT= + ./create-latest-svc scope [ghe_domain] [name] [user] [labels] + -s required scope: repo (:owner/:repo) or org (:organization) + -g optional ghe_hostname: the fully qualified domain name of your GitHub Enterprise Server deployment + -n optional name of the runner, defaults to hostname + -u optional user svc will run as, defaults to current + -l optional list of labels (split by comma) applied on the runner" + exit 0 + ;; + esac +done + +shift "$((OPTIND - 1))" + +if ! "$flags_found"; then + runner_scope=${1} + ghe_hostname=${2} + runner_name=${3:-$(hostname)} + svc_user=${4:-$USER} + labels=${5} +fi + +# apply defaults +runner_name=${runner_name:-$(hostname)} +svc_user=${svc_user:-$USER} echo "Configuring runner @ ${runner_scope}" sudo echo @@ -142,7 +174,7 @@ echo echo "Configuring as a service ..." prefix="" if [ "${runner_plat}" == "linux" ]; then -prefix="sudo " + prefix="sudo " fi ${prefix}./svc.sh install ${svc_user}