sanity-io / github-action-sanity

MIT License
57 stars 20 forks source link

Use action with a monorepo with sanity in sub directory? #3

Open eivindml opened 3 years ago

eivindml commented 3 years ago

Hi,

I have this project structure:

frontend/
studio/

How can I run the workflow from this sub directory?

kmelve commented 3 years ago

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/github-action-sanity@v0.1-alpha
        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!

eivindml commented 3 years ago

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
ndimatteo commented 3 years ago

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

stianlp commented 3 years ago

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
eivindml commented 3 years ago

@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
tylermcrobert commented 3 years ago

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

SimeonGriggs commented 3 years ago

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 )

fibonacid commented 3 years ago

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
geball commented 2 years ago

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!

nickdandakis commented 2 years ago

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.

ghost commented 2 years ago

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/github-action-sanity@v0.3
        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
ThomasWaghubinger commented 2 years ago

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/github-action-sanity@v0.3
        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

gubatenkov commented 2 years ago

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/github-action-sanity@v0.4 dont work with sub dirs. Had before same issue as the dude above

ThomasWaghubinger commented 2 years ago

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/github-action-sanity@v0.4 dont work with sub dirs. Had before same issue as the dude above

Wohoo Thanks! This finally works! 🎉

geball commented 2 years ago

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.

ThomasWaghubinger commented 2 years ago

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 🤷‍♂️