Skip to content

Commit 8424056

Browse files
author
Charlie McNeill
authored
[vergo:minor-release] Use Bearer auth if presented with the GITHUB_TOKEN env variable (#17)
1 parent 4ccf91a commit 8424056

5 files changed

Lines changed: 61 additions & 26 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [0.28.0] - 09-04-2024
4+
Prefers to use the GITHUB_TOKEN Bearer authentication over SSH if the environment variable is present.
5+
This is useful when pushing tags using GitHub actions.
6+
37
## [0.27.0] - 08-02-2023
48
Fixed bug introduced in version `0.21.0` where `vergo bump` and `vergo check` would fail if the current commit is not
59
the latest on a versioned branch e.g. `master` or `main`.

GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
export
44

55
GORELEASER_VERSION := 0.179.0
6-
LINTER_VERSION := 1.46.2
6+
LINTER_VERSION := 1.53.3
77
PATH := $(shell pwd)/bin:$(PATH)
88
SHELL := bash
99

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ You can address the error `ssh: handshake failed: knownhosts: key is unknown ` w
142142
- Calling `ssh-keyscan -H github.com >> ~/.ssh/known_hosts` prior to pushing your vergo tag to introduce github to your known hosts.
143143
- Calling `vergo` with the `--disable-strict-host-check` flag. This should only be used on CI where known hosts are not cached.
144144

145+
## Using GITHUB_TOKEN inside GitHub Actions
146+
147+
Vergo will first try to use Token Bearer Authentication using the GITHUB_TOKEN environment variable when running inside a GitHub Action/Workflow. It will fallback to ssh based authentication if the GITHUB_TOKEN is not present.
148+
149+
Inside github actions please ensure that the GITHUB_TOKEN environment variable is set with the `${{ secrets.GITHUB_TOKEN }}` in order to push to the current repository.
150+
151+
Please see [token authentication](https://docs.github.com/en/actions/security-guides/automatic-token-authentication#using-the-github_token-in-a-workflow) for further details.
152+
153+
The GITHUB_TOKEN will require the following permissions to be able to push:
154+
```yaml
155+
permissions:
156+
contents: write
157+
```
158+
145159
## Running Locally - SSH Key Failures
146160
You can address the error `FATA[0000] failed to get signers, make sure to add private key identities to the authentication agent error="<nil>"` when pushing tags with vergo by:
147161
- Calling `ssh-add ~/.ssh/<github_key>` to add your github ssh key to the ssh agent

git/git.go

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"sort"
1010
"strings"
1111

12+
"github.com/go-git/go-git/v5/plumbing/transport"
13+
"github.com/go-git/go-git/v5/plumbing/transport/http"
14+
1215
"github.com/Masterminds/semver/v3"
1316
gogit "github.com/go-git/go-git/v5"
1417
"github.com/go-git/go-git/v5/config"
@@ -118,33 +121,30 @@ type PushTagFunc func(
118121
func PushTag(r *gogit.Repository, version, prefix, remote string, dryRun bool, disableStrictHostChecking bool) error {
119122
tag := prefix + version
120123

121-
socket, found := os.LookupEnv("SSH_AUTH_SOCK")
122-
if !found {
123-
return ErrUndefinedSSHAuthSock
124-
}
125-
126-
conn, err := net.Dial("unix", socket)
127-
if err != nil {
128-
log.WithError(err).Fatalln("Failed to open SSH_AUTH_SOCK")
129-
}
124+
var auth transport.AuthMethod
130125

131-
agentClient := agent.NewClient(conn)
132-
defer func() {
133-
_ = conn.Close()
134-
}()
126+
if githubToken, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
127+
log.Debug("Using Github Bearer Token Auth")
128+
auth = &http.TokenAuth{
129+
Token: githubToken,
130+
}
131+
} else if socket, ok := os.LookupEnv("SSH_AUTH_SOCK"); ok {
132+
log.Debug("Using SSH Agent Authentication")
133+
conn, err := net.Dial("unix", socket)
134+
if err != nil {
135+
log.WithError(err).Fatalln("Failed to open SSH_AUTH_SOCK")
136+
}
135137

136-
signers, err := agentClient.Signers()
137-
if err != nil || len(signers) == 0 {
138-
log.WithError(err).Fatalln("failed to get signers, make sure to add private key identities to the authentication agent")
139-
}
138+
agentClient := agent.NewClient(conn)
139+
defer func() {
140+
_ = conn.Close()
141+
}()
140142

141-
auth := &ssh.PublicKeys{
142-
User: "git",
143-
Signer: signers[0],
144-
}
143+
sshAuth := generateSshAuth(agentClient, disableStrictHostChecking)
145144

146-
if disableStrictHostChecking {
147-
auth.HostKeyCallback = cryptossh.InsecureIgnoreHostKey()
145+
auth = sshAuth
146+
} else {
147+
return ErrUndefinedSSHAuthSock
148148
}
149149

150150
log.Debugf("Pushing tag: %v", tag)
@@ -159,7 +159,7 @@ func PushTag(r *gogit.Repository, version, prefix, remote string, dryRun bool, d
159159
if dryRun {
160160
log.Infof("Dry run: push tag %v", tag)
161161
} else {
162-
err = r.Push(po)
162+
err := r.Push(po)
163163

164164
if err != nil {
165165
if errors.Is(err, gogit.NoErrAlreadyUpToDate) {
@@ -173,6 +173,23 @@ func PushTag(r *gogit.Repository, version, prefix, remote string, dryRun bool, d
173173
return nil
174174
}
175175

176+
func generateSshAuth(agentClient agent.ExtendedAgent, disableStrictHostChecking bool) *ssh.PublicKeys {
177+
signers, err := agentClient.Signers()
178+
if err != nil || len(signers) == 0 {
179+
log.WithError(err).Fatalln("failed to get signers, make sure to add private key identities to the authentication agent")
180+
}
181+
182+
sshAuth := &ssh.PublicKeys{
183+
User: "git",
184+
Signer: signers[0],
185+
}
186+
187+
if disableStrictHostChecking {
188+
sshAuth.HostKeyCallback = cryptossh.InsecureIgnoreHostKey()
189+
}
190+
return sshAuth
191+
}
192+
176193
func ListRefs(repo *gogit.Repository, prefix string, direction SortDirection, maxListSize int) ([]SemverRef, error) {
177194
versions, err := refsWithPrefix(repo, prefix)
178195
if err != nil {

release/release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func ValidateHEAD(repo *gogit.Repository, remoteName string, versionedBranches [
9090
validRef := false
9191
for _, mainBranchName := range versionedBranches {
9292
remote, err := repo.Remote(remoteName)
93-
if err != nil && err != gogit.ErrRemoteNotFound {
93+
if err != nil && !errors.Is(err, gogit.ErrRemoteNotFound) {
9494
return err
9595
}
9696
var branchRef plumbing.ReferenceName

0 commit comments

Comments
 (0)