Skip to main content

Using Husonym in CI

Introduction

Continuous Integration is a primary usecase for utilizing Husonym. It's often the case that integration or unit tests run in CI that need good data. It's easy enough to spin up a Postgres or other kind of database using Github Actions, but the problem becomes hydrating that database with solid data that can be used for testing purposes.

For this reason, we built the husonym sync command to enable synchronizing a connection configured in Husonym to a locally hosted database, or any other database that may not otherwise be available over the internet easily.

Getting the CLI installed into a Github Action is a task in itself, however. The Husonym CLI is published as pre-built binaries on the GitHub Releases page, which makes it easy to install in any CI environment.

Installing the Husonym CLI

You can install the CLI in a Github Action by downloading the release archive that matches the runner's OS and architecture. For example, on a standard ubuntu-latest runner:

- name: Set up Husonym CLI
run: |
HUSONYM_VERSION=<version> # e.g. 1.0.0
curl -fsSL "https://github.com/fishtre-compagnie/husonym/releases/download/v${HUSONYM_VERSION}/husonym_${HUSONYM_VERSION}_linux_amd64.tar.gz" -o husonym.tar.gz
tar -xzf husonym.tar.gz husonym
sudo mv husonym /usr/local/bin/husonym
husonym version

Afterwards, any husonym command can be run in subsequent steps.

Setup a Github Action to sync remote data to a CI Postgres Database

This is a full example of a Github Action that pulls down data from a remotely configured Husonym Connection and hydrates the local Postgres database.

If you want to try this with a sample dataset, there is a good one on sqltutorial.org

name: Setup Husonym CLI and PostgreSQL Database

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
setup:
runs-on: ubuntu-latest

services:
postgres:
image: postgres
env:
POSTGRES_DB: husonym
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- name: Checkout
uses: actions/checkout@v4

- name: PostgreSQL Schema Setup
run: |
PGPASSWORD=postgres psql -h localhost -U postgres -d husonym -f sql/create.sql

- name: Select from Employees table to see that it's empty
run: |
PGPASSWORD=postgres psql -h localhost -U postgres -d husonym -c 'SELECT * from husonym.employees;'

- name: Set up Husonym CLI
run: |
HUSONYM_VERSION=<version> # e.g. 1.0.0
curl -fsSL "https://github.com/fishtre-compagnie/husonym/releases/download/v${HUSONYM_VERSION}/husonym_${HUSONYM_VERSION}_linux_amd64.tar.gz" -o husonym.tar.gz
tar -xzf husonym.tar.gz husonym
sudo mv husonym /usr/local/bin/husonym

- name: Husonym sync command
run: husonym sync --api-key ${{ secrets.HUSONYM_API_KEY }} --connection-id <connection-uuid> --destination-driver postgres --destination-connection-url "postgresql://postgres:postgres@localhost:5432/husonym?sslmode=disable"
env:
HUSONYM_API_URL: <husonym-api-url>

- name: Select from PostgreSQL
run: |
PGPASSWORD=postgres psql -h localhost -U postgres -d husonym -c 'SELECT * from husonym.employees;'