Last active
May 14, 2020 20:22
-
-
Save kohenkatz/f779673e7239fa3d0fa9a5620a800667 to your computer and use it in GitHub Desktop.
Use GitLab CI to upload built Go binaries to a Nexus (raw) repository. Don't forget to define the four "UPLOAD_NEXUS_..." environment variables in the CI configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variables: | |
BIN_NAME: executable-name | |
ARTIFACTS_DIR: build | |
GO_PROJECT: example.com/go/project/module/path | |
GOFLAGS: -mod=readonly | |
GOPRIVATE: example.com/go/if/needed | |
GOBIN: ${CI_PROJECT_DIR}/bin | |
# Use this to install any binary tools needed | |
# and to create the output directory | |
before_script: | |
- export PATH=${GOBIN}:${PATH} | |
- go install github.com/kevinburke/go-bindata/go-bindata | |
- mkdir -p ${CI_PROJECT_DIR}/${ARTIFACTS_DIR} | |
build: | |
script: | |
- go generate ./... | |
- go build --tags release -i -v -o ${CI_PROJECT_DIR}/${ARTIFACTS_DIR}/${BIN_NAME} | |
- go run scripts/upload-to-nexus.go | |
artifacts: | |
paths: | |
- ${ARTIFACTS_DIR} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
) | |
func main() { | |
base, n := getenv("UPLOAD_NEXUS_BASE", 0) | |
repo, n := getenv("UPLOAD_NEXUS_REPO", n) | |
user, n := getenv("UPLOAD_NEXUS_USER", n) | |
pass, n := getenv("UPLOAD_NEXUS_PASS", n) | |
binName, n := getenv("BIN_NAME", n) | |
ciProjectDir, n := getenv("CI_PROJECT_DIR", n) | |
artifactsDir, n := getenv("ARTIFACTS_DIR", n) | |
if n != 7 { | |
fmt.Println("Missing one or more envvars UPLOAD_NEXUS_*") | |
os.Exit(1) | |
} | |
subfolder := "UNKNOWN" | |
if tagName, ok := os.LookupEnv("CI_COMMIT_TAG") { | |
subfolder = tagName | |
} else { | |
currentTime := time.Now().Format("20060102") | |
subfolder += "-" + currentTime | |
if hash, ok := os.LookupEnv("CI_COMMIT_SHORT_SHA"); ok { | |
subfolder += "-" + hash | |
} | |
} | |
url := fmt.Sprintf("%s/repository/%s/%s/%s/%s", base, repo, binName, subfolder, binName) | |
filePath := fmt.Sprintf("%s/%s/%s", ciProjectDir, artifactsDir, binName) | |
fmt.Printf("Uploading from %s\n", filePath) | |
fmt.Printf("Uploading to %s\n", url) | |
uploadFile(url, filePath, user, pass) | |
} | |
func getenv(name string, number int) (string, int) { | |
if value, ok := os.LookupEnv(name); ok { | |
return value, number + 1 | |
} | |
return "", number | |
} | |
func uploadFile(url string, filepath string, user string, pass string) { | |
f, err := os.Open(filepath) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer f.Close() | |
req, err := http.NewRequest(http.MethodPut, url, f) | |
if err != nil { | |
log.Fatal(err) | |
} | |
req.SetBasicAuth(user, pass) | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer resp.Body.Close() | |
content, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(string(content)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment