Skip to content

Commit 697b594

Browse files
authored
Add make.sh workflows (#1696)
1 parent 911af98 commit 697b594

File tree

5 files changed

+311
-4
lines changed

5 files changed

+311
-4
lines changed

.ci/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ARG NODE_JS_VERSION=10
1+
ARG NODE_JS_VERSION=16
22
FROM node:${NODE_JS_VERSION}
33

44
# Create app directory

.ci/make.mjs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/* global $ argv */
21+
22+
'use strict'
23+
24+
import 'zx/globals'
25+
26+
import { readFile, writeFile } from 'fs/promises'
27+
import assert from 'assert'
28+
import { join } from 'desm'
29+
import semver from 'semver'
30+
31+
assert(typeof argv.task === 'string', 'Missing task parameter')
32+
33+
switch (argv.task) {
34+
case 'release':
35+
release(argv._).catch(onError)
36+
break
37+
case 'bump':
38+
bump(argv._).catch(onError)
39+
break
40+
case 'codegen':
41+
codegen(argv._).catch(onError)
42+
break
43+
default:
44+
console.log(`Unknown task: ${argv.task}`)
45+
process.exit(1)
46+
}
47+
48+
async function release (args) {
49+
assert(args.length === 2, 'Release task expects two parameters')
50+
let [version, outputFolder] = args
51+
52+
if (process.env.WORKFLOW === 'snapshot' && !version.endsWith('SNAPSHOT')) {
53+
version = `${version}-SNAPSHOT`
54+
}
55+
56+
await bump([version])
57+
58+
const packageJson = JSON.parse(await readFile(
59+
join(import.meta.url, '..', 'package.json'),
60+
'utf8'
61+
))
62+
63+
await $`npm run build`
64+
await $`npm pack`
65+
await $`zip elasticsearch-js-${version}.zip elastic-elasticsearch-${packageJson.version}.tgz`
66+
await $`rm elastic-elasticsearch-${packageJson.version}.tgz`
67+
await $`mv ${join(import.meta.url, '..', `elasticsearch-js-${version}.zip`)} ${join(import.meta.url, '..', outputFolder, `elasticsearch-js-${version}.zip`)}`
68+
}
69+
70+
async function bump (args) {
71+
assert(args.length === 1, 'Bump task expects one parameter')
72+
const [version] = args
73+
const packageJson = JSON.parse(await readFile(
74+
join(import.meta.url, '..', 'package.json'),
75+
'utf8'
76+
))
77+
78+
const cleanVersion = semver.clean(version.includes('SNAPSHOT') ? version.split('-')[0] : version)
79+
assert(semver.valid(cleanVersion))
80+
packageJson.version = cleanVersion
81+
packageJson.versionCanary = `${cleanVersion}-canary.0`
82+
83+
await writeFile(
84+
join(import.meta.url, '..', 'package.json'),
85+
JSON.stringify(packageJson, null, 2),
86+
'utf8'
87+
)
88+
89+
const testMatrix = await readFile(join(import.meta.url, 'test-matrix.yml'), 'utf8')
90+
await writeFile(
91+
join(import.meta.url, 'test-matrix.yml'),
92+
testMatrix.replace(/STACK_VERSION:\s+\- "[0-9]+[0-9\.]*[0-9](?:\-SNAPSHOT)?"/, `STACK_VERSION:\n - "${cleanVersion}-SNAPSHOT"`), // eslint-disable-line
93+
'utf8'
94+
)
95+
}
96+
97+
// this command can only be executed locally for now
98+
async function codegen (args) {
99+
assert(args.length === 1, 'Bump task expects one parameter')
100+
const clientGeneratorPath = join(import.meta.url, '..', '..', 'elastic-client-generator-js')
101+
const [version] = args
102+
103+
const isGeneratorCloned = await $`[[ -d ${clientGeneratorPath} ]]`.exitCode === 0
104+
assert(isGeneratorCloned, 'You must clone the elastic-client-generator-js first')
105+
106+
await $`npm install --prefix ${clientGeneratorPath}`
107+
// this command will take a while!
108+
if (version === 'main') {
109+
await $`npm run elasticsearch --prefix ${clientGeneratorPath} -- --version main`
110+
} else {
111+
await $`npm run elasticsearch --prefix ${clientGeneratorPath} -- --version ${version.split('.').slice(0, 2).join('.')}`
112+
}
113+
await $`npm run lint --prefix ${clientGeneratorPath}`
114+
115+
await $`rm -rf ${join(import.meta.url, '..', 'src', 'api')}`
116+
await $`mkdir ${join(import.meta.url, '..', 'src', 'api')}`
117+
await $`cp -R ${join(import.meta.url, '..', '..', 'elastic-client-generator-js', 'output')}/* ${join(import.meta.url, '..', 'src', 'api')}`
118+
await $`mv ${join(import.meta.url, '..', 'src', 'api', 'reference.asciidoc')} ${join(import.meta.url, '..', 'docs', 'reference.asciidoc')}`
119+
await $`npm run build`
120+
}
121+
122+
function onError (err) {
123+
console.log(err)
124+
process.exit(1)
125+
}

.ci/make.sh

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
#!/usr/bin/env bash
2+
3+
# ------------------------------------------------------- #
4+
#
5+
# Skeleton for common build entry script for all elastic
6+
# clients. Needs to be adapted to individual client usage.
7+
#
8+
# Must be called: ./.ci/make.sh <target> <params>
9+
#
10+
# Version: 1.1.0
11+
#
12+
# Targets:
13+
# ---------------------------
14+
# assemble <VERSION> : build client artefacts with version
15+
# bump <VERSION> : bump client internals to version
16+
# codegen <VERSION> : generate endpoints
17+
# docsgen <VERSION> : generate documentation
18+
# examplegen : generate the doc examples
19+
# clean : clean workspace
20+
#
21+
# ------------------------------------------------------- #
22+
23+
# ------------------------------------------------------- #
24+
# Bootstrap
25+
# ------------------------------------------------------- #
26+
27+
script_path=$(dirname "$(realpath -s "$0")")
28+
repo=$(realpath "$script_path/../")
29+
generator=$(realpath "$script_path/../../elastic-client-generator-js")
30+
31+
# shellcheck disable=SC1090
32+
CMD=$1
33+
TASK=$1
34+
TASK_ARGS=()
35+
VERSION=$2
36+
STACK_VERSION=$VERSION
37+
NODE_JS_VERSION=16
38+
WORKFLOW=${WORKFLOW-staging}
39+
set -euo pipefail
40+
41+
product="elastic/elasticsearch-js"
42+
output_folder=".ci/output"
43+
OUTPUT_DIR="$repo/${output_folder}"
44+
REPO_BINDING="${OUTPUT_DIR}:/sln/${output_folder}"
45+
mkdir -p "$OUTPUT_DIR"
46+
47+
echo -e "\033[34;1mINFO:\033[0m PRODUCT ${product}\033[0m"
48+
echo -e "\033[34;1mINFO:\033[0m VERSION ${STACK_VERSION}\033[0m"
49+
echo -e "\033[34;1mINFO:\033[0m OUTPUT_DIR ${OUTPUT_DIR}\033[0m"
50+
51+
# ------------------------------------------------------- #
52+
# Parse Command
53+
# ------------------------------------------------------- #
54+
55+
case $CMD in
56+
clean)
57+
echo -e "\033[36;1mTARGET: clean workspace $output_folder\033[0m"
58+
rm -rf "$output_folder"
59+
echo -e "\033[32;1mdone.\033[0m"
60+
exit 0
61+
;;
62+
assemble)
63+
if [ -v $VERSION ]; then
64+
echo -e "\033[31;1mTARGET: assemble -> missing version parameter\033[0m"
65+
exit 1
66+
fi
67+
echo -e "\033[36;1mTARGET: assemble artefact $VERSION\033[0m"
68+
TASK=release
69+
TASK_ARGS=("$VERSION" "$output_folder")
70+
;;
71+
codegen)
72+
if [ -v $VERSION ]; then
73+
echo -e "\033[31;1mTARGET: codegen -> missing version parameter\033[0m"
74+
exit 1
75+
fi
76+
echo -e "\033[36;1mTARGET: codegen API v$VERSION\033[0m"
77+
TASK=codegen
78+
# VERSION is BRANCH here for now
79+
TASK_ARGS=("$VERSION")
80+
;;
81+
docsgen)
82+
if [ -v $VERSION ]; then
83+
echo -e "\033[31;1mTARGET: docsgen -> missing version parameter\033[0m"
84+
exit 1
85+
fi
86+
echo -e "\033[36;1mTARGET: generate docs for $VERSION\033[0m"
87+
TASK=codegen
88+
# VERSION is BRANCH here for now
89+
TASK_ARGS=("$VERSION" "$codegen_folder")
90+
;;
91+
examplesgen)
92+
echo -e "\033[36;1mTARGET: generate examples\033[0m"
93+
TASK=codegen
94+
# VERSION is BRANCH here for now
95+
TASK_ARGS=("$VERSION" "$codegen_folder")
96+
;;
97+
bump)
98+
if [ -v $VERSION ]; then
99+
echo -e "\033[31;1mTARGET: bump -> missing version parameter\033[0m"
100+
exit 1
101+
fi
102+
echo -e "\033[36;1mTARGET: bump to version $VERSION\033[0m"
103+
TASK=bump
104+
# VERSION is BRANCH here for now
105+
TASK_ARGS=("$VERSION")
106+
;;
107+
*)
108+
echo -e "\nUsage:\n\t $CMD is not supported right now\n"
109+
exit 1
110+
esac
111+
112+
113+
# ------------------------------------------------------- #
114+
# Build Container
115+
# ------------------------------------------------------- #
116+
117+
echo -e "\033[34;1mINFO: building $product container\033[0m"
118+
119+
docker build \
120+
--file .ci/Dockerfile \
121+
--tag ${product} \
122+
--build-arg NODE_JS_VERSION=${NODE_JS_VERSION} \
123+
--build-arg USER_ID="$(id -u)" \
124+
--build-arg GROUP_ID="$(id -g)" \
125+
.
126+
127+
# ------------------------------------------------------- #
128+
# Run the Container
129+
# ------------------------------------------------------- #
130+
131+
echo -e "\033[34;1mINFO: running $product container\033[0m"
132+
133+
docker run \
134+
--volume $repo:/usr/src/app \
135+
--volume $generator:/usr/src/elastic-client-generator-js \
136+
--volume /usr/src/app/node_modules \
137+
--env "WORKFLOW=${WORKFLOW}" \
138+
--name make-elasticsearch-js \
139+
--rm \
140+
$product \
141+
node .ci/make.mjs --task $TASK ${TASK_ARGS[*]}
142+
143+
# ------------------------------------------------------- #
144+
# Post Command tasks & checks
145+
# ------------------------------------------------------- #
146+
147+
if [[ "$CMD" == "assemble" ]]; then
148+
if compgen -G ".ci/output/*" > /dev/null; then
149+
echo -e "\033[32;1mTARGET: successfully assembled client v$VERSION\033[0m"
150+
else
151+
echo -e "\033[31;1mTARGET: assemble failed, empty workspace!\033[0m"
152+
exit 1
153+
fi
154+
fi
155+
156+
if [[ "$CMD" == "bump" ]]; then
157+
if [ -n "$(git status --porcelain)" ]; then
158+
echo -e "\033[32;1mTARGET: successfully bumped client v$VERSION\033[0m"
159+
else
160+
echo -e "\033[31;1mTARGET: failed bumped client v$VERSION\033[0m"
161+
exit 1
162+
fi
163+
fi
164+
165+
if [[ "$CMD" == "codegen" ]]; then
166+
if [ -n "$(git status --porcelain)" ]; then
167+
echo -e "\033[32;1mTARGET: successfully generated client v$VERSION\033[0m"
168+
else
169+
echo -e "\033[31;1mTARGET: failed generating client v$VERSION\033[0m"
170+
exit 1
171+
fi
172+
fi
173+
174+
if [[ "$CMD" == "docsgen" ]]; then
175+
echo "TODO"
176+
fi
177+
178+
if [[ "$CMD" == "examplesgen" ]]; then
179+
echo "TODO"
180+
fi

.ci/test-matrix.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
STACK_VERSION:
3-
- 8.3.0-SNAPSHOT
3+
- "8.3.0-SNAPSHOT"
44

55
NODE_JS_VERSION:
66
- 18

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@types/stoppable": "^1.1.1",
5858
"@types/tap": "^15.0.7",
5959
"cross-zip": "^4.0.0",
60+
"desm": "^1.2.0",
6061
"fast-deep-equal": "^3.1.3",
6162
"into-stream": "^7.0.0",
6263
"js-yaml": "^4.1.0",
@@ -76,7 +77,8 @@
7677
"ts-standard": "^11.0.0",
7778
"typescript": "^4.6.4",
7879
"workq": "^3.0.0",
79-
"xmlbuilder2": "^3.0.2"
80+
"xmlbuilder2": "^3.0.2",
81+
"zx": "^6.1.0"
8082
},
8183
"dependencies": {
8284
"@elastic/transport": "^8.2.0",
@@ -89,4 +91,4 @@
8991
"coverage": false,
9092
"check-coverage": false
9193
}
92-
}
94+
}

0 commit comments

Comments
 (0)