Skip to content

Commit

Permalink
Merge pull request #8 from smjt-h/gohackathon
Browse files Browse the repository at this point in the history
add go and glide
  • Loading branch information
smjt-h authored Dec 7, 2023
2 parents f359ef9 + f2ad118 commit e493a21
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 5 deletions.
1 change: 1 addition & 0 deletions builder/rules/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func (h Harness) GetRules() []Rule {
harness.ConfigurePipelineVersion,
harness.ConfigurePlatform,
harness.ConfigureGo,
harness.ConfigureGoGlide,
harness.ConfigureNode,
harness.ConfigurePython,
harness.ConfigureC,
Expand Down
8 changes: 5 additions & 3 deletions builder/rules/harness/rule_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ func ConfigureDocker(fsys fs.FS, pipeline *spec.Pipeline) error {
script := new(spec.StepPlugin)
script.Image = "plugins/docker"
script.With = map[string]interface{}{
"tags": "latest",
"repo": repo,
"dry_run": true,
"tags": "latest",
"repo": repo,
"dry_run": true,
"username": "<+input>",
"password": "<+input>",
}

if useImage {
Expand Down
29 changes: 27 additions & 2 deletions builder/rules/harness/rule_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,39 @@ func ConfigureGo(fsys fs.FS, pipeline *spec.Pipeline) error {
// add the go test step
{
script := new(spec.StepExec)
script.Run = "go test -v ./..."
script.Run = "go test -coverprofile=coverage.out ./..."

if useImage {
script.Image = "golang"
}

step := new(spec.Step)
step.Name = "go_test"
step.Name = "go_test_coverage"
step.Type = "script"
step.Spec = script

stage.Steps = append(stage.Steps, step)
}

// add the go test with report step
{
script := new(spec.StepExec)
script.Run = `export GOBIN=/home/harness/go/bin
export PATH=/home/harness/go/bin:$PATH
echo $PATH
go install github.com/jstemmer/go-junit-report/v2@latest
go test -v 2>&1 ./... | go-junit-report -set-exit-code > report.xml`

if useImage {
script.Image = "golang"
}

script.Reports = append(script.Reports, &spec.Report{
Type: "junit",
Path: []string{"/harness/report.xml"},
})
step := new(spec.Step)
step.Name = "go_test_report"
step.Type = "script"
step.Spec = script

Expand Down
164 changes: 164 additions & 0 deletions builder/rules/harness/rule_go_glide.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package harness

import (
"fmt"
"io/fs"

spec "github.com/drone/spec/dist/go"
"github.com/wings-software/autogen-go/utils"
)

// ConfigureGo configures a Go step.
func ConfigureGoGlide(fsys fs.FS, pipeline *spec.Pipeline) error {
stage := pipeline.Stages[0].Spec.(*spec.StageCI)

// check for the go.mod file.
if !utils.Exists(fsys, "vendor") && !utils.Exists(fsys, "glide.yaml") && !utils.Exists(fsys, "glide.lock") {
return nil
}

fmt.Println("Found vendor folder")
// check if we should use a container-based
// execution environment.
useImage := utils.IsContainerRuntime(pipeline)

// add the glide prerequisite step
{
script := new(spec.StepExec)
script.Run = `export GOBIN=/home/harness/go/bin
go install github.com/jstemmer/go-junit-report/v2@latest
mkdir /home/harness/go/src
echo cd to gopath
cd /home/harness/go
ls
export PATH=/home/harness/go/bin:$PATH
echo $PATH
# GLIDE_VERSION="v0.13.3"
GLIDE_URL="https://github.com/Masterminds/glide/releases/download/v0.13.3/glide-v0.13.3-linux-386.zip"
TMP_DIR="/tmp/glide_tmp"
# Create a temporary directory
mkdir -p "$TMP_DIR"
# Download Glide binary
wget "$GLIDE_URL" -O "$TMP_DIR/glide.zip"
# Extract the downloaded ZIP file
unzip "$TMP_DIR/glide.zip" -d "$TMP_DIR"
# Find Glide binary in extracted folder
# GLIDE_BINARY=$(find "$TMP_DIR" -name "glide*" -type f -executable)
# echo $GLIDE_BINARY
GLIDE_BINARY=$TMP_DIR/linux-386/glide
# Move Glide binary to go/bin directory
if [ -n "$GLIDE_BINARY" ]; then
mv "$GLIDE_BINARY" "/home/harness/go/bin/"
echo "Glide binary moved to /home/harness/go/bin/"
else
echo "Glide binary not found."
fi
# Clean up temporary directory
rm -rf "$TMP_DIR"
echo installed
cd /harness
export GO111MODULE=off
echo glide install
glide install
export GO111MODULE=off
go get
echo done go get
go build`

if useImage {
script.Image = "golang"
}

step := new(spec.Step)
step.Name = "prerequisite"
step.Type = "script"
step.Spec = script

stage.Steps = append(stage.Steps, step)
}

// add the go build step
{
script := new(spec.StepExec)
script.Run = `export GO111MODULE=off
go build
`

if useImage {
script.Image = "golang"
}

step := new(spec.Step)
step.Name = "go_build"
step.Type = "script"
step.Spec = script

stage.Steps = append(stage.Steps, step)
}

// add the go test step
{
script := new(spec.StepExec)
script.Run = `export GO111MODULE=off
go test -coverprofile=coverage.out ./...`

if useImage {
script.Image = "golang"
}

step := new(spec.Step)
step.Name = "go_test_coverage"
step.Type = "script"
step.Spec = script

stage.Steps = append(stage.Steps, step)
}

// add the go test with report step
{
script := new(spec.StepExec)
script.Run = `export GOBIN=/home/harness/go/bin
export PATH=/home/harness/go/bin:$PATH
go install github.com/jstemmer/go-junit-report/v2@latest
export GO111MODULE=off
go test -v 2>&1 ./... | go-junit-report -set-exit-code > report.xml`

if useImage {
script.Image = "golang"
}

script.Reports = append(script.Reports, &spec.Report{
Type: "junit",
Path: []string{"/harness/report.xml"},
})
step := new(spec.Step)
step.Name = "go_test_report"
step.Type = "script"
step.Spec = script

stage.Steps = append(stage.Steps, step)
}

return nil
}

0 comments on commit e493a21

Please sign in to comment.