Skip to content

Commit

Permalink
feat: add support for adding labels to self hosted runner
Browse files Browse the repository at this point in the history
this adds a new flag `--ghactions-runner-labels` that takes a string
slice containing the desired labels to be added to the action runner

e.g `--ghactions-runner-labels="azure,mapt,win,self-hosted"` these
labels would be added in addition to the default labels already added
by the runner agent
  • Loading branch information
anjannath committed Nov 6, 2024
1 parent abc8bb7 commit 1b9bc0d
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 12 deletions.
3 changes: 2 additions & 1 deletion cmd/mapt/cmd/aws/hosts/fedora.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func getFedoraCreate() *cobra.Command {
if viper.IsSet(params.InstallGHActionsRunner) {
err := ghactions.InitGHRunnerArgs(viper.GetString(params.GHActionsRunnerToken),
viper.GetString(params.GHActionsRunnerName),
viper.GetString(params.GHActionsRunnerRepo))
viper.GetString(params.GHActionsRunnerRepo),
viper.GetStringSlice(params.GHActionsRunnerLabels))
if err != nil {
logging.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/mapt/cmd/aws/hosts/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func getMacRequest() *cobra.Command {
if viper.IsSet(params.InstallGHActionsRunner) {
err := ghactions.InitGHRunnerArgs(viper.GetString(params.GHActionsRunnerToken),
viper.GetString(params.GHActionsRunnerName),
viper.GetString(params.GHActionsRunnerRepo))
viper.GetString(params.GHActionsRunnerRepo),
viper.GetStringSlice(params.GHActionsRunnerLabels))
if err != nil {
logging.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/mapt/cmd/aws/hosts/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func getRHELCreate() *cobra.Command {
if viper.IsSet(params.InstallGHActionsRunner) {
err := ghactions.InitGHRunnerArgs(viper.GetString(params.GHActionsRunnerToken),
viper.GetString(params.GHActionsRunnerName),
viper.GetString(params.GHActionsRunnerRepo))
viper.GetString(params.GHActionsRunnerRepo),
viper.GetStringSlice(params.GHActionsRunnerLabels))
if err != nil {
logging.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/mapt/cmd/aws/hosts/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func getWindowsCreate() *cobra.Command {
if viper.IsSet(params.InstallGHActionsRunner) {
err := ghactions.InitGHRunnerArgs(viper.GetString(params.GHActionsRunnerToken),
viper.GetString(params.GHActionsRunnerName),
viper.GetString(params.GHActionsRunnerRepo))
viper.GetString(params.GHActionsRunnerRepo),
viper.GetStringSlice(params.GHActionsRunnerLabels))
if err != nil {
logging.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/mapt/cmd/azure/hosts/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func getCreateWindowsDesktop() *cobra.Command {
if viper.IsSet(params.InstallGHActionsRunner) {
err := ghactions.InitGHRunnerArgs(viper.GetString(params.GHActionsRunnerToken),
viper.GetString(params.GHActionsRunnerName),
viper.GetString(params.GHActionsRunnerRepo))
viper.GetString(params.GHActionsRunnerRepo),
viper.GetStringSlice(params.GHActionsRunnerLabels))
if err != nil {
logging.Fatal(err)
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/mapt/cmd/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
GHActionsRunnerTokenDesc string = "Token needed for registering the Github Actions Runner token"
GHActionsRunnerNameDesc string = "Name for the Github Actions Runner"
GHActionsRunnerRepoDesc string = "Full URL of the repository where the Github Actions Runner should be registered"
GHActionsRunnerLabelsDesc string = "List of labels separated by comma to be added to the self-hosted runner"
Memory string = "memory"
MemoryDesc string = "Amount of RAM for the cloud instance in GiB"
CPUs string = "cpus"
Expand All @@ -54,6 +55,7 @@ const (
GHActionsRunnerToken string = "ghactions-runner-token"
GHActionsRunnerName string = "ghactions-runner-name"
GHActionsRunnerRepo string = "ghactions-runner-repo"
GHActionsRunnerLabels string = "ghactions-runner-labels"
)

func GetGHActionsFlagset() *pflag.FlagSet {
Expand All @@ -62,6 +64,7 @@ func GetGHActionsFlagset() *pflag.FlagSet {
flagSet.StringP(GHActionsRunnerToken, "", "", GHActionsRunnerTokenDesc)
flagSet.StringP(GHActionsRunnerName, "", "", GHActionsRunnerNameDesc)
flagSet.StringP(GHActionsRunnerRepo, "", "", GHActionsRunnerRepoDesc)
flagSet.StringSlice(GHActionsRunnerLabels, nil, GHActionsRunnerLabelsDesc)
return flagSet
}

Expand Down
1 change: 1 addition & 0 deletions docs/self-hosted-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ To deploy a Windows runner on the Azure provider, we can use the following comma
--backed-url file:///Users/tester/workspace \
--conn-details-output /Users/tester/workspace/conn-details
```
> *NOTE:* additional _labels_ can be added to the runner using the flag `--ghactions-runner-labels`, e.g `--ghactions-runner-lables="azure,mapt,windows"`
30 changes: 23 additions & 7 deletions pkg/util/ghactions/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ghactions

import (
"fmt"
"strings"

"github.com/pkg/errors"
"github.com/redhat-developer/mapt/pkg/util"
Expand All @@ -11,6 +12,7 @@ type RunnerArgs struct {
Token string
RepoURL string
Name string
Labels []string
}

const (
Expand All @@ -26,23 +28,23 @@ const (
Invoke-WebRequest -Uri %s -OutFile actions-runner-win.zip
Add-Type -AssemblyName System.IO.Compression.FileSystem ;
[System.IO.Compression.ZipFile]::ExtractToDirectory("$PWD\actions-runner-win.zip", "$PWD")
./config.cmd --token $ghToken --url %s --name %s --unattended --runasservice --replace`
./config.cmd --token $ghToken --url %s --name %s --unattended --runasservice --replace %s`

// whitespace at the start is required since this is expanded in a cloud-init yaml file
// to start as service need to relable the runsvc.sh file on rhel: https://github.com/actions/runner/issues/3222
installActionRunnerSnippetLinux string = ` mkdir ~/actions-runner && cd ~/actions-runner` + "\n" +
` curl -o actions-runner-linux.tar.gz -L %s` + "\n" +
` tar xzf ./actions-runner-linux.tar.gz` + "\n" +
` sudo ./bin/installdependencies.sh` + "\n" +
` ./config.sh --token %s --url %s --name %s --unattended --replace` + "\n" +
` ./config.sh --token %s --url %s --name %s --unattended --replace %s` + "\n" +
` sudo ./svc.sh install` + "\n" +
` chcon system_u:object_r:usr_t:s0 $(pwd)/runsvc.sh` + "\n" +
` sudo ./svc.sh start`

installActionRunnerSnippetMacos string = `mkdir ~/actions-runner && cd ~/actions-runner
curl -o actions-runner-osx.tar.gz -L %s
tar xzf ./actions-runner-osx.tar.gz
./config.sh --token %s --url %s --name %s --unattended --replace
./config.sh --token %s --url %s --name %s --unattended --replace %s
./svc.sh install
plistName=$(basename $(./svc.sh status | grep "plist$"))
mkdir -p ~/Library/LaunchDaemons
Expand All @@ -52,7 +54,7 @@ launchctl load ~/Library/LaunchDaemons/"${plistName}"`

var args *RunnerArgs

func InitGHRunnerArgs(token, name, repoURL string) error {
func InitGHRunnerArgs(token, name, repoURL string, labels []string) error {
if token == "" || name == "" || repoURL == "" {
return errors.New("All args are required and must have non-empty values")
}
Expand All @@ -61,6 +63,10 @@ func InitGHRunnerArgs(token, name, repoURL string) error {
RepoURL: repoURL,
Name: name,
}

if len(labels) > 0 {
args.Labels = labels
}
return nil
}

Expand All @@ -71,26 +77,36 @@ func GetToken() string {
return util.IfNillable(args != nil, token, "")
}

func GetLabels() string {
var labels = func() string {
if len(args.Labels) > 0 {
return fmt.Sprintf("--labels %s", strings.Join(args.Labels, ","))
}
return ""
}
return util.IfNillable(args != nil, labels, "")
}

func GetActionRunnerSnippetWin() string {
var snippetWindows = func() string {
return fmt.Sprintf(installActionRunnerSnippetWindows,
fmt.Sprintf(runnerBaseURLWin, runnerVersion), args.RepoURL, args.Name)
fmt.Sprintf(runnerBaseURLWin, runnerVersion), args.RepoURL, args.Name, GetLabels())
}
return util.IfNillable(args != nil, snippetWindows, "")
}

func GetActionRunnerSnippetLinux() string {
var snippetLinux = func() string {
return fmt.Sprintf(installActionRunnerSnippetLinux,
fmt.Sprintf(runnerBaseURLLinux, runnerVersion), args.Token, args.RepoURL, args.Name)
fmt.Sprintf(runnerBaseURLLinux, runnerVersion), args.Token, args.RepoURL, args.Name, GetLabels())
}
return util.IfNillable(args != nil, snippetLinux, "")
}

func GetActionRunnerSnippetMacos() string {
var snippetMacos = func() string {
return fmt.Sprintf(installActionRunnerSnippetMacos,
fmt.Sprintf(runnerBaseURLMacos, runnerVersion), args.Token, args.RepoURL, args.Name)
fmt.Sprintf(runnerBaseURLMacos, runnerVersion), args.Token, args.RepoURL, args.Name, GetLabels())
}
return util.IfNillable(args != nil, snippetMacos, "")
}

0 comments on commit 1b9bc0d

Please sign in to comment.