Skip to content

Commit a7b8d4f

Browse files
authored
Added versioning, build and release process (#3)
1 parent 9963bf0 commit a7b8d4f

11 files changed

Lines changed: 309 additions & 35 deletions

File tree

.github/workflows/ci.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- "*"
6+
tags:
7+
- "v*.*.*"
8+
pull_request:
9+
branches:
10+
- "*"
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Set up Go 1.15
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: 1.15
20+
id: go
21+
22+
- name: Check out code into the Go module directory
23+
uses: actions/checkout@v2
24+
25+
- name: Verify go.mod is sane
26+
run: go mod tidy && git diff --no-patch --exit-code
27+
28+
- name: Install dependencies
29+
run: go mod download
30+
31+
- name: Cross platform build
32+
run: make build-cross
33+
34+
- name: Make release binaries
35+
if: contains(github.ref, 'tags')
36+
run: make dist
37+
38+
- name: Get the version
39+
id: get_version
40+
if: contains(github.ref, 'tags')
41+
run: echo "VERSION=${GITHUB_REF/refs\/tags\//}" |tee -a $GITHUB_ENV
42+
43+
- name: Generate Changelog
44+
if: contains(github.ref, 'tags')
45+
run: |
46+
LATEST_TAG=$(git tag --sort=creatordate | sed '$!d')
47+
PREVIOUS_TAG=$(git tag --sort=creatordate | sed 'x;$!d')
48+
if [ -z "${PREVIOUS_TAG}" ]; then
49+
REV_RANGE=HEAD
50+
else
51+
REV_RANGE=${PREVIOUS_TAG}..${LATEST_TAG}
52+
fi
53+
git log --pretty=format:"- %s %H (%aN)" --no-merges ${REV_RANGE} > ${VERSION}-CHANGELOG.txt
54+
cat ${VERSION}-CHANGELOG.txt
55+
56+
- name: Create release
57+
if: contains(github.ref, 'tags')
58+
uses: softprops/action-gh-release@v1
59+
env:
60+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
61+
with:
62+
body_path: ${{ env.VERSION }}-CHANGELOG.txt
63+
draft: false
64+
prerelease: false
65+
66+
- name: Upload release binaries
67+
if: contains(github.ref, 'tags')
68+
uses: svenstaro/upload-release-action@v2
69+
with:
70+
repo_token: ${{ secrets.GITHUB_TOKEN }}
71+
file: _dist/*{tar.gz,zip}
72+
tag: ${{ github.ref }}
73+
overwrite: true
74+
file_glob: true

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin
2+
_dist

Makefile

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
export GO111MODULE ?= on
2+
export GOARCH ?= amd64
3+
export CGO_ENABLED ?= 0
4+
5+
INSTALL_PATH ?= /usr/local/bin
6+
PROJECT ?= oc-helm
7+
DIST_DIRS := find * -type d -exec
8+
TAR_DIST_DIRS := find * -type d -not -name "*windows*" -exec
9+
ZIP_DIST_DIRS := find * -type d -name "*windows*" -exec
10+
REPOPATH ?= github.com/redhat-cop/$(PROJECT)
11+
COMMIT := $(shell git rev-parse HEAD)
12+
VERSION ?= $(shell git describe --always --tags --dirty)
13+
GOOS ?= $(shell go env GOOS)
14+
GOPATH ?= $(shell go env GOPATH)
15+
16+
BINDIR := $(CURDIR)/bin
17+
BINNAME := oc-helm
18+
DISTDIR := ./_dist
19+
PLATFORMS ?= darwin/amd64 windows/amd64 linux/amd64
20+
21+
VERSION_PACKAGE := $(REPOPATH)/pkg/version
22+
COMPRESS:=gzip --best -k -c
23+
24+
DATE_FMT = %Y-%m-%dT%H:%M:%SZ
25+
ifdef SOURCE_DATE_EPOCH
26+
BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+$(DATE_FMT)" 2>/dev/null)
27+
else
28+
BUILD_DATE ?= $(shell date "+$(DATE_FMT)")
29+
endif
30+
GO_LDFLAGS :="-s -w
31+
GO_LDFLAGS += -X $(VERSION_PACKAGE).version=$(VERSION)
32+
GO_LDFLAGS += -X $(VERSION_PACKAGE).buildDate=$(BUILD_DATE)
33+
GO_LDFLAGS += -X $(VERSION_PACKAGE).gitCommit=$(COMMIT)
34+
GO_LDFLAGS +="
35+
36+
GO_FILES := $(shell find . -type f -name '*.go')
37+
38+
.PHONY: all
39+
all: build
40+
41+
.PHONY: build
42+
build: CGO_ENABLED := 1
43+
build: GO_LDFLAGS := $(subst -s -w,,$(GO_LDFLAGS))
44+
build:
45+
go build -race -ldflags $(GO_LDFLAGS) -o '$(BINDIR)'/$(BINNAME) main.go
46+
47+
build-cross: LDFLAGS += -extldflags "-static"
48+
build-cross: $(GO_FILES) $(BUILDDIR) gox
49+
GOFLAGS="-trimpath" gox -osarch="$(PLATFORMS)" -tags netgo -ldflags $(GO_LDFLAGS) -parallel=3 -output="_dist/{{.OS}}-{{.Arch}}/$(BINNAME)"
50+
51+
.PHONY: install
52+
install: build
53+
@install "$(BINDIR)/$(BINNAME)" "$(INSTALL_PATH)/$(BINNAME)"
54+
55+
gox:
56+
ifeq (, $(shell which gox))
57+
@{ \
58+
set -e ;\
59+
GOX_TMP_DIR=$$(mktemp -d) ;\
60+
cd $$GOX_TMP_DIR ;\
61+
go mod init tmp ;\
62+
go get github.com/mitchellh/gox ;\
63+
rm -rf $$GOX_TMP_DIR ;\
64+
}
65+
GOX=$(GOBIN)/controller-gen
66+
else
67+
GOX=$(shell which gox)
68+
endif
69+
70+
.PHONY: clean
71+
clean:
72+
@rm -rf '$(BINDIR)' $(DISTDIR)
73+
74+
.PHONY: dist
75+
dist:
76+
( \
77+
cd $(DISTDIR) && \
78+
$(DIST_DIRS) cp ../LICENSE {} \; && \
79+
$(DIST_DIRS) cp ../README.md {} \; && \
80+
$(TAR_DIST_DIRS) tar -zcf oc-helm-${VERSION}-{}.tar.gz {} \; && \
81+
$(ZIP_DIST_DIRS) zip -r oc-helm-${VERSION}-{}.zip {} \; \
82+
)

README.md

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,9 @@ The following prerequisites must be met prior to using the plugin:
3131

3232
Perform the following steps to setup and configure the plugin on your machine:
3333

34-
1. Clone the repository and navigate to the project directory:
35-
36-
```shell
37-
git clone https://github.com/sabre1041/oc-helm
38-
cd oc-helm
39-
```
34+
1. Download the latest release for your operating system from the [Release Page](https://github.com/sabre1041/oc-helm/releases)
4035

41-
2. Build the plugin
42-
43-
```shell
44-
go build -o oc-helm main.go
45-
```
46-
47-
3. Copy the resulting binary to a location on your `PATH`
48-
49-
```shell
50-
mv oc-helm <DIRECTORY_ON_PATH>
51-
```
52-
53-
4. Confirm the installation of the plugin
54-
55-
```shell
56-
oc helm
57-
58-
OpenShift Command Line tool to interact with Helm capabilities.
59-
60-
Usage:
61-
oc-helm [command]
62-
...
63-
64-
```
36+
2. Extract the compressed archive and move the resulting binary to your path
6537

6638
## Walkthrough
6739

@@ -145,4 +117,40 @@ Finally, uninstall the chart
145117
oc helm uninstall quarkus
146118

147119
release "quarkus" uninstalled
120+
```
121+
122+
## Development
123+
124+
1. Clone the repository and navigate to the project directory:
125+
126+
```shell
127+
git clone https://github.com/sabre1041/oc-helm
128+
cd oc-helm
129+
```
130+
131+
2. Build the plugin
132+
133+
```shell
134+
make build
135+
```
136+
137+
The binary will be placed in the `bin` folder
138+
139+
3. Install the binary to your path
140+
141+
```shell
142+
make install
143+
```
144+
145+
4. Confirm the installation of the plugin
146+
147+
```shell
148+
oc helm
149+
150+
OpenShift Command Line tool to interact with Helm capabilities.
151+
152+
Usage:
153+
oc-helm [command]
154+
...
155+
148156
```

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func newRootCmd(commandLineOptions *options.CommandLineOption) (*cobra.Command,
3434
SilenceErrors: true,
3535
}
3636

37+
rootCmd.PersistentFlags().StringVar(&commandLineOptions.APIServer, "apiserver", os.Getenv(constants.OPENSHIFT_APISERVER), "OpenShift API Server Hostname")
3738
rootCmd.PersistentFlags().StringVar(&commandLineOptions.ConsoleHostname, "console-hostname", os.Getenv(constants.OPENSHIFT_CONSOLE_HOSTNAME_ENV), "OpenShift Console Hostname")
3839
rootCmd.PersistentFlags().StringVar(&commandLineOptions.Context, "context", "", "Kubernetes Context")
3940
rootCmd.PersistentFlags().StringVarP(&commandLineOptions.Namespace, "namespace", "n", "", "Kubernetes namespace")
@@ -47,6 +48,7 @@ func newRootCmd(commandLineOptions *options.CommandLineOption) (*cobra.Command,
4748
newRollbackCmd(commandLineOptions),
4849
newUninstallCmd(commandLineOptions),
4950
newUpgradeCmd(commandLineOptions),
51+
newVersionCmd(commandLineOptions),
5052
)
5153

5254
return rootCmd, nil

cmd/version.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"fmt"
20+
"text/template"
21+
22+
"github.com/redhat-cop/oc-helm/pkg/options"
23+
"github.com/redhat-cop/oc-helm/pkg/version"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
const (
28+
versionTemplate = `{{.Version}}
29+
`
30+
fullInfoTemplate = `oc-helm: {{.Version}}
31+
platform: {{.Platform}}
32+
git commit: {{.GitCommit}}
33+
build date: {{.BuildDate}}
34+
go version: {{.GoVersion}}
35+
compiler: {{.Compiler}}
36+
`
37+
flagFull = "full"
38+
)
39+
40+
func newVersionCmd(commandLineOptions *options.CommandLineOption) *cobra.Command {
41+
42+
// indexCmd represents the index command
43+
versionCmd := &cobra.Command{
44+
Use: "version",
45+
Short: "Print the version information",
46+
Long: "Print the version information",
47+
Args: cobra.NoArgs,
48+
RunE: func(cmd *cobra.Command, args []string) error {
49+
50+
var tpl string
51+
52+
if cmd.Flag(flagFull).Changed {
53+
tpl = fullInfoTemplate
54+
} else {
55+
tpl = versionTemplate
56+
}
57+
58+
var t = template.Must(template.New("info").Parse(tpl))
59+
60+
if err := t.Execute(commandLineOptions.Streams.Out, version.GetBuildInfo()); err != nil {
61+
return fmt.Errorf("Could not print version info")
62+
}
63+
64+
return nil
65+
66+
},
67+
}
68+
69+
versionCmd.Flags().BoolP(flagFull, "f", false, "print extended version information")
70+
71+
return versionCmd
72+
73+
}

pkg/action/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (b *baseAction) Setup() error {
4646
b.commandLineOptions.ConsoleHostname = consoleURL.Hostname()
4747
} else {
4848

49-
serverURL, err := url.Parse(b.commandLineOptions.Server)
49+
serverURL, err := url.Parse(b.commandLineOptions.APIServer)
5050

5151
if err != nil {
5252
return err

pkg/client/openshift.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewOpenShiftClient(commonOptions *options.CommandLineOption) (*OpenShiftCli
3333

3434
return &OpenShiftClient{
3535
headers: headers,
36-
server: commonOptions.Server,
36+
server: commonOptions.APIServer,
3737
httpClient: httpClient,
3838
}, nil
3939
}

pkg/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package constants
33
const (
44
OPENSHIFT_HELM_INDEX_SEPERATOR = "--"
55
OPENSHIFT_CONSOLE_HOSTNAME_ENV = "OPENSHIFT_CONSOLE_HOSTNAME"
6+
OPENSHIFT_APISERVER = "OPENSHIFT_APISERVER"
67
OPENSHIFT_OAUTH_TOKEN_ENV = "OPENSHIFT_OAUTH_TOKEN"
78
)

pkg/options/options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type CommandLineOption struct {
1414
Token string
1515
Insecure bool
1616
Namespace string
17-
Server string
17+
APIServer string
1818
ChartName string
1919
ChartRepository string
2020
Context string
@@ -67,8 +67,8 @@ func (c *CommandLineOption) Process() error {
6767
c.Namespace = currentContext.Namespace
6868
}
6969

70-
if c.Server == "" {
71-
c.Server = rawConfig.Clusters[currentContext.Cluster].Server
70+
if c.APIServer == "" {
71+
c.APIServer = rawConfig.Clusters[currentContext.Cluster].Server
7272
}
7373

7474
return nil

0 commit comments

Comments
 (0)