From b9206c72cdd3703e61b65d52c3dc9cfc07bb5ac4 Mon Sep 17 00:00:00 2001 From: Suraj Keshri Date: Mon, 2 Mar 2020 06:29:16 -0500 Subject: [PATCH] updates --- .dockerignore | 3 +++ .eslintrc | 2 +- Dockerfile | 8 +++++++ canIDeploy.js | 56 +++++++++++++++++++++++++++++----------------- canIDeploy.test.ts | 19 +++++++++++----- createHooks.js | 10 +++++---- index.js | 15 ++++++++++++- package-lock.json | 10 +++++++++ package.json | 9 +++++++- readme.md | 36 +++++++++++++++++++++++++++++ 10 files changed, 136 insertions(+), 32 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 readme.md diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f56b913 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.git +node_modules/ +packages/ diff --git a/.eslintrc b/.eslintrc index 0a6f0af..9b55843 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,7 +1,7 @@ { "extends": "eslint:recommended", "parserOptions": { - "ecmaVersion": 2017 + "ecmaVersion": 2018 }, "env": { "node": true, diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8140a26 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM node:13.8.0-stretch +WORKDIR /app +COPY package*.json ./ + +RUN npm ci + +COPY . . +ENTRYPOINT ["node", "index.js"] diff --git a/canIDeploy.js b/canIDeploy.js index a11c223..19d6602 100644 --- a/canIDeploy.js +++ b/canIDeploy.js @@ -6,40 +6,56 @@ const yaml = require("js-yaml"); const _ = require("lodash"); const pact = require("@pact-foundation/pact-node"); -exports.canIDeploy = function({ - kustomizeFile, - retryTimes, - retryInterval, - brokerUrl, - brokerToken - // verbose -}) { +function getPacticipants({ kustomizeFile, serviceNames }) { const yamlFile = path.resolve(kustomizeFile); const doc = yaml.safeLoad(fs.readFileSync(yamlFile, "utf8")); const imageTagPairs = _.fromPairs( - doc.images.map(a => [getPacticipant(a.name), a.newTag]) + doc.images + .filter(a => serviceNames.indexOf(_.last(a.name.split("/"))) > -1) + .map(a => [getPacticipant(a.name), a.newTag]) ); const pacticipants = Object.entries(imageTagPairs).map(([name, version]) => ({ name, version })); + return pacticipants; +} - const canDeployOptions = { - pacticipants, +exports.getPacticipants = getPacticipants; + +exports.canIDeploy = function({ + kustomizeFile, + retryTimes, + retryInterval, + brokerUrl, + brokerToken, + consumer, + providers + // verbose +}) { + const defaultOptions = { pactBroker: brokerUrl || process.env.PACT_BROKER_URL, pactBrokerToken: brokerToken || process.env.PACT_BROKER_TOKEN, retryWhileUnknown: retryTimes || 5, - retryInterval: retryInterval || 30 + retryInterval: retryInterval || 30, + output: "table" }; - - // console.log(!!verbose); - - return pact - .canDeploy(canDeployOptions) - .then(console.log) - .catch(e => { - throw e; + const promises = providers.map(provider => { + const pacticipants = getPacticipants({ + kustomizeFile, + serviceNames: [consumer, provider] }); + const canDeployOptions = { pacticipants, ...defaultOptions }; + return pact.canDeploy(canDeployOptions).catch( + async () => + await pact.canDeploy({ + ...canDeployOptions, + retryWhileUnknown: 0, + retryInterval: 0 + }) + ); + }); + return Promise.all(promises); }; diff --git a/canIDeploy.test.ts b/canIDeploy.test.ts index 1574cc4..25c0163 100644 --- a/canIDeploy.test.ts +++ b/canIDeploy.test.ts @@ -1,17 +1,26 @@ const path = require("path"); -const { canIDeploy } = require("./canIDeploy"); +const { canIDeploy, getPacticipants } = require("./canIDeploy"); jest.setTimeout(60000); describe("canIDeploy", () => { - test("canIDeploy", async () => { + test("canIDeploy pacticipant test", async () => { const filePath = path.resolve( __dirname, "./__tests__/deploy-kustomization.yaml" ); - await canIDeploy({ + let result = getPacticipants({ kustomizeFile: filePath, - retryTimes: 1, - retryInterval: 10 + serviceNames: ["frontend", "gateway"] }); + expect(result.map(a => a.name)).toEqual( + expect.arrayContaining(["Frontend", "Gateway"]) + ); + result = getPacticipants({ + kustomizeFile: filePath, + serviceNames: ["frontend", "gateway", "auth-service"] + }); + expect(result.map(a => a.name)).toEqual( + expect.arrayContaining(["Frontend", "Gateway", "AuthService"]) + ); }); }); diff --git a/createHooks.js b/createHooks.js index 1439864..50dff72 100644 --- a/createHooks.js +++ b/createHooks.js @@ -6,15 +6,16 @@ const util = require("util"); const exec = util.promisify(require("child_process").exec); const JENKINS_USERNAME = process.env.JENKINS_USERNAME; -const JENKINS_USERTOKEN = process.env.JENKINS_USERTOKEN; +const JENKINS_USERTOKEN = process.env.JENKINS_API_TOKEN; const JENKINS_HTTPS_URL = process.env.JENKINS_HTTPS_URL; const PACT_BROKER_TOKEN = process.env.PACT_BROKER_TOKEN; const PACT_BROKER_URL = process.env.PACT_BROKER_URL; +const JENKINS_CRUMB = process.env.JENKINS_CRUMB; const providerMap = { "auth-service": ["group-service"], - frontend: ["gateway"], - gateway: ["auth-service", "group-service"] + frontend: ["auth-service", "group-service", "file-service"] + // gateway: ["auth-service", "group-service"] }; async function createWebhook(consumer, provider) { @@ -22,10 +23,11 @@ async function createWebhook(consumer, provider) { pact-broker create-webhook \ ${JENKINS_HTTPS_URL}/job/${provider}/job/master/buildWithParameters?PACT_VERIFY=true \ --request=POST \ + --header=Jenkins-Crumb:${JENKINS_CRUMB} \ --contract-content-changed \ --consumer=${getPacticipant(consumer)} \ --provider=${getPacticipant(provider)} \ - --user=${JENKINS_USERNAME}:${JENKINS_USERTOKEN} \ + --user="${JENKINS_USERNAME}:${JENKINS_USERTOKEN}" \ --broker-base-url="${PACT_BROKER_URL}" \ --broker-token="${PACT_BROKER_TOKEN}" `); diff --git a/index.js b/index.js index a5fccef..c08560c 100644 --- a/index.js +++ b/index.js @@ -50,10 +50,21 @@ yargs return yargs .option("brokerToken", { alias: "t", - type: "strring", + type: "string", describe: "broker auth token" }) .demand(["brokerToken"]) + .option("consumer", { + type: "string", + describe: "name of the consumer" + }) + .demand(["consumer"]) + .option("providers", { + type: "array", + describe: "providers on which the consumer depends" + }) + .demand(["providers"]) + .check(argv => argv.providers.length > 0, false) .option("brokerUrl", { alias: "b", type: "string", @@ -77,6 +88,8 @@ yargs argv => { return canIDeploy({ kustomizeFile: argv.filepath, + consumer: argv.consumer, + providers: argv.providers, retryInterval: argv.retryInterval, retryTimes: argv.retryTimes, brokerToken: argv.brokerToken, diff --git a/package-lock.json b/package-lock.json index a52118f..7299930 100644 --- a/package-lock.json +++ b/package-lock.json @@ -758,6 +758,16 @@ "@types/istanbul-lib-report": "*" } }, + "@types/jest": { + "version": "25.1.3", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-25.1.3.tgz", + "integrity": "sha512-jqargqzyJWgWAJCXX96LBGR/Ei7wQcZBvRv0PLEu9ZByMfcs23keUJrKv9FMR6YZf9YCbfqDqgmY+JUBsnqhrg==", + "dev": true, + "requires": { + "jest-diff": "^25.1.0", + "pretty-format": "^25.1.0" + } + }, "@types/node": { "version": "13.7.4", "resolved": "https://registry.npmjs.org/@types/node/-/node-13.7.4.tgz", diff --git a/package.json b/package.json index b2955ed..8be129b 100644 --- a/package.json +++ b/package.json @@ -3,12 +3,18 @@ "version": "0.1.0", "description": "", "main": "index.js", + "bin": "index.js", "scripts": { "pretest": "KUBECONFIG=./config.yaml kind create cluster", "test": "jest || :", "test:watch": "jest --watch || :", "posttest": "rm ./config.yaml && KUBECONFIG=./config.yaml kind delete cluster", - "package": "pkg -t node10-macos-x64,node10-linux-x64 -o=packages/utils index.js" + "package": "pkg -t node10-macos-x64,node10-linux-x64 -o=packages/utils ." + }, + "pkg": { + "assets": [ + "node_modules/@pact-foundation/pact-node/standalone/**/*" + ] }, "keywords": [], "author": "Suraj Keshri ", @@ -24,6 +30,7 @@ "yargs": "^15.1.0" }, "devDependencies": { + "@types/jest": "^25.1.3", "eslint": "^6.8.0", "jest": "^25.1.0", "jest-watch-typeahead": "^0.4.2", diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..23bac1d --- /dev/null +++ b/readme.md @@ -0,0 +1,36 @@ +### To trigger remote build of a pipeline with trigger parameters: + +1. Define parameters in the pipeline project: + +```groovy +pipeline { + agent any + parameters { + string(name: 'TRIGGER_PARAM', defaultValue: '', description: 'trigger param description') + } +} +``` + +2. create token from here: http://myjenkins:8080/user/surajkeshri/configure + +``` +http://[USERNAME]:[TOKEN]@myjenkins:8080/job/[pipeline-project-name]/job/[branch]/buildWithParameters?TRIGGER_PARAM=yo +``` + +### To trigger remote build of a pipeline without params: + +1. Step 1 without paramters +2. Same as step 2 +3. Trigger: + +``` +http://[USERNAME]:[TOKEN]@myjenkins:8080/job/[pipeline-project-name]/job/[branch]/build +``` + +pact-broker create-webhook http://myjenkins:8080/job/auth-service/job/master/buildWithParameters?PACT_VERIFY=true --request=POST --broker-base-url=\${BROKER_BASE_URL} + +pact-broker create-webhook \ +http://myjenkins:8080/job/test/buildWithParameters?PACT_VERIFY=true \ +--request=POST --contract-content-changed \ +--provider=AuthService --user=surajkeshri:11d68c6558003716571dd8f4a9b544b76b \ +--broker-base-url=$PACT_BROKER_URL --broker-token=$PACT_BROKER_TOKEN