github-action-sanity icon indicating copy to clipboard operation
github-action-sanity copied to clipboard

Use action with a monorepo with sanity in sub directory?

Open eivindml opened this issue 4 years ago • 16 comments
trafficstars

Hi,

I have this project structure:

frontend/
studio/

How can I run the workflow from this sub directory?

eivindml avatar Jan 18 '21 12:01 eivindml

I believe you can set working-directory where you specify the job. So:

name: Deploy Sanity
on:
  push:
    branches: [main]
jobs:
  sanity-deploy:
    defaults:
      run:
        working-directory: studio
    runs-on: ubuntu-18.04
    name: Deploy Sanity
    steps:
      - uses: actions/checkout@v2
      - uses: sanity-io/[email protected]
        env:
          SANITY_AUTH_TOKEN: ${{ secrets.SANITY_AUTH_TOKEN }}
        with:
          args: deploy

Haven't tested it myself, but you can go here to read more. Let us know if it works!

kmelve avatar Jan 18 '21 13:01 kmelve

On step closer, but looks like it won't find the sanity.json config file. It is defined in studio/sanity.json. 🤔

Skjermbilde 2021-01-18 kl  15 51 32

eivindml avatar Jan 18 '21 14:01 eivindml

Same issue here, my Sanity studio is in a subfolder of my project causing the action to fail 😬

ndimatteo avatar Jan 18 '21 18:01 ndimatteo

This works for us:

name: Deploy Sanity Studio
on:
  push:
    branches: [main]
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup Node.js environment
        uses: actions/setup-node@v2
        with:
          node-version: 12.x
      - name: Install dependencies
        run: |
          cd sanity
          npm install
      - name: Deploy Sanity Studio
        run: |
          set -e
          cd sanity
          SANITY_AUTH_TOKEN="${{ secrets.SANITY_STUDIO_DEPLOY }}" npx sanity deploy

stianlp avatar Jan 29 '21 22:01 stianlp

@stianlp I just tried that, but I get command not found: sanity. Any ideas? I tried adding sanity as a dependency.

Skjermbilde 2021-02-03 kl  09 04 59

eivindml avatar Feb 03 '21 08:02 eivindml

@eivindml same here. really haven't found a way to get this to work in a subdirectory

tylermcrobert avatar Mar 11 '21 20:03 tylermcrobert

The above code doesn't include a step to install @sanity/cli, if it's not already in your package.json. So you can add it like this. I've also added a paths-ignore key so it doesn't redeploy on changes to the website in the same repo.

Change studio to the name of folder of your Sanity studio in your repo.

name: Deploy Sanity Studio
on:
  push:
    branches: [main]
    paths-ignore: 
      - "./web/**"
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup Node.js environment
        uses: actions/setup-node@v2
        with:
          node-version: 12.x
      - name: Install dependencies
        run: |
          cd studio
          npm install
          npm install @sanity/cli
      - name: Deploy Sanity Studio
        run: |
          set -e
          cd studio
          SANITY_AUTH_TOKEN="${{ secrets.SANITY_AUTH_TOKEN }}" npx sanity deploy

(I forked Knut's action with the intention of updating it but this was quicker ... maybe next time! :D )

SimeonGriggs avatar Apr 02 '21 08:04 SimeonGriggs

Here is the example for backups when the studio is inside a sub directory (admin).

name: Backup Routine
on:
    workflow_dispatch:
    schedule:
        # Runs at 04:00 UTC on the 1st and 17th of every month
        - cron: '0 4 */16 * *'

jobs:
    backup-dataset:
        runs-on: ubuntu-18.04
        name: Backup dataset
        defaults:
            run:
                working-directory: admin
        steps:
            - uses: actions/checkout@v2
            - name: Install dependencies
              run: yarn install

            - name: Export dataset
              run: |
                  SANITY_AUTH_TOKEN="${{ secrets.SANITY_AUTH_TOKEN }}" \
                  npx sanity dataset export \
                  development backups/backup.tar.gz

            - name: Upload backup.tar.gz
              uses: actions/upload-artifact@v2
              with:
                  name: backup-tarball
                  path: admin/backups/backup.tar.gz
                  # Fails the workflow if no files are found; defaults to 'warn'
                  if-no-files-found: error

fibonacid avatar Jun 28 '21 12:06 fibonacid

There's a tagged pre-release, v0.3-alpha, that tests a new API to permit you to specify a subdirectory for the studio. Details are in the v0.3-alpha README, but the essential change to note (other than tagging @v0.3-alpha) is that args now takes two or more arguments (instead of one or more) and the first is the working directory (. for the top-level of the repo or a named subdirectory such as studio in the case of a monorepo, for example).

This is still an alpha and is not guaranteed to be the direction taken with the API. However, we'd be interested to see if it solves anyone's issues when using this workflow where the studio is in a subdirectory. Please let us know!

geball avatar Jan 25 '22 05:01 geball

FWIW our team has targeted v0.3-alpha and with the new API for args, we're able to deploy Sanity studio from a subdirectory.

Thank you @geoffreyjball!

I will say, the API feels a little clunky and a better API would be to allow for a separate param for directory, in addition to the args param.

nickdandakis avatar Jan 27 '22 15:01 nickdandakis

Hello Everyone,

The API v0.3 is supposed to work, however I get this weird output.

image

Here is my YAML configuration of workflow

name: Github Actions
on:
  schedule:
    # Runs at 00:00 UTC on each Sunday
    - cron: "0 0 * * 0"
  workflow_dispatch:
jobs:
  sanity-backup:
    runs-on: ubuntu-latest
    name: Backup dataset
    steps:
      - uses: actions/checkout@v3
      - name: Export dataset
        uses: sanity-io/[email protected]
        env:
          SANITY_AUTH_TOKEN: ${{ secrets.SANITY_AUTH_TOKEN }}
        with:
          args: studio dataset export production backups/backup.tar.gz
      - name: Upload backup.tar.gz
        uses: actions/upload-artifact@v2
        with:
          name: backup-tarball
          path: backups/backup.tar.gz
          # Fails the workflow if no files are found; defaults to 'warn'
          if-no-files-found: error

arenhovsepyan avatar Jun 29 '22 01:06 arenhovsepyan

Hey,

Hello Everyone,

The API v0.3 is supposed to work, however I get this weird output.

image

Here is my YAML configuration of workflow

name: Github Actions
on:
  schedule:
    # Runs at 00:00 UTC on each Sunday
    - cron: "0 0 * * 0"
  workflow_dispatch:
jobs:
  sanity-backup:
    runs-on: ubuntu-latest
    name: Backup dataset
    steps:
      - uses: actions/checkout@v3
      - name: Export dataset
        uses: sanity-io/[email protected]
        env:
          SANITY_AUTH_TOKEN: ${{ secrets.SANITY_AUTH_TOKEN }}
        with:
          args: studio dataset export production backups/backup.tar.gz
      - name: Upload backup.tar.gz
        uses: actions/upload-artifact@v2
        with:
          name: backup-tarball
          path: backups/backup.tar.gz
          # Fails the workflow if no files are found; defaults to 'warn'
          if-no-files-found: error

Hey, i can't make it work to get the backup uploaded. As i can see in your screenshot, your upload process seems also to fail.

Did you manage to get it work?

Thanks

ThomasWaghubinger avatar Nov 25 '22 07:11 ThomasWaghubinger

Here is the example for backups when the studio is inside a sub directory (admin).

name: Backup Routine
on:
    workflow_dispatch:
    schedule:
        # Runs at 04:00 UTC on the 1st and 17th of every month
        - cron: '0 4 */16 * *'

jobs:
    backup-dataset:
        runs-on: ubuntu-18.04
        name: Backup dataset
        defaults:
            run:
                working-directory: admin
        steps:
            - uses: actions/checkout@v2
            - name: Install dependencies
              run: yarn install

            - name: Export dataset
              run: |
                  SANITY_AUTH_TOKEN="${{ secrets.SANITY_AUTH_TOKEN }}" \
                  npx sanity dataset export \
                  development backups/backup.tar.gz

            - name: Upload backup.tar.gz
              uses: actions/upload-artifact@v2
              with:
                  name: backup-tarball
                  path: admin/backups/backup.tar.gz
                  # Fails the workflow if no files are found; defaults to 'warn'
                  if-no-files-found: error

This one help a lot thanks, man! By the way sanity-io/[email protected] dont work with sub dirs. Had before same issue as the dude above

tenkkov avatar Nov 29 '22 17:11 tenkkov

Here is the example for backups when the studio is inside a sub directory (admin).

name: Backup Routine
on:
    workflow_dispatch:
    schedule:
        # Runs at 04:00 UTC on the 1st and 17th of every month
        - cron: '0 4 */16 * *'

jobs:
    backup-dataset:
        runs-on: ubuntu-18.04
        name: Backup dataset
        defaults:
            run:
                working-directory: admin
        steps:
            - uses: actions/checkout@v2
            - name: Install dependencies
              run: yarn install

            - name: Export dataset
              run: |
                  SANITY_AUTH_TOKEN="${{ secrets.SANITY_AUTH_TOKEN }}" \
                  npx sanity dataset export \
                  development backups/backup.tar.gz

            - name: Upload backup.tar.gz
              uses: actions/upload-artifact@v2
              with:
                  name: backup-tarball
                  path: admin/backups/backup.tar.gz
                  # Fails the workflow if no files are found; defaults to 'warn'
                  if-no-files-found: error

This one help a lot thanks, man! By the way sanity-io/[email protected] dont work with sub dirs. Had before same issue as the dude above

Wohoo Thanks! This finally works! 🎉

ThomasWaghubinger avatar Nov 29 '22 20:11 ThomasWaghubinger

Glad you got this working, @ThomasWaghubinger. I agree with what's been said a few times, which is that my fix in v0.3-alpha and later is clunky. If you have suggestions for how to better approach this, I'm all ears.

geball avatar Nov 29 '22 20:11 geball

Glad you got this working, @ThomasWaghubinger. I agree with what's been said a few times, which is that my fix in v0.3-alpha and later is clunky. If you have suggestions for how to better approach this, I'm all ears.

Thanks, hmm.. To be honest, this was the very first time I had anything to do with github actions. But the way @sokolenkov posted here works pretty good so far, maybe it would make sense to reference this somewhere in one of the sanity posts about backup with github actions or so 🤷‍♂️

ThomasWaghubinger avatar Nov 29 '22 20:11 ThomasWaghubinger