Skip to content

Commit

Permalink
feat: run docker login
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdk committed Feb 8, 2024
1 parent 82d79b6 commit bd43211
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ linters:
- structcheck
- varcheck

# Linters that are not used for this project.
- wrapcheck
- depguard

linters-settings:
goheader:
template: |-
Expand Down
24 changes: 24 additions & 0 deletions docker/exec.go
Original file line number Diff line number Diff line change
@@ -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()
}
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package main
import (
"fmt"
"os"

"github.com/joshdk/actions-docker-shim/docker"
)

func main() {
Expand All @@ -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
}

0 comments on commit bd43211

Please sign in to comment.