Skip to content

Commit

Permalink
Add pipeline for running tests
Browse files Browse the repository at this point in the history
- Works both locally and on GitHub actions
- Update .gitignore

Closes #1
  • Loading branch information
rukmal committed Sep 14, 2022
1 parent 9aea8d6 commit 5442f87
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
89 changes: 89 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: GraphQL API tests

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}-db
DOCKERFILE_FOLDER: ./db

on: push

jobs:
push-db-container:
name: Database Setup
runs-on: ubuntu-22.04
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
if: ${{ !env.ACT }} # Only run on GitHub Actions
uses: actions/checkout@v3
- name: Log in to the Container registry
if: ${{ !env.ACT }} # Only run on GitHub Actions
id: docker-registry-login
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
if: ${{ !env.ACT }} # Only run on GitHub Actions
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Setting up Docker Buildx with docker-container driver is required
# at the moment to be able to use a subdirectory with Git context
- name: Set up Docker Buildx
if: ${{ !env.ACT }} # Only run on GitHub Actions
id: buildx
uses: docker/setup-buildx-action@v2
with:
install: true # See: https://github.com/docker/setup-buildx-action#install-by-default
- name: Build and push Docker image to GitHub container registry
if: ${{ !env.ACT }} # Only run on GitHub Actions
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
file: ${{ env.DOCKERFILE_FOLDER }}/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

bal-test:
name: GraphQL API Tests
runs-on: ubuntu-22.04
needs: push-db-container
services:
mysql:
image: ghcr.io/${{ github.repository }}-db:${{ github.ref_name }}
env:
MYSQL_ROOT_PASSWORD: test
ports:
- 3306:3306
steps:
- name: Checkout
uses: actions/checkout@v3
# Install local deps if this action is being run locally with act
- name: (Local Only) Start database in Docker container
if: ${{ env.ACT }}
run: |
docker build -f ${{ env.DOCKERFILE_FOLDER }}/Dockerfile -t ghcr.io/${{ github.repository }}-db:${{ github.ref_name }} .
source api/Config.toml
docker run -d -e MYSQL_ROOT_PASSWORD=$PASSWORD -p 3306:3306 ghcr.io/${{ github.repository }}-db:${{ github.ref_name }}
- name: (Local Only) Test ballerina project
if: ${{ env.ACT }}
uses: ballerina-platform/ballerina-action@394eb82cc07e020948fee8d1474143ae393147f4
with:
args: test
env:
WORKING_DIR: api
BAL_CONFIG_FILES: tests/LocalConfig.toml

- name: Test ballerina project
if: ${{ !env.ACT }}
uses: ballerina-platform/ballerina-action@394eb82cc07e020948fee8d1474143ae393147f4
with:
args: test
env:
WORKING_DIR: api
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
notes
notes.md

25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.DEFAULT_GOAL := help

# Run GitHub Workflows with Act
###############################

.PHONY: run
run: ## Run GitHub workflows with act
act -v

.PHONY: clean
clean: ## Clean docker containers and volumes (causes act to break sometimes)
docker rm -f $(shell docker ps -a -q)
docker volume rm $(shell docker volume ls -q)

.PHONY: all
all: ## Cleans up docker containers, and runs GitHub workflows with act
$(MAKE) clean
$(MAKE) run

# Util
#######

.PHONY: help
help: # See: https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Local Development

### Notes for Running Tests

- Need to change `HOST` to `localhost` in `tests/Config.toml` for local tests to run correctly.

0 comments on commit 5442f87

Please sign in to comment.