From bd43211284c50419cfe24e0c4157b69d586681cd Mon Sep 17 00:00:00 2001 From: Josh Komoroske Date: Thu, 8 Feb 2024 18:44:24 -0500 Subject: [PATCH] feat: run docker login --- .golangci.yaml | 4 ++++ docker/exec.go | 24 ++++++++++++++++++++++++ main.go | 22 ++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 docker/exec.go diff --git a/.golangci.yaml b/.golangci.yaml index 0e5d17b..4e0e6ec 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -16,6 +16,10 @@ linters: - structcheck - varcheck + # Linters that are not used for this project. + - wrapcheck + - depguard + linters-settings: goheader: template: |- diff --git a/docker/exec.go b/docker/exec.go new file mode 100644 index 0000000..0a8d23a --- /dev/null +++ b/docker/exec.go @@ -0,0 +1,24 @@ +// Copyright Josh Komoroske. All rights reserved. +// Use of this source code is governed by the MIT license, +// a copy of which can be found in the LICENSE.txt file. +// SPDX-License-Identifier: MIT + +// Package docker exposes functions for executing specific docker commands. +package docker + +import ( + "os" + "os/exec" + "strings" +) + +// Login executes a docker login to ghcr.io with the given username and +// password. +func Login(username, password string) error { + cmd := exec.Command("/usr/bin/docker", "login", "ghcr.io", "-u", username, "--password-stdin") + cmd.Stdin = strings.NewReader(password + "\n") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd.Run() +} diff --git a/main.go b/main.go index b0f79d5..67152fa 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ package main import ( "fmt" "os" + + "github.com/joshdk/actions-docker-shim/docker" ) func main() { @@ -18,6 +20,26 @@ func main() { } } +//nolint:forbidigo,wsl func mainCmd() error { + var token string + if value := os.Getenv("GITHUB_TOKEN"); value != "" { + // Environment variable named "GITHUB_TOKEN". + token = value + } else if value := os.Getenv("INPUT_GITHUB_TOKEN"); value != "" { + // Input named "github-token". + token = value + } else if value := os.Getenv("INPUT_TOKEN"); value != "" { + // Input named "token". + token = value + } + + fmt.Printf("::group::%s\n", "Docker login") + err := docker.Login(os.Getenv("GITHUB_ACTOR"), token) + fmt.Println("::endgroup::") + if err != nil { + return err + } + return nil }