Skip to content

Commit

Permalink
chore: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-krakowski committed Aug 8, 2023
0 parents commit 6ab3e7f
Show file tree
Hide file tree
Showing 25 changed files with 3,761 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PACT_BROKER_BASE_URL=https://****.pactflow.io
PACT_BROKER_TOKEN=1234567890
PACTICIPANT=pactflow-example-provider
PORT=4000
48 changes: 48 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: PR
on:
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
env:
PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
PACTICIPANT: 'backend'
OAS_PATH=: 'oas/swagger.yml'
REPORT_FILE_CONTENT_TYPE: 'text/plain'
VERIFIER_TOOL: 'tap'
REPORT_PATH: 'oas/swagger.yml'
VERSION: ${{ github.sha }}
GIT_BRANCH: ${{ github.ref_name }}
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: yarn install
- name: Run tests
run: make test
- name: Publish contracts
run: make publish_provider_contract
can_i_deploy:
runs-on: ubuntu-latest
needs: test
env:
PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
PACTICIPANT: 'backend'
VERSION: ${{ github.sha }}
GIT_BRANCH: ${{ github.ref_name }}
DEPLOYMENT_TARGET: 'production'
REPORT_PATH: 'oas/swagger.yml'
REPORT_FILE_CONTENT_TYPE: 'text/plain'
VERIFIER_TOOL: 'tap'
steps:
- uses: actions/checkout@v3
- name: Can I deploy?
run: make can_i_deploy

63 changes: 63 additions & 0 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Production
on:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
env:
PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
PACTICIPANT: 'backend'
OAS_PATH=: 'oas/swagger.yml'
REPORT_FILE_CONTENT_TYPE: 'text/plain'
VERIFIER_TOOL: 'tap'
REPORT_PATH: 'oas/swagger.yml'
VERSION: ${{ github.sha }}
GIT_BRANCH: ${{ github.ref_name }}
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: yarn install
- name: Run tests
run: make test
- name: Publish contracts
run: make publish_provider_contract
can_i_deploy:
runs-on: ubuntu-latest
needs: test
env:
PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
PACTICIPANT: 'backend'
VERSION: ${{ github.sha }}
GIT_BRANCH: ${{ github.ref_name }}
DEPLOYMENT_TARGET: 'production'
REPORT_PATH: 'oas/swagger.yml'
REPORT_FILE_CONTENT_TYPE: 'text/plain'
VERIFIER_TOOL: 'tap'
steps:
- uses: actions/checkout@v3
- name: Can I deploy?
run: make can_i_deploy
deployment:
runs-on: ubuntu-latest
needs: can_i_deploy
env:
PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
PACTICIPANT: 'backend'
VERSION: ${{ github.sha }}
GIT_BRANCH: ${{ github.ref_name }}
DEPLOYMENT_TARGET: 'production'
steps:
- uses: actions/checkout@v3
- name: Deploy
run: make deploy
- name: Record deployment
run: make record_deployment
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Logs
logs
*.log
npm-debug.log*

# nyc test coverage
.nyc_output

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules

# Optional npm cache directory
.npm

# mac files
.DS_Store

# vim swap files
*.swp

# webstorm
.idea

# vscode
.vscode
*code-workspace

# compiled app
dist

.env
5 changes: 5 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
after: utils/generateOas.ts
test-ignore: $.
test-regex: (\/|^)tests\/.*|\.test\.ts$
ts: true
coverage: false
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
ifneq (,$(wildcard ./.env))
include .env
export
endif

## ====================
## LOCAL DEPLOYMENT
## ====================

VERSION?= $(shell git rev-parse --short HEAD)
GIT_BRANCH?= $(shell git rev-parse --abbrev-ref HEAD)

OAS_PATH=oas/swagger.yml
REPORT_PATH?=$(shell ls oas/*)
REPORT_FILE_CONTENT_TYPE?=text/plain
VERIFIER_TOOL?=tap
DEPLOYMENT_TARGET?=production

ci: test can_i_deploy record_deployment

## ====================
## CI DEPLOYMENT
## ====================

install: yarn install

test:
@echo "========== STAGE: test 🧪 =========="
@echo "Running tests to test locally running provider"
@yarn run test

publish_provider_contract:
@echo "========== STAGE: publish-provider-contract =========="
docker-compose run publish_provider_contract

can_i_deploy:
@echo "========== STAGE: can-i-deploy? 🌉 =========="
docker-compose run can_i_deploy

deploy:
@echo "========== STAGE: deploy 🚀 =========="
@echo "Deploying to prod"

record_deployment:
docker-compose run record_deployment

.PHONY: start stop test
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[![Can I deploy Status](https://ermingo.pactflow.io/pacticipants/backend/branches/main/latest-version/can-i-deploy/to-environment/production/badge)](https://ermingo.pactflow.io/pacticipants/backend/branches/main/latest-version/can-i-deploy/to-environment/production/badge)

## Description
This backend application is part of a bi-directional contract testing example built using Fastify, Fastify CLI, `@fastify/swagger`, `zod` and `tap`. The application is the provider in our contract testing example.

## Features
- API Documentation using `@fastify/swagger` with schemas defined using Zod
- Testing using `tap`
- Bi-directional Contract testing using `Pact`
- CI/CD integration through `GitHub Actions`


## Technologies Used
- `fastify`
- `@fastify/swagger`
- `fastify/cli`
- `zod`
- `tap`
- `Pact`
- `Docker`

## Requirements
- Node.js v18
- Docker

## CI Workflow

### test
The test job is run on every push to the main branch. It starts by setting up Node.js, installs dependencies using Yarn, then runs the tests defined in the Makefile via `make test`. After successful test execution, it publishes the provider contract using `make publish_provider_contract`.

Environment variables are defined to specify the Pact Broker Base URL, Pact Broker Token, the name of the participant (in this case, 'backend'), and other variables related to reporting and versioning.

### can_i_deploy
The can_i_deploy job is dependent on the test job and only runs if the test job passes. It checks if it's safe to deploy the provider (backend in this case) to production using make can_i_deploy.

### deployment
The deployment job depends on the can_i_deploy job and only runs if the can_i_deploy job passes. It deploys the backend and records the deployment using make deploy and make record_deployment commands respectively.

## Local Deployment
### Setup
- Create a .env file in the root directory and add the following variables:
```
PACT_BROKER_BASE_URL=https://****.pactflow.io
PACT_BROKER_TOKEN=1234567890
PACTICIPANT=name-of-your-service
PORT=4000
```

### Development
Run the following command to start the app in development mode:

```bash
yarn dev
```

### Test
To map the CI process locally execute the command:
```bash
make ci
```
48 changes: 48 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
version: '3.8'
services:
can_i_deploy:
image: pactfoundation/pact-cli:latest
environment:
- PACT_BROKER_BASE_URL
- PACT_BROKER_TOKEN
command: >
pact-broker can-i-deploy
--pacticipant ${PACTICIPANT}
--version ${VERSION}
--to-environment ${DEPLOYMENT_TARGET}
--retry-while-unknown 6
--retry-interval 10
volumes:
- ./:/${PWD}
record_deployment:
image: pactfoundation/pact-cli:latest
command: >
pact-broker record_deployment
--pacticipant ${PACTICIPANT}
--version ${VERSION}
--environment ${DEPLOYMENT_TARGET}
volumes:
- ./:/${PWD}
environment:
- PACT_BROKER_BASE_URL
- PACT_BROKER_TOKEN
publish_provider_contract:
image: pactfoundation/pact-cli:latest
working_dir: /app
environment:
- PACT_BROKER_BASE_URL=${PACT_BROKER_BASE_URL}
- PACT_BROKER_TOKEN=${PACT_BROKER_TOKEN}
- VERSION=${VERSION}
- GIT_BRANCH=${GIT_BRANCH}
volumes:
- ./:/app
command: >
/bin/sh -c "pactflow publish-provider-contract ${OAS_PATH}
--provider ${PACTICIPANT}
--provider-app-version $${VERSION}
--branch $${GIT_BRANCH}
--content-type application/yaml
--verification-exit-code=0
--verification-results ${REPORT_PATH}
--verification-results-content-type ${REPORT_FILE_CONTENT_TYPE}
--verifier ${VERIFIER_TOOL}"
67 changes: 67 additions & 0 deletions oas/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
openapi: 3.0.3
info:
title: My provider
description: API
version: 0.1.0
components:
schemas:
Schema:
type: object
properties:
userSchemaRequest:
type: object
properties:
id:
type: string
required:
- id
additionalProperties: false
userSchemaReply:
type: object
properties:
id:
type: number
name:
type: string
required:
- id
- name
additionalProperties: false
userSchemaReplyArray:
type: array
items:
$ref: "#/components/schemas/Schema/properties/userSchemaReply"
required:
- userSchemaRequest
- userSchemaReply
- userSchemaReplyArray
additionalProperties: false
paths:
/users:
get:
operationId: getAllUsers
description: Get all users
responses:
"200":
description: Default Response
content:
application/json:
schema:
$ref: "#/components/schemas/Schema/properties/userSchemaReplyArray"
"/users/{id}":
get:
operationId: getUser
description: Get a user by id
parameters:
- schema:
type: string
in: path
name: id
required: true
responses:
"200":
description: Default Response
content:
application/json:
schema:
$ref: "#/components/schemas/Schema/properties/userSchemaReply"
Loading

0 comments on commit 6ab3e7f

Please sign in to comment.