Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable GitHub token env var key #22

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## [0.29.0] - 13-05-2024
Allows the environment variable key that is looked up to enable token based authentication to be configurable. The default is now `GH_TOKEN`.

## [0.28.1] - 09-05-2024
Fixed a bug with GITHUB_TOKEN authentication where pushes would fail when configured to use a GitHub token.
Fixed version of golangci-lint to work with more recent golang versions.
Expand Down
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,37 @@ You can address the error `ssh: handshake failed: knownhosts: key is unknown ` w
- Calling `ssh-keyscan -H github.com >> ~/.ssh/known_hosts` prior to pushing your vergo tag to introduce github to your known hosts.
- Calling `vergo` with the `--disable-strict-host-check` flag. This should only be used on CI where known hosts are not cached.

## Using GITHUB_TOKEN inside GitHub Actions
## Authentication

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.
Vergo supports 2 method of Git authentication:
- SSH
- Access token

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.
### SSH

SSH authentication is enabled when the `SSH_AUTH_SOCK` environment variable is present. To use SSH `SSH_AUTH_SOCK` will need to contain the path of the unix file socket that the SSH client uses to connect to the SSH agent.

### Access token

Access token authentication is enabled when an environment variable with the same key as what is configured by the `--token-env-var-key` CLI arg exists. This takes precedence over `SSH_AUTH_SOCK`, so if both are set then access token auth will be used. The configurability of `--token-env-var-key` allows the following:
- `GITHUB_TOKEN` is set but SHOULD NOT be used by `vergo`
- `GH_TOKEN` is set and SHOULD be used by `vergo`

The above can be achieved with `vergo --token-env-var-key GH_TOKEN`.

## Using token authentication inside GitHub Actions

Inside GitHub Actions please ensure that the value of the `GH_TOKEN` environment variable is set to `${{ secrets.GITHUB_TOKEN }}` in order to push to the current repository. As above, `GH_TOKEN` can be changed to something else by setting `--token-env-var-key`.

Example workflow job step using the provided GITHUB_TOKEN with `vergo`:
```yaml
- name: Tag release
run: |
vergo check release -t my-app
vergo bump minor -t my-app --push-tag
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

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.

Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func BumpCmd(bumpFunc bump.Func, pushTag vergo.PushTagFunc) *cobra.Command {
return err
}
if pushTagParam {
err = pushTag(repo, version.String(), rootFlags.tagPrefix, rootFlags.remote, rootFlags.dryRun, rootFlags.disableStrictHostChecking)
err = pushTag(repo, version.String(), rootFlags.tagPrefix, rootFlags.remote, rootFlags.dryRun, rootFlags.disableStrictHostChecking, rootFlags.tokenEnvVarKey)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/cmd_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ const withMetadata = "with-metadata"

const sortDirection = "sort-direction"
const maxListSize = "max-list-size"

const tokenEnvVarKey = "token-env-var-key"
4 changes: 2 additions & 2 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func bumpSuccess(t *testing.T) bump.Func {
}
}

func mockPushTagSuccess(_ *git.Repository, _, _, _ string, _ bool, _ bool) error {
func mockPushTagSuccess(_ *git.Repository, _, _, _ string, _ bool, _ bool, _ string) error {
return nil
}

func mockPushTagFailure(_ *git.Repository, _, _, _ string, _ bool, _ bool) error {
func mockPushTagFailure(_ *git.Repository, _, _, _ string, _ bool, _ bool, _ string) error {
return errors.New("push tag failed")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func PushCmd() *cobra.Command {
if err != nil {
return err
}
err = vergo.PushTag(repo, ref.Version.String(), rootFlags.tagPrefix, rootFlags.remote, rootFlags.dryRun, rootFlags.disableStrictHostChecking)
err = vergo.PushTag(repo, ref.Version.String(), rootFlags.tagPrefix, rootFlags.remote, rootFlags.dryRun, rootFlags.disableStrictHostChecking, rootFlags.tokenEnvVarKey)
if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func RootCmd() *cobra.Command {
rootCmd.PersistentFlags().StringP(repositoryLocation, "l", ".", "repository location")
rootCmd.PersistentFlags().String(logLevel, "Info", "set log level")
rootCmd.PersistentFlags().BoolP(strictHostChecking, "d", false, "disable strict host checking for git. should only be enabled on ci.")
rootCmd.PersistentFlags().StringP(tokenEnvVarKey, "k", "GH_TOKEN", "environment variable key to use for lookup when deciding if token based git auth should be used")
rootCmd.PersistentFlags().Bool(dryRun, false, "dry run")
rootCmd.PersistentFlags().StringSlice(versionedBranchNames, []string{"master", "main"},
"names of the main working branches")
Expand All @@ -30,6 +31,7 @@ func RootCmd() *cobra.Command {

type RootFlags struct {
remote, tagPrefix, tagPrefixRaw, repositoryLocation string
tokenEnvVarKey string
logLevel log.Level
withPrefix, dryRun, disableStrictHostChecking bool
versionedBranches []string
Expand Down Expand Up @@ -68,6 +70,10 @@ func readRootFlags(cmd *cobra.Command) (*RootFlags, error) {
if err != nil {
return nil, err
}
tokenEnvVarKey, err := cmd.Flags().GetString(tokenEnvVarKey)
if err != nil {
return nil, err
}
logLevel, err := log.ParseLevel(logLevelParam)
if err != nil {
log.WithError(err).Errorln("invalid log level, using INFO instead")
Expand All @@ -85,6 +91,7 @@ func readRootFlags(cmd *cobra.Command) (*RootFlags, error) {
dryRun: dryRun,
withPrefix: withPrefix,
disableStrictHostChecking: disableStrictHostChecking,
tokenEnvVarKey: tokenEnvVarKey,
}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ func CreateTag(repo *gogit.Repository, version, prefix string, dryRun bool) erro
type PushTagFunc func(
repo *gogit.Repository,
version, prefix, remote string,
dryRun bool, disableStrictHostChecking bool) error
dryRun bool, disableStrictHostChecking bool, tokenEnvVarKey string) error

func PushTag(r *gogit.Repository, version, prefix, remote string, dryRun bool, disableStrictHostChecking bool) error {
func PushTag(r *gogit.Repository, version, prefix, remote string, dryRun bool, disableStrictHostChecking bool, tokenEnvVarKey string) error {
tag := prefix + version

var auth transport.AuthMethod

if githubToken, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
if githubToken, ok := os.LookupEnv(tokenEnvVarKey); ok {
log.Debug("Using Github Bearer Token Auth")
auth = &http.BasicAuth{
Username: "can-be-anything",
Expand Down
Binary file removed vergo
Binary file not shown.