Skip to content

Commit

Permalink
Merge pull request #8 from pactflow/record_deployment
Browse files Browse the repository at this point in the history
Record deployment
  • Loading branch information
YOU54F authored Aug 15, 2022
2 parents a488b05 + 1dcd19f commit 87b7d14
Show file tree
Hide file tree
Showing 9 changed files with 2,134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions record-deployment/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock.json
node_modules/
21 changes: 21 additions & 0 deletions record-deployment/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Yousaf Nabi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 23 additions & 0 deletions record-deployment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# record-deployment action

Record deployment of an application to an environment

## Example

```yml
# (This just saves defining these multiple times for different pact jobs)
env:
application_name: "my-consumer-app"
pact_broker: ${{ secrets.PACT_BROKER }}
pact_broker_token: ${{ secrets.PACT_BROKER_TOKEN }}

jobs:
pact-record-deployment:
needs: can-i-deploy
runs-on: ubuntu-latest
steps:
- uses: pactflow/actions/[email protected]
env:
version: "1.0.1"
environment: "test"
```
23 changes: 23 additions & 0 deletions record-deployment/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: "record-deployment"
description: "Record deployment of an application to an environment"
branding:
icon: "check"
color: "green"
inputs:
pact_broker:
description: "The url of your pact broker"
required: true
pact_broker_token:
description: "Your pact broker token"
required: true
application_name:
description: "The name of your application (usually project name)"
required: true
version:
description: "The version of your application (defaults to latest)"
required: true
runs:
using: "composite"
steps:
- run: ${{ github.action_path }}/recordDeployment.sh
shell: bash
20 changes: 20 additions & 0 deletions record-deployment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "record-deployment",
"version": "1.0.0",
"description": "Record deployment of an application to an environment",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test:unit": "./node_modules/mocha/bin/mocha --recursive test/unit",
"test:unit:watch": "./node_modules/mocha/bin/mocha -w --recursive test/unit",
"lint": "standard ."
},
"author": "",
"license": "ISC",
"dependencies": {
"mocha": "^9.2.2",
"standard": "^16.0.4"
}
}
30 changes: 30 additions & 0 deletions record-deployment/recordDeployment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

MISSING=()
[ ! "$pact_broker" ] && MISSING+=("pact_broker")
[ ! "$pact_broker_token" ] && MISSING+=("pact_broker_token")
[ ! "$application_name" ] && MISSING+=("application_name")
[ ! "$version" ] && MISSING+=("version")
[ ! "$environment" ] && MISSING+=("environment")

if [ ${#MISSING[@]} -gt 0 ]; then
echo "ERROR: The following environment variables are not set:"
printf '\t%s\n' "${MISSING[@]}"
exit 1
fi

echo "
pact_broker: '$pact_broker'
pact_broker_token: '$pact_broker_token'
application_name: '$application_name'
version: '$version'
environment: '$environment'"

docker run --rm \
-e PACT_BROKER_BASE_URL=$pact_broker \
-e PACT_BROKER_TOKEN=$pact_broker_token \
pactfoundation/pact-cli:latest \
broker record-deployment \
--pacticipant "$application_name" \
--version $version \
--environment $environment
11 changes: 11 additions & 0 deletions record-deployment/test/unit/docker-mock.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# Using 'bash' to mock 'docker' so it doesn't execute during test.

echo ">>>> 0.1"
COMMAND=${1:?"Syntax $0 <command>"}
echo ">>>> 0.2"
shopt -s expand_aliases
echo ">>>> 0.3"
alias docker="echo docker"
echo ">>>> 0.4"
source $COMMAND
82 changes: 82 additions & 0 deletions record-deployment/test/unit/recordDeployment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const { describe, it } = require("mocha");
const assert = require("assert");
const { spawnSync } = require("child_process");

const dockerMock = "./test/unit/docker-mock.sh";
const scriptTotest = "./recordDeployment.sh";

const mandatoryVars = {
pact_broker: "pact_broker-set",
pact_broker_token: "pact_broker_token-set",
application_name: "application_name-set",
version: "version-set",
environment: "environment-set",
};

// Examines the generated docker call to check each element is in place when called with `mandatoryVars`.
const dockerCallParameters = [
['calls docker with "run --rm"', /docker run --rm/],
[
"sets PACT_BROKER_BASE_URL",
new RegExp(
`docker .* -e PACT_BROKER_BASE_URL=${mandatoryVars.pact_broker}`
),
],
[
"sets PACT_BROKER_TOKEN",
new RegExp(
`docker .* -e PACT_BROKER_TOKEN=${mandatoryVars.pact_broker_token}`
),
],
["uses latest pact-cli", /docker .* -e .*pactfoundation\/pact-cli:latest/],
["uses record-deployment", /pact-cli.*broker record-deployment/],
[
"sets the participant",
new RegExp(
`record-deployment.*--pacticipant ${mandatoryVars.application_name}`
),
],
[
"sets environment",
new RegExp(
`record-deployment.* --environment ${mandatoryVars.environment}`
),
],
[
"sets version",
new RegExp(`record-deployment.* --version ${mandatoryVars.version}`),
],
];

// Runs the script we're testing, passing params etc....
const spawnScript = (env = mandatoryVars) =>
spawnSync(dockerMock, [scriptTotest], { env, shell: true });

describe("recordDeployment", () => {
describe("docker command", () =>
dockerCallParameters.forEach(([description, matcher]) =>
it(description, () => {
const result = spawnScript();

assert.match(result.stdout.toString(), matcher);
})
));

describe("mandatory variables", () =>
Object.keys(mandatoryVars).forEach((testVar) => {
it(`fails if mandatory variable '${testVar} 'is not set`, () => {
// Deep clone, so the 'delete' doesn't affect the original.
const env = JSON.parse(JSON.stringify(mandatoryVars));
delete env[testVar];
const result = spawnScript(env);

assert.strictEqual(result.status, 1);
});
}));

it("does not fail if all variables are set", () => {
const result = spawnScript();

assert.strictEqual(result.status, 0);
});
});
Loading

0 comments on commit 87b7d14

Please sign in to comment.