JSPDF.jpg
Cover image for ron

Ron Northcutt Verified userVerified user

Head of DevRel

Appsmith

Speed Up Drupal Development with This Simple Ddev Script

Goal

Create a simple shell script that will automate the creation and updating of your local development environment using DDev.

Prerequisites

  • Composer installed on your local machine.

  • Docker installed on your local machine.

  • Curl on Linux or Brew on MacOS.

Overview

I consider myself a reasonably DRY person. I mean this in the programming sense, not in terms of actual lack of liquid. I'm 60% water, just like any other human... 61% when it rains.

What I mean is that I Don't want to Repeat Myself, especially when we are talking about daily or weekly tasks. Anytime I have a series of repeated steps in a routine, I have a natural drive to look at automating that process. It doesn't work in all areas of my life, but when it comes to software development, there are usually more opportunities to create shortcuts... er... I mean... to standardize. Yeah - that's it. I'm not lazy, just efficient.

I'm the primary developer for our community portal in addition to my other responsibilities, so I end up working on Drupal code at least a few times a week. DDev is the tool we use for local development, and I ❤️ how simple and powerful it is. A ton of thought and work has gone into making DDev such a reliable and elegant tool. It "just works™".

However, I noticed that I used the same series of commands every time I set up the project on my local machine (especially after a reboot). I figured this would be a good candidate for a simple shell script that I can use to get it all done in a single command. To my delight, this has turned out to be super simple and incredibly convenient. I've been using this script for about 5 months, and even though its not the most impressive work I've done... the result has been super impactful. 

When I have time to write code, I run a single command and then go do other things. Meanwhile, it creates the local docker containers, updates them, builds the codebase, gets the latest DB and files from prod, adds my SSH keys to the containers, and generates a one-time login link for the local website. Whenever I am ready to switch into full dev mode, everything it ready to go

So - if you want to take a few minutes to create an automation script for your own local dev environment, I can promise you it will be worth it. Let's see how easy it is.

  1. Install DDev

    If you haven't already, download and install DDev on your local. This is almost always going to be super easy to do:

    // On MacOS
    brew install ddev/ddev/ddev
    
    // On Linux
    curl -fsSL https://pkg.ddev.com/apt/gpg.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/ddev.gpg > /dev/null
    echo "deb [signed-by=/etc/apt/trusted.gpg.d/ddev.gpg] https://pkg.ddev.com/apt/ * *" | sudo tee /etc/apt/sources.list.d/ddev.list
    sudo apt update && sudo apt install -y ddev

    Note: ddev provides a wrapper around the containers and becomes the meta-command for much of what we do. 

  2. Configure Environment Variables

    Depending on your hosting provider, you may need to set up specific environment variables for DDev. Refer to DDev's documentation and your hosting provider's documentation for detailed instructions. The most common managed hosting companies are:

  3. Understanding the workflow

    Before we write the script, we need to understand the basic workflow and commands. This really is a super simple script - it's just running a series of commands. But, we need to know what commands we need and why to ensure we set it up properly.

    The script automates the process of setting up a local Drupal development environment. Here's what each part does:

    • ddev start: Starts the DDev containers.
    • ddev auth ssh: Adds your SSH key to the auth container.
    • ddev pull [PROVIDER] -y: Pulls the latest database and files from the production environment.
    • ddev drush cr: Clears the Drupal cache.
    • ddev drush uli: Generates a one-time login URL for the admin user.

    When you run these commands in this order, DDev will create the local environment, update it, and give you a link to login and get started.

  4. Create the script

    In your codebase, create a file called startdev.sh (or whatever you like). Put in the commands you want to run along with any output text. Here is my example:

    #!/bin/bash
    
    # Start the containers and set them up
    echo '<----------- Starting containers ------------------------------->'
    ddev start
    
    # Add your SSH key to the auth container
    echo '<----------- Adding SSH key to auth container ------------------>'
    ddev auth ssh
    
    # Pull the latest DB and files from prod to local
    echo '<----------- Pulling latest DB and files from prod ------------->'
    ddev pull acquia -y
    
    # Clear the cache after the DB update
    echo '<----------- Clearing cache ------------------------------------>'
    ddev drush cr
    
    # Create a one-time login for the admin super user
    echo '<----------- Creating a one-time login for admin --------------->'
    ddev drush uli
    
    echo 'Done!'
    echo '<----------- Local dev set up - lets have some fun!  ----------->'

    The great thing about this is that my notes above make it clear where I am in the process. Meanwhile, the other outputs from DDev will display normally.

    Note: I'm using Acquia as the provider in this example, but you will need to change that to whatever provider you set up in step 2.

  5. Run the script

    Honestly, that's about it. All we need to do is give the script execute permissions, add it to your repo, and then run it!

    // Give the script execute permissions. You only need to do this once.
    chmod +x startdev.sh
    
    // Add it to your repo
    git add startdev.sh
    git commit -m "Adding an awesome local dev script to my project."

    Now, any time you need to setup (or update) your local dev environment, just run this command:

    ./startdev.sh

    Note: The ./ prefix is used in Unix-like operating systems, including Linux and macOS, to run executable files or scripts that are located in the current directory. Unless your current directory is in your PATH environment variable, you need to tell the OS where the file is located.

  6. Customize the script

    Why stop there? There are plenty of things you can do to customize your script. You can modify the messages, add a quote of the day, run other commands (like enabling dev modules), etc. For me, I decided to add some fun ASCII art to the script to display a custom message for the devs on the team:

    ddev setup script

Conclusion

While this is a really simple script, its actually super powered by the DDev commands. This is something I use all the time, and it not only saves me time, but it lets me relax and focus on what really matters. 

Additional Resources

selwynpolit public View selwynpolit's profile
Fri, 12/15/2023 - 17:50

Hey Ron, 

glad to you see you are still dabbling in the Drupal world!  Nice article.  I'm also a huge DDEV fan.