roots / trellis

WordPress LEMP stack with PHP 8.2, Composer, WP-CLI and more
https://roots.io/trellis/
MIT License
2.51k stars 607 forks source link

Add conditional dry_run #1483

Closed MWDelaney closed 1 year ago

MWDelaney commented 1 year ago

Sometimes I want to be able to check if Trellis can deploy, including whether it can run its build hooks, install Composer dependencies, etc, without actually deploying. A use case example is a check using a GitHub workflow.

Test with the following:

$ trellis deploy --extra-vars "dry_run=true" production

I was able to get this working using the following GitHub workflow(s)

# .github/workflows/dry-run.yml
name: Dry-run deploy to target branch
run-name: Dry-run deploy to target branch

on:
  pull_request:
    branches: [staging, production]

jobs:
  dry-run:
    uses: ./.github/workflows/trellis-cli.yml
    secrets: inherit
    with:
      extra-vars: '"dry_run=true"'
# .github/workflows/trellis-cli.yml

name: trellis-cli
run-name: 'Trellis CLI'

on:
  workflow_call:
    inputs:
      extra-vars:
        required: false
        type: string

jobs:
  trellis-cli:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - uses: andstor/file-existence-action@v2
      id: check_files
      with:
        files: "trellis/deploy.yml"
    - uses: shimataro/ssh-key-action@v2
      with:
        key: ${{ secrets.TRELLIS_DEPLOY_SSH_PRIVATE_KEY }}
        known_hosts: ${{ secrets.TRELLIS_DEPLOY_SSH_KNOWN_HOSTS }}
    - uses: webfactory/ssh-agent@v0.5.4
      with:
        ssh-private-key: ${{ secrets.TRELLIS_DEPLOY_SSH_PRIVATE_KEY }}
    - uses: roots/setup-trellis-cli@v1
      with:
        repo-token: ${{ secrets.GITHUB_TOKEN }}
        ansible-vault-password: ${{ secrets.ANSIBLE_VAULT_PASSWORD }}
    - name: Deploy
      if: steps.check_files.outputs.files_exists == 'true'
      run: trellis deploy --extra-vars ${{ inputs.extra-vars }} ${{ github.base_ref }}
MWDelaney commented 1 year ago

Would there be a practical way to divert the deploy directory to a temporary location so that it doesn't interrupt the chain of deploys?

swalkinshaw commented 1 year ago

Overriding the project_root variable should theoretically do that. If could be set to a dir in /tmp?

MWDelaney commented 1 year ago

Seems to work...

$ trellis deploy --extra-vars "dry_run=true project_root=/tmp/trellis" staging
root@example.com:/tmp/trellis# ls
releases  shared

Is there an inventory that's being updated for rollback somewhere that this going to interfere with?

swalkinshaw commented 1 year ago

Is there an inventory that's being updated for rollback somewhere that this going to interfere with?

I don't think so because it's in a different path, so the normal deploy help wouldn't even know about it.

Deploys that aren't finalized get an unfinished file written to them, but again, this would be in a different path so it's outside of the deploy cleanup process. One small risk is that they wouldn't be cleaned up by default?

MWDelaney commented 1 year ago

Could either run the cleanup on that path, or just rm -rf the /tmp/trellis directory before it bails?

swalkinshaw commented 1 year ago

Deleting it right after works 👍 (maybe with a variable to optionally skip that)

MWDelaney commented 1 year ago

This hard-codes a path that I'm setting in --extra-vars though