Skip to content

Commit 706da5d

Browse files
authored
[APM-CI] Pipeline for OpBeans Frontend (#12)
1 parent e2aa70c commit 706da5d

10 files changed

Lines changed: 212 additions & 2 deletions

File tree

.ci/Jenkinsfile

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env groovy
2+
@Library('apm@current') _
3+
4+
pipeline {
5+
agent { label 'linux && immutable' }
6+
environment {
7+
BASE_DIR = 'src/github.com/elastic/opbeans-frontend'
8+
NOTIFY_TO = credentials('notify-to')
9+
JOB_GCS_BUCKET = credentials('gcs-bucket')
10+
JOB_GCS_CREDENTIALS = 'apm-ci-gcs-plugin'
11+
DOCKERHUB_SECRET = 'secret/apm-team/ci/elastic-observability-dockerhub'
12+
PIPELINE_LOG_LEVEL = 'INFO'
13+
}
14+
options {
15+
timeout(time: 1, unit: 'HOURS')
16+
buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '20', daysToKeepStr: '30'))
17+
timestamps()
18+
ansiColor('xterm')
19+
disableResume()
20+
durabilityHint('PERFORMANCE_OPTIMIZED')
21+
rateLimitBuilds(throttle: [count: 60, durationName: 'hour', userBoost: true])
22+
quietPeriod(10)
23+
}
24+
parameters {
25+
booleanParam(name: 'Run_As_Master_Branch', defaultValue: false, description: 'Allow to run any steps on a PR, some steps normally only run on master branch.')
26+
}
27+
triggers {
28+
issueCommentTrigger('(?i).*(?:jenkins\\W+)?run\\W+(?:the\\W+)?tests(?:\\W+please)?.*')
29+
}
30+
stages {
31+
stage('Initializing'){
32+
agent { label 'linux && immutable' }
33+
options { skipDefaultCheckout() }
34+
environment {
35+
PATH = "${env.PATH}:${env.WORKSPACE}/bin"
36+
HOME = "${env.WORKSPACE}"
37+
}
38+
stages {
39+
/**
40+
Checkout the code and stash it, to use it on other stages.
41+
*/
42+
stage('Checkout') {
43+
steps {
44+
deleteDir()
45+
gitCheckout(basedir: "${BASE_DIR}")
46+
stash allowEmpty: true, name: 'source', useDefaultExcludes: false
47+
}
48+
}
49+
/**
50+
Build the project from code..
51+
*/
52+
stage('Build') {
53+
steps {
54+
deleteDir()
55+
unstash 'source'
56+
dir("${BASE_DIR}"){
57+
sh 'make build'
58+
}
59+
}
60+
}
61+
/**
62+
Execute unit tests.
63+
*/
64+
stage('Test') {
65+
steps {
66+
deleteDir()
67+
unstash 'source'
68+
dir("${BASE_DIR}"){
69+
sh "make test"
70+
}
71+
}
72+
post {
73+
always {
74+
junit(allowEmptyResults: true,
75+
keepLongStdio: true,
76+
testResults: "${BASE_DIR}/**/junit-*.xml")
77+
}
78+
}
79+
}
80+
stage('Release') {
81+
input {
82+
message 'Should we release a new version?'
83+
ok 'Yes, we should.'
84+
parameters {
85+
string(name: 'VERSION', defaultValue: 'latest', description: 'What tag?')
86+
}
87+
}
88+
when {
89+
beforeInput true
90+
beforeAgent true
91+
allOf {
92+
anyOf {
93+
branch 'master'
94+
branch "\\d+\\.\\d+"
95+
branch "v\\d?"
96+
tag "v\\d+\\.\\d+\\.\\d+*"
97+
expression { return params.Run_As_Master_Branch }
98+
}
99+
}
100+
}
101+
steps {
102+
deleteDir()
103+
unstash 'source'
104+
dir("${BASE_DIR}"){
105+
dockerLogin(secret: "${DOCKERHUB_SECRET}", registry: 'docker.io')
106+
sh "VERSION=${VERSION} make publish"
107+
}
108+
}
109+
}
110+
}
111+
}
112+
}
113+
post {
114+
always {
115+
notifyBuildResult()
116+
}
117+
}
118+
}

.dockerignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
.vscode
44
node_modules
55
package-lock.json
6-
Dockerfile
6+
Dockerfile
7+
bats/
8+
target/
9+
tests/

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ elastic-apm-rum
2020
#lockfiles
2121
yarn.lock
2222
package-lock.json
23+
24+
# For the BATS testing
25+
bats/
26+
target/

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "tests/test_helper/bats-assert"]
2+
path = tests/test_helper/bats-assert
3+
url = https://github.com/ztombol/bats-assert
4+
[submodule "tests/test_helper/bats-support"]
5+
path = tests/test_helper/bats-support
6+
url = https://github.com/ztombol/bats-support

Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
VERSION ?= latest
2+
LTS_ALPINE ?= 12-alpine
3+
4+
.PHONY: help
5+
.DEFAULT_GOAL := help
6+
7+
help: ## Display this help text
8+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
9+
10+
all: build test
11+
12+
build: ## Build docker image
13+
@docker build --file Dockerfile --tag=opbeans/opbeans-frontend:${VERSION} .
14+
15+
bats: ## Install bats in the project itself
16+
@git clone https://github.com/sstephenson/bats.git
17+
18+
prepare-test: bats ## Prepare the bats dependencies
19+
@docker pull node:${LTS_ALPINE}
20+
@mkdir -p target
21+
@git submodule sync
22+
@git submodule update --init --recursive
23+
24+
test: prepare-test ## Run the tests
25+
@echo "Tests are in progress, please be patient"
26+
@bats/bin/bats --tap tests | tee target/results.tap
27+
@docker run --rm -v "${PWD}":/usr/src/app -w /usr/src/app node:${LTS_ALPINE} \
28+
sh -c "npm install tap-xunit -g && cat target/results.tap | tap-xunit --package='co.elastic.opbeans' > target/junit-results.xml"
29+
30+
publish: build ## Publish docker image
31+
@docker push opbeans/opbeans-frontend:${VERSION}
32+
33+
clean: ## Clean autogenerated files/folders
34+
@rm -rf bats
35+
@rm -rf target

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Build Status](https://apm-ci.elastic.co/job/apm-agent-nodejs/job/opbeans-frontend-mbp/job/master/badge/icon)](https://apm-ci.elastic.co/job/apm-agent-nodejs/job/opbeans-frontend-mbp/job/master/)
2+
13
This project was bootstrapped with [Create React App](https://github.com/facebookincubator/create-react-app).
24

35
Below you will find some information on how to perform common tasks.<br>
@@ -911,7 +913,7 @@ This feature is experimental and still [has major usage issues](https://github.c
911913
912914
### Editor Integration
913915
914-
If you use [Visual Studio Code](https://code.visualstudio.com), there is a [Jest extension](https://github.com/orta/vscode-jest) which works with Create React App out of the box. This provides a lot of IDE-like features while using a text editor: showing the status of a test run with potential fail messages inline, starting and stopping the watcher automatically, and offering one-click snapshot updates.
916+
If you use [Visual Studio Code](https://code.visualstudio.com), there is a [Jest extension](https://github.com/orta/vscode-jest) which works with Create React App out of the box. This provides a lot of IDE-like features while using a text editor: showing the status of a test run with potential fail messages inline, starting and stopping the watcher automatically, and offering one-click snapshot updates.
915917
916918
![VS Code Jest Preview](https://cloud.githubusercontent.com/assets/49038/20795349/a032308a-b7c8-11e6-9b34-7eeac781003f.png)
917919
@@ -1242,3 +1244,23 @@ It is reported that `npm run build` can fail on machines with no swap space, whi
12421244
## Something Missing?
12431245
12441246
If you have ideas for more “How To” recipes that should be on this page, [let us know](https://github.com/facebookincubator/create-react-app/issues) or [contribute some!](https://github.com/facebookincubator/create-react-app/edit/master/packages/react-scripts/template/README.md)
1247+
1248+
## Testing locally
1249+
1250+
The simplest way to test this demo is by running:
1251+
1252+
```bash
1253+
make test
1254+
```
1255+
1256+
Tests are written using [bats](https://github.com/sstephenson/bats) under the tests dir
1257+
1258+
## Publishing to dockerhub locally
1259+
1260+
Publish the docker image with
1261+
1262+
```bash
1263+
VERSION=1.2.3 make publish
1264+
```
1265+
1266+
NOTE: VERSION refers to the tag for the docker image which will be published in the registry

tests/test_helper/bats-assert

Submodule bats-assert added at 9f88b42

tests/test_helper/bats-support

Submodule bats-support added at 004e707

tests/test_helpers.bash

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bats
2+
3+
# check dependencies
4+
(
5+
type docker &>/dev/null || ( echo "docker is not available"; exit 1 )
6+
type curl &>/dev/null || ( echo "curl is not available"; exit 1 )
7+
)>&2

tests/tests.bats

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bats
2+
3+
load 'test_helper/bats-support/load'
4+
load 'test_helper/bats-assert/load'
5+
load test_helpers
6+
7+
IMAGE="bats-opbeans"
8+
CONTAINER="bats-opbeans"
9+
10+
@test "build image" {
11+
cd $BATS_TEST_DIRNAME/..
12+
docker build -t $IMAGE .
13+
}

0 commit comments

Comments
 (0)