Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
suraj-keshri committed Mar 2, 2020
1 parent d2f0a30 commit b9206c7
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.git
node_modules/
packages/
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2017
"ecmaVersion": 2018
},
"env": {
"node": true,
Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:13.8.0-stretch
WORKDIR /app
COPY package*.json ./

RUN npm ci

COPY . .
ENTRYPOINT ["node", "index.js"]
56 changes: 36 additions & 20 deletions canIDeploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
19 changes: 14 additions & 5 deletions canIDeploy.test.ts
Original file line number Diff line number Diff line change
@@ -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"])
);
});
});
10 changes: 6 additions & 4 deletions createHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@ 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) {
const { stdout, stderr } = await exec(`
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}"
`);
Expand Down
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
Expand All @@ -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",
Expand Down
36 changes: 36 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b9206c7

Please sign in to comment.