diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 742d50b..db3581e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,6 +31,7 @@ jobs: "dotnet", "eclipse-deps", "git-lfs", + "github-cli", "github-copilot-cli", "gitlab-cli", "go", diff --git a/build/build.go b/build/build.go index c64f163..4f5e0e7 100644 --- a/build/build.go +++ b/build/build.go @@ -114,6 +114,10 @@ func init() { gotaskr.Task("Feature:git-lfs:Package", func() error { return packageFeature("git-lfs") }) gotaskr.Task("Feature:git-lfs:Test", func() error { return testFeature("git-lfs") }) + ////////// github-cli + gotaskr.Task("Feature:github-cli:Package", func() error { return packageFeature("github-cli") }) + gotaskr.Task("Feature:github-cli:Test", func() error { return testFeature("github-cli") }) + ////////// github-copilot-cli gotaskr.Task("Feature:github-copilot-cli:Package", func() error { return packageFeature("github-copilot-cli") }) gotaskr.Task("Feature:github-copilot-cli:Test", func() error { return testFeature("github-copilot-cli") }) diff --git a/features/src/github-cli/NOTES.md b/features/src/github-cli/NOTES.md new file mode 100644 index 0000000..9898537 --- /dev/null +++ b/features/src/github-cli/NOTES.md @@ -0,0 +1,11 @@ +## Notes + +### System Compatibility + +Debian, Ubuntu, Alpine + +### Accessed Urls + +Needs access to the following URL for downloading and resolving: +* https://github.com +* https://api.github.com diff --git a/features/src/github-cli/README.md b/features/src/github-cli/README.md new file mode 100644 index 0000000..b40db9c --- /dev/null +++ b/features/src/github-cli/README.md @@ -0,0 +1,33 @@ +# GitHub CLI (github-cli) + +Installs the GitHub CLI. + +## Example Usage + +```json +"features": { + "ghcr.io/postfinance/devcontainer-features/github-cli:1.0.0": { + "version": "latest", + "downloadUrl": "" + } +} +``` + +## Options + +| Option | Description | Type | Default Value | Proposals | +|-----|-----|-----|-----|-----| +| version | The version of GitHub CLI to install. | string | latest | latest, 2.67.0 | +| downloadUrl | The download URL to use for GitHub CLI binaries. | string | <empty> | https://mycompany.com/artifactory/github-releases-remote | + +## Notes + +### System Compatibility + +Debian, Ubuntu, Alpine + +### Accessed Urls + +Needs access to the following URL for downloading and resolving: +* https://github.com +* https://api.github.com diff --git a/features/src/github-cli/devcontainer-feature.json b/features/src/github-cli/devcontainer-feature.json new file mode 100644 index 0000000..17debb0 --- /dev/null +++ b/features/src/github-cli/devcontainer-feature.json @@ -0,0 +1,25 @@ +{ + "id": "github-cli", + "version": "1.0.0", + "name": "GitHub CLI", + "description": "Installs the GitHub CLI.", + "options": { + "version": { + "type": "string", + "proposals": [ + "latest", + "2.67.0" + ], + "default": "latest", + "description": "The version of GitHub CLI to install." + }, + "downloadUrl": { + "type": "string", + "default": "", + "proposals": [ + "https://mycompany.com/artifactory/github-releases-remote" + ], + "description": "The download URL to use for GitHub CLI binaries." + } + } +} diff --git a/features/src/github-cli/install.sh b/features/src/github-cli/install.sh new file mode 100644 index 0000000..e2e3b0a --- /dev/null +++ b/features/src/github-cli/install.sh @@ -0,0 +1,5 @@ +. ./functions.sh + +"./installer_$(detect_arch)" \ + -version="${VERSION:-"latest"}" \ + -downloadUrl="${DOWNLOADURL:-""}" diff --git a/features/src/github-cli/installer.go b/features/src/github-cli/installer.go new file mode 100644 index 0000000..394482e --- /dev/null +++ b/features/src/github-cli/installer.go @@ -0,0 +1,104 @@ +package main + +import ( + "builder/installer" + "flag" + "fmt" + "os" + "path/filepath" + "regexp" + + "github.com/roemer/gover" +) + +////////// +// Configuration +////////// + +var versionRegex *regexp.Regexp = regexp.MustCompile(`(?m)^v(?P(\d+)\.(\d+)\.(\d+))$`) + +////////// +// Main +////////// + +func main() { + if err := runMain(); err != nil { + fmt.Printf("Error: %v\n", err) + os.Exit(1) + } +} + +func runMain() error { + // Handle the flags + version := flag.String("version", "latest", "") + downloadUrl := flag.String("downloadUrl", "", "") + flag.Parse() + + // Load settings from an external file + if err := installer.LoadOverrides(); err != nil { + return err + } + + // Apply override logic for URLs + installer.HandleGitHubOverride(downloadUrl, "cli/cli", "github-cli-download-url") + + // Create and process the feature + feature := installer.NewFeature("GitHub CLI", false, + &ghComponent{ + ComponentBase: installer.NewComponentBase("GitHub CLI", *version), + DownloadUrl: *downloadUrl, + }) + return feature.Process() +} + +////////// +// Implementation +////////// + +type ghComponent struct { + *installer.ComponentBase + DownloadUrl string +} + +func (c *ghComponent) GetAllVersions() ([]*gover.Version, error) { + tags, err := installer.Tools.GitHub.GetTags("cli", "cli") + if err != nil { + return nil, err + } + return installer.Tools.Versioning.ParseVersionsFromList(tags, versionRegex, true) +} + +func (c *ghComponent) InstallVersion(version *gover.Version) error { + // Map architecture + archPart, err := installer.Tools.System.MapArchitecture(map[string]string{ + installer.AMD64: "amd64", + installer.ARM64: "arm64", + }) + if err != nil { + return err + } + // Download the file + fileName := fmt.Sprintf("gh_%s_linux_%s.tar.gz", version.Raw, archPart) + downloadUrl, err := installer.Tools.Http.BuildUrl(c.DownloadUrl, "v"+version.Raw, fileName) + if err != nil { + return err + } + if err := installer.Tools.Download.ToFile(downloadUrl, fileName, "GitHub CLI"); err != nil { + return err + } + defer os.Remove(fileName) + // Extract to temp directory + tempDir, err := os.MkdirTemp("", "github-cli-extract") + if err != nil { + return err + } + defer os.RemoveAll(tempDir) + if err := installer.Tools.Compression.ExtractTarGz(fileName, tempDir, true); err != nil { + return err + } + // Install binary (located at bin/gh inside the extracted folder) + if err := installer.Tools.System.InstallBinaryToUsrLocalBin(filepath.Join(tempDir, "bin", "gh"), "gh"); err != nil { + return err + } + return nil +} diff --git a/features/test/github-cli/install.sh b/features/test/github-cli/install.sh new file mode 100644 index 0000000..a6453fe --- /dev/null +++ b/features/test/github-cli/install.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -e + +[[ -f "$(dirname "$0")/../functions.sh" ]] && source "$(dirname "$0")/../functions.sh" +[[ -f "$(dirname "$0")/functions.sh" ]] && source "$(dirname "$0")/functions.sh" + +check_version "$(gh version)" "gh version 2.67.0" diff --git a/features/test/github-cli/scenarios.json b/features/test/github-cli/scenarios.json new file mode 100644 index 0000000..bb2e174 --- /dev/null +++ b/features/test/github-cli/scenarios.json @@ -0,0 +1,15 @@ +{ + "install": { + "build": { + "dockerfile": "Dockerfile", + "options": [ + "--add-host=host.docker.internal:host-gateway" + ] + }, + "features": { + "./github-cli": { + "version": "2.67.0" + } + } + } +} diff --git a/features/test/github-cli/test-images.json b/features/test/github-cli/test-images.json new file mode 100644 index 0000000..42119c9 --- /dev/null +++ b/features/test/github-cli/test-images.json @@ -0,0 +1,6 @@ +[ + "mcr.microsoft.com/devcontainers/base:debian-11", + "mcr.microsoft.com/devcontainers/base:debian-12", + "mcr.microsoft.com/devcontainers/base:alpine", + "mcr.microsoft.com/devcontainers/base:ubuntu-24.04" +] diff --git a/override-all.env b/override-all.env index 34fb2ee..78e5f07 100644 --- a/override-all.env +++ b/override-all.env @@ -30,6 +30,9 @@ DOTNET_NUGET_CONFIG_PATH="" # git-lfs GIT_LFS_DOWNLOAD_URL="" +# github-cli +GITHUB_CLI_DOWNLOAD_URL="" + # github-copilot-cli GITHUB_COPILOT_CLI_DOWNLOAD_URL=""