Mooie manier om een dev-omgeving te draaien

Hi everyone, just sharing a handy script I’ve put together that helps me run my Discourse dev instance.

It pulls all the latest changes, pulls the updates from installed plugins, and runs all the migrations, as well as installing the necessary gems and pnpm packages.

The downside to the script is that it also pulls the plugins bundled with core, which technically wouldn’t do anything since they would be updated with the first git pull. Oh well.

Note: chatgpt.com helped me write the if statement logic (my initial experiments failed).
~/rundiscourse.sh:

#!/bin/bash

cd ~/discourse
git pull
cd plugins
PLUGIN_DIR=~/discourse/plugins

for folder in "$PLUGIN_DIR"/*; do
  if [ -d "$folder/.git" ]; then
    output=$(git -C "$folder" pull)
    if ! echo "$output" | grep -q "Already up to date."; then
      echo "[$(basename "$folder")] $output"
    fi
  fi
done
cd ~/discourse
bundle install
pnpm install
bin/rails db:migrate
DISCOURSE_HOSTNAME=localhost UNICORN_LISTENER=localhost:3000 bin/ember-cli -u

Then, run:

chmod +x rundiscourse.sh

Then, go to the ~/.bashrc file (using nano, or anything), and add this bit if you don’t already have it (it’s nearer the bottom)

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Then, add the following to the ~/.bash_aliases file:

alias discourse="~/rundiscourse.sh"

Close your terminal and re-open it.
Now, when you run discourse in the terminal, your development environment should start!


I hoped this helped! Also, do let me know if I’m doing something wrong, or if it can be improved. Thanks!

1 like

Nice. Een paar suggesties.

De shebang moet #!/usr/bin/env sh zijn, wat het meest draagbaar is. Het script gebruikt geen bash-specifieke commando’s, dus het zou moeten werken in elke Bourne shell-compatibele shell, niet alleen bash.

Voeg na de shebang set -e toe. Dit zorgt ervoor dat het script stopt als een van de commando’s mislukt. Anders zal het vrolijk doorgaan met het uitvoeren van commando’s en later falen omdat iets als bundle install is mislukt, en de foutmelding is nu verdwenen van het scherm van de gebruiker.

Nu is de plugin git pull een beetje problematischer, omdat fouten die naar STDERR worden geschreven en een falende git-uitvoering niet correct zullen worden afgehandeld. Dus ik heb STDERR ook naar STDOUT gepusht en de exit code bijgehouden.

Geen noodzaak voor een reeks cd commando’s, of het instellen van de PLUGIN_DIR variabele.

#!/usr/bin/env sh
set -e

cd ~/discourse
git pull

for folder in plugins/*; do
  if [ -d "$folder/.git" ]; then
    exitcode=0
    output=$(git -C "$folder" pull 2>&1) || exitcode=$?
    if echo "$output" | grep -q -v "Already up to date."; then
      echo "[$(basename "$folder")]"
      echo "$output"
      [ "$exitcode" -ne 0 ] && exit "$exitcode"
    fi
  fi
done

bundle install
pnpm install
bin/rails db:migrate
DISCOURSE_HOSTNAME=localhost UNICORN_LISTENER=localhost:3000 bin/ember-cli -u

Ik had al een vergelijkbaar script, behalve de git pulling, omdat ik niet wil dat dat elke keer gebeurt.

1 like

Bedankt voor het inzicht! Ik zal dat proberen.