From 5442f87e5d5cac4df3fc98c8d4acbeff71766da3 Mon Sep 17 00:00:00 2001 From: Rukmal Weerawarana Date: Wed, 14 Sep 2022 15:17:45 +0530 Subject: [PATCH] Add pipeline for running tests - Works both locally and on GitHub actions - Update .gitignore Closes #1 --- .github/workflows/push.yml | 89 ++++++++++++++++++++++++++++++++++++++ .gitignore | 3 +- Makefile | 25 +++++++++++ README.md | 5 +++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/push.yml create mode 100644 Makefile create mode 100644 README.md diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml new file mode 100644 index 0000000..5b9e41a --- /dev/null +++ b/.github/workflows/push.yml @@ -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 diff --git a/.gitignore b/.gitignore index 6100d36..baabd11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store -notes +notes.md + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..486490f --- /dev/null +++ b/Makefile @@ -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}' diff --git a/README.md b/README.md new file mode 100644 index 0000000..9de398f --- /dev/null +++ b/README.md @@ -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.