Skip to content

Commit

Permalink
Merge pull request #3 from opendevstack/hotfix/rollout-stage
Browse files Browse the repository at this point in the history
Fix component stage rollout and agent image tag
  • Loading branch information
felipecruz91 authored Sep 7, 2021
2 parents 6081646 + 6f54dc9 commit 9dd3049
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bin/
converter
out/
internal/test/**/out/Jenkinsfile
out/Jenkinsfile
cover.out
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
export GO111MODULE=on

all: build dist
all: build test dist
.PHONY: all

build:
go build -o converter
.PHONY: build

test:
go test -v -coverprofile cover.out
.PHONY: test

dist:
mkdir -p bin
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags -a -installsuffix cgo -o bin/converter
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ You can run the binary for your platform. To preview the changes that will be do
## Run it with `Go`

```cli
go run . -filename="examples/machine-learning/Jenkinsfile" -out out/Jenkinsfile"
go run . -filename="examples/machine-learning/Jenkinsfile" -out "out/Jenkinsfile"
```

## Features
Expand Down
62 changes: 62 additions & 0 deletions internal/test/stage-rollout-multi-line/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// See https://www.opendevstack.org/ods-documentation/ for usage and customization.

@Library('[email protected]') _

node {
dockerRegistry = env.DOCKER_REGISTRY
}

odsComponentPipeline(
imageStreamTag: 'ods/jenkins-agent-maven:3.0.0',
branchToEnvironmentMapping: [
'master': 'dev',
'release/': 'test',
]
) { context ->

odsComponentStageImportOpenShiftImageOrElse(context) {
stageBuild(context)
odsComponentStageScanWithSonar(context)
odsComponentStageBuildOpenShiftImage(context,[
tailorParams: [
"DOCKER_REGISTRY=${dockerRegistry}"
]
])
}
odsComponentStageRolloutOpenShiftDeployment(context, [
deployTimeoutMinutes: 5,
openshiftDir: "openshift",
dockerDir: "docker",
resourceName : "${context.componentId}",
tailorSelector : "app=${context.projectId}-${context.componentId}",
tailorExclude : 'bc,is',
tailorParams: [
"DOCKER_REGISTRY=${dockerRegistry}"
]
// tailorParams : [
// "PROJECT=${context.projectId}", // projectId == atlasdev-dev, targetProject == atlasdev
// "ENV=${context.environment}",
// "DOCKER_REGISTRY=${dockerRegistry}",
// "COMPONENT=${context.componentId}",
// "TAGVERSION=${context.gitCommit}",
// ],
])
}

def stageBuild(def context) {
def javaOpts = "-Xmx512m"
def gradleTestOpts = "-Xmx128m"
def springBootEnv = context.environment
if (springBootEnv.contains('-dev')) {
springBootEnv = 'dev'
}
stage('Build and Unit Test') {
withEnv(["TAGVERSION=${context.tagversion}", "NEXUS_HOST=${context.nexusHost}", "NEXUS_USERNAME=${context.nexusUsername}", "NEXUS_PASSWORD=${context.nexusPassword}", "JAVA_OPTS=${javaOpts}","GRADLE_TEST_OPTS=${gradleTestOpts}","ENVIRONMENT=${springBootEnv}"]) {
def chmodXGradelw = sh(script: "chmod +x gradlew", returnStatus: true)
def status = sh(script: "./gradlew clean build --stacktrace --no-daemon", returnStatus: true)
if (status != 0) {
error "Build failed!"
}
}
}
}
45 changes: 45 additions & 0 deletions internal/test/stage-rollout-multi-line/golden/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// See https://www.opendevstack.org/ods-documentation/ for usage and customization.

@Library('[email protected]') _

node {
dockerRegistry = env.DOCKER_REGISTRY
}

odsComponentPipeline(
imageStreamTag: 'ods/jenkins-agent-maven:4.0.0',
branchToEnvironmentMapping: [
'master': 'dev',
'release/': 'test',
]
) { context ->

odsComponentFindOpenShiftImageOrElse(context) {
stageBuild(context)
odsComponentStageScanWithSonar(context)
odsComponentStageBuildOpenShiftImage(context,[
tailorParams: [
"DOCKER_REGISTRY=${dockerRegistry}"
]
])
}
odsComponentStageRolloutOpenShiftDeployment(context)
}

def stageBuild(def context) {
def javaOpts = "-Xmx512m"
def gradleTestOpts = "-Xmx128m"
def springBootEnv = context.environment
if (springBootEnv.contains('-dev')) {
springBootEnv = 'dev'
}
stage('Build and Unit Test') {
withEnv(["TAGVERSION=${context.tagversion}", "NEXUS_HOST=${context.nexusHost}", "NEXUS_USERNAME=${context.nexusUsername}", "NEXUS_PASSWORD=${context.nexusPassword}", "JAVA_OPTS=${javaOpts}","GRADLE_TEST_OPTS=${gradleTestOpts}","ENVIRONMENT=${springBootEnv}"]) {
def chmodXGradelw = sh(script: "chmod +x gradlew", returnStatus: true)
def status = sh(script: "./gradlew clean build --stacktrace --no-daemon", returnStatus: true)
if (status != 0) {
error "Build failed!"
}
}
}
}
Empty file.
22 changes: 22 additions & 0 deletions internal/test/stage-rollout-single-line/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// See https://www.opendevstack.org/ods-documentation/ for usage and customization.

@Library('[email protected]') _

odsComponentPipeline(
imageStreamTag: 'ods/jenkins-agent-base:3.0.0',
openshiftBuildTimeout: 30,
branchToEnvironmentMapping: [
'master': 'dev',
// 'release/': 'test'
]
) { context ->

odsComponentStageImportOpenShiftImageOrElse(context) {
odsComponentStageBuildOpenShiftImage(context)
}

// rollout auth proxy - this registers the dc for later promotion
odsComponentStageRolloutOpenShiftDeployment(context, [resourceName: "${context.componentId}-auth-proxy"])

odsComponentStageRolloutOpenShiftDeployment(context)
}
22 changes: 22 additions & 0 deletions internal/test/stage-rollout-single-line/golden/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// See https://www.opendevstack.org/ods-documentation/ for usage and customization.

@Library('[email protected]') _

odsComponentPipeline(
imageStreamTag: 'ods/jenkins-agent-base:4.0.0',
openshiftBuildTimeout: 30,
branchToEnvironmentMapping: [
'master': 'dev',
// 'release/': 'test'
]
) { context ->

odsComponentFindOpenShiftImageOrElse(context) {
odsComponentStageBuildOpenShiftImage(context)
}

// rollout auth proxy - this registers the dc for later promotion
odsComponentStageRolloutOpenShiftDeployment(context)

odsComponentStageRolloutOpenShiftDeployment(context)
}
Empty file.
Empty file added out/.keep
Empty file.
19 changes: 16 additions & 3 deletions replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,28 @@ func replaceLibrary(content string) string {
}

func replaceAgentImages(content string) string {
re := regexp.MustCompile(`'ods/jenkins-agent-(.*):.*'`)
return re.ReplaceAllString(content, "'ods/jenkins-agent-$1:4.x'")
re := regexp.MustCompile(`'ods\/jenkins-agent-(.*):(\d).(.*)'`)
return re.ReplaceAllString(content, "'ods/jenkins-agent-$1:4.$3'")
}

func replaceComponentStageImport(content string) string {
return strings.Replace(content, "odsComponentStageImportOpenShiftImageOrElse", "odsComponentFindOpenShiftImageOrElse", -1)
}

func replaceComponentStageRollout(content string) string {
content, match := replaceComponentStageRolloutMultiLine(content)
if !match {
content, _ = replaceComponentStageRolloutSingleLine(content)
}
return content
}

func replaceComponentStageRolloutMultiLine(content string) (string, bool) {
re := regexp.MustCompile(`(?ms)odsComponentStageRolloutOpenShiftDeployment\((context, \[\n?.*)]\)$`)
return re.ReplaceAllString(content, "odsComponentStageRolloutOpenShiftDeployment(context)"), re.Match([]byte(content))
}

func replaceComponentStageRolloutSingleLine(content string) (string, bool) {
re := regexp.MustCompile(`odsComponentStageRolloutOpenShiftDeployment(.*)`)
return re.ReplaceAllString(content, "odsComponentStageRolloutOpenShiftDeployment(context)")
return re.ReplaceAllString(content, "odsComponentStageRolloutOpenShiftDeployment(context)"), re.Match([]byte(content))
}
61 changes: 61 additions & 0 deletions replacer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"io/ioutil"
"testing"
)

func TestReplace(t *testing.T) {

type args struct {
inputFile string
convertedFile string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Stage Rollout Single Line",
args: args{
inputFile: "internal/test/stage-rollout-single-line/Jenkinsfile",
convertedFile: "internal/test/stage-rollout-single-line/out/Jenkinsfile",
},
want: readFile("internal/test/stage-rollout-single-line/golden/Jenkinsfile"),
},
{
name: "Stage Rollout Multi Line",
args: args{
inputFile: "internal/test/stage-rollout-multi-line/Jenkinsfile",
convertedFile: "internal/test/stage-rollout-multi-line/out/Jenkinsfile",
},
want: readFile("internal/test/stage-rollout-multi-line/golden/Jenkinsfile"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

Replace(tt.args.inputFile, tt.args.convertedFile, false)

converted, err := ioutil.ReadFile(tt.args.convertedFile)
if err != nil {
t.Errorf("failed to open file: %w", err)
}

if got := string(converted); got != string(tt.want) {
t.Errorf("Replace() =\n%v, want\n%v", got, string(tt.want))
}
})
}

}

func readFile(goldenFile string) string {
want, err := ioutil.ReadFile(goldenFile)
if err != nil {
panic(err)
}

return string(want)
}

0 comments on commit 9dd3049

Please sign in to comment.