Skip to content

Instantly share code, notes, and snippets.

@aflansburg
Last active July 22, 2024 19:22
Show Gist options
  • Save aflansburg/328782a924b3c1d11c2c96da38c2d858 to your computer and use it in GitHub Desktop.
Save aflansburg/328782a924b3c1d11c2c96da38c2d858 to your computer and use it in GitHub Desktop.
checking for changes between release tags with paths-filter

Check for changes in Hasura metadata & migrations between GH releases

What

Checks for changes to local hasura metadata and migration files. This workflow would run on your main / default branch and detect changes.

You could then run hasura migrate apply or hasura metadata apply conditionally based on these changes.

Setup

  1. Install act: https://nektosact.com/installation/index.html
  2. Place the workflow in the appropriate place in your project under `.github/workflows/hasura-changes-since-release.yml
  3. Grab a Github Personal Access Token (PAT) for your repository.
  4. Run the command:
act -s GITHUB_TOKEN -W '.github/workflows/hasura-changes-since-release.tf'
  1. You'll be prompted to provide your PAT. Enter/paste that and ENTER.
  2. Profit 💰

Output

Output should look something like:

| Migration changes are:
| Metadata changes are: foo/bar/metadata/databases/baz/foobar/foobaz.yaml

Notes

The act tool can be finicky. There are sometimes issues with paths-filter and git version as well. That's why you see git version 2.44 pinned.

It would be worthwile to build a base image with all of the dependencies and git installed from source, which would make it run much more quickly!

You'll notice if: ${{ env.ACT }} on some steps which means they will only fire if you are running the action locally via act.

Important!

If running on Mac ARM architecture you may want to disable Rosetta in Docker Desktop settings.

name: Get Previous Release Tag
on:
push:
tags:
- 'v*'
jobs:
get-previous-tag:
runs-on: ubuntu-latest
steps:
- name: Echo the git version
run: echo "Git version pre dep setup is $(git --version)"
- name: Install Act dependencies
if: ${{ env.ACT }}
run: |
apt-get update
apt-get install -y sudo software-properties-common curl build-essential libterm-readline-gnu-perl
- name: Install additional build dependencies
if: ${{ env.ACT }}
run: |
sudo apt-get update
sudo apt-get install -y gettext libssl-dev libcurl4-openssl-dev zlib1g-dev expat libexpat1-dev
- name: Download and install Git 2.44.0
run: |
curl -o git-2.44.0.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.44.0.tar.gz
tar -xzf git-2.44.0.tar.gz
cd git-2.44.0
make prefix=/usr/local all
sudo make prefix=/usr/local install
git --version
- name: Echo the git version
run: echo "Git version is $(git --version)"
- name: Check out code
uses: actions/checkout@v4
- name: Branch
run: echo "Current branch/ref is ${{ github.ref }}"
- name: Get the latest tag
id: get_latest_tag
run: |
LATEST_TAG=$(git describe --tags --abbrev=0)
echo "Latest tag is $LATEST_TAG"
echo "LATEST_TAG=${LATEST_TAG}" >> $GITHUB_ENV
- name: Get previous tag
id: get_previous_tag
run: |
echo "Current tags are $(git tag --sort=-v:refname)"
echo "Latest tag is ${{ env.LATEST_TAG }}"
ALL_TAGS=$(git tag --sort=-v:refname)
PREVIOUS_TAG=$(git describe --tags --abbrev=0 $LATEST_TAG^)
echo "PREVIOUS_TAG=${PREVIOUS_TAG}" >> $GITHUB_ENV
- name: Output previous release tag
run: echo "Previous release tag is ${{ env.PREVIOUS_TAG }} and its sha is $(git rev-parse ${{ env.PREVIOUS_TAG }})"
- name: Set sha for previous release tag
run: echo "PREVIOUS_TAG_SHA=$(git rev-parse ${{ env.PREVIOUS_TAG }})" >> $GITHUB_ENV
- name: Output current release tag
run: echo "Current release tag is ${{ env.LATEST_TAG }} and its sha is $(git rev-parse ${{ env.LATEST_TAG }})"
- name: Set sha for current release tag
run: echo "LATEST_TAG_SHA=$(git rev-parse ${{ env.LATEST_TAG }})" >> $GITHUB_ENV
- name: Apply filters
id: hasura-changes
uses: dorny/paths-filter@v3
with:
filters: |
migrations:
- 'path/to_your/migrations/**'
metadata:
- 'path/to_your/metadata/**'
base: ${{ env.PREVIOUS_TAG_SHA }}
ref: ${{ env.LATEST_TAG_SHA }}
list-files: 'shell'
- name: Output Hasura migration/metadata changes
run: |
echo "Migration changes are: ${{ steps.hasura-changes.outputs.migrations_files }}"
echo "Metadata changes are: ${{ steps.hasura-changes.outputs.metadata_files }}"
# Example - apply migration or metadata changes conditionally IF there are any
- name: Apply Hasura migrations
if: steps.hasura-changes.outputs.migrations_files != ''
run: |
echo "Applying Hasura migrations..."
hasura migrate apply --endpoint <your-hasura-endpoint> --admin-secret <your-admin-secret>
- name: Apply Hasura metadata
if: steps.hasura-changes.outputs.metadata_files != ''
run: |
echo "Applying Hasura metadata..."
hasura metadata apply --endpoint <your-hasura-endpoint> --admin-secret <your-admin-secret>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment