Skip to content

Commit

Permalink
Merge pull request #102 from rafibarash/automate-release
Browse files Browse the repository at this point in the history
Automate release with goreleaser
  • Loading branch information
rafibarash committed Sep 9, 2021
2 parents 6eedb0c + 15b613c commit 4cdd60d
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 19 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: goreleaser

on:
push:
tags: ["*"]

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Unshallow
run: git fetch --prune --unshallow
- uses: actions/setup-go@v1
with:
go-version: 1.17.x
- uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ bazel*
**/testdata
docker-credential-gcr
.idea
*.iml
*.iml
dist/
31 changes: 31 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This is an example .goreleaser.yml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
before:
hooks:
# Needed because we use go modules.
- go mod tidy
builds:
- env:
- CGO_ENABLED=0
ldflags:
- "-s -w -X github.com/GoogleCloudPlatform/docker-credential-gcr/config.Version={{.Version}}"
goarch:
- amd64
- arm64
- 386
goos:
- linux
- windows
- darwin
archives:
- name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}-{{ .Version }}"
checksum:
name_template: "checksums.txt"
snapshot:
name_template: "{{ .Version }}"
changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
2 changes: 1 addition & 1 deletion cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ func NewVersionSubcommand() subcommands.Command {
}

func (p *versionCmd) Execute(context.Context, *flag.FlagSet, ...interface{}) subcommands.ExitStatus {
fmt.Fprintf(os.Stdout, "Google Container Registry Docker credential helper %d.%d.%d\n", config.MajorVersion, config.MinorVersion, config.PatchVersion)
fmt.Fprintf(os.Stdout, "Google Container Registry Docker credential helper %s\n", config.Version)
return subcommands.ExitSuccess
}
28 changes: 16 additions & 12 deletions config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package config
import (
"context"
"fmt"
"runtime/debug"
"strings"

"golang.org/x/oauth2/google"
)
Expand All @@ -32,19 +34,21 @@ const (
// performing the OAuth2 Authorization Code grant flow.
// See https://developers.google.com/identity/protocols/OAuth2InstalledApp
GCRCredHelperClientNotSoSecret = "HpVi8cnKx8AAkddzaNrSWmS8"
)

// From http://semver.org/
// MAJOR version when you make incompatible API changes,
// MINOR version when you add functionality in a backwards-compatible manner, and
// PATCH version when you make backwards-compatible bug fixes.
// Version can be set via:
// -ldflags="-X 'github.com/GoogleCloudPlatform/docker-credential-gcr/config.Version=$TAG'"
var Version string

// MajorVersion is the credential helper's major version number.
MajorVersion = 2
// MinorVersion is the credential helper's minor version number.
MinorVersion = 0
// PatchVersion is the credential helper's patch version number.
PatchVersion = 4
)
func init() {
if Version == "" {
i, ok := debug.ReadBuildInfo()
if !ok {
return
}
Version = i.Main.Version
}
}

// DefaultGCRRegistries contains the list of default registries to authenticate for.
var DefaultGCRRegistries = [...]string{
Expand Down Expand Up @@ -112,4 +116,4 @@ var GCRScopes = []string{"https://www.googleapis.com/auth/cloud-platform"}
var OAuthHTTPContext = context.Background()

// GcrOAuth2Username is the Basic auth username accompanying Docker requests to GCR.
var GcrOAuth2Username = fmt.Sprintf("_dcgcr_%d_%d_%d_token", MajorVersion, MinorVersion, PatchVersion)
var GcrOAuth2Username = fmt.Sprintf("_dcgcr_%s_token", strings.ReplaceAll(Version, ".", "_"))
2 changes: 1 addition & 1 deletion credhelper/helper_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/golang/mock/gomock"
)

var expectedGCRUsername = fmt.Sprintf("_dcgcr_%d_%d_%d_token", config.MajorVersion, config.MinorVersion, config.PatchVersion)
var expectedGCRUsername = fmt.Sprintf("_dcgcr_%s_token", config.Version)

var testGCRHosts = [...]string{
"gcr.io",
Expand Down
7 changes: 6 additions & 1 deletion test/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !unit && !gazelle
// +build !unit,!gazelle

// Copyright 2016 Google, Inc.
Expand Down Expand Up @@ -123,7 +124,11 @@ func TestEndToEnd_GCRCreds(t *testing.T) {
}

if match, err := regexp.MatchString("_dcgcr_(?:[0-9]+_)*token", creds.Username); !match || err != nil {
t.Errorf("Bad GCR username: Wanted: %q, Got (val, err): %q, %v", "_dcgcr_(?:[0-9]+_)*token", creds.Username, err)
// Fail if not a dev version.
devUsername := "_dcgcr__token"
if creds.Username != devUsername {
t.Errorf("Bad GCR username: Wanted: %q, Got (val, err): %q, %v", "_dcgcr_(?:[0-9]+_)*token", creds.Username, err)
}
}
if creds.Secret != gcrAccessToken {
t.Errorf("Bad GCR access token. Wanted: %s, Got: %s", gcrAccessToken, creds.Secret)
Expand Down
11 changes: 8 additions & 3 deletions test/version_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !unit && !gazelle
// +build !unit,!gazelle

// Copyright 2016 Google, Inc.
Expand Down Expand Up @@ -32,9 +33,13 @@ func TestVersion(t *testing.T) {
}

// Enforce a particular format so that a regex can extract the version easily.
expectedRegex := "Google Container Registry Docker credential helper [0-9]+\\.[0-9]+\\.[0-9]+\n"
actual := out.String()
if match, _ := regexp.MatchString(expectedRegex, actual); !match {
t.Fatalf("Expected version string to match: %s, got: %s", expectedRegex, actual)
expectedProdRegex := "Google Container Registry Docker credential helper [0-9]+\\_[0-9]+\\_[0-9]+\n"
if match, _ := regexp.MatchString(expectedProdRegex, actual); !match {
// Fail if not a dev version.
expectedDevString := "Google Container Registry Docker credential helper (devel)\n"
if actual != expectedDevString {
t.Fatalf("Expected version string to match: %s, got: %s", expectedProdRegex, actual)
}
}
}

0 comments on commit 4cdd60d

Please sign in to comment.