Skip to content

Lesson 5 of 6

Write a deploy script

Add a script that copies the archive to the server folder, extracts it, and restarts the server.

7 minDevOps

After this lesson you will be able to write a script that packages the site, copies the archive to the server folder, extracts it, and restarts the web server.

The server folder

So far you have run the web server from the public folder. That folder is a copy of your site files. When you change the site, you copy the changed files into public and the server shows them. In this lesson you will make that process repeatable with a script.

The public folder is the server folder. It is the folder the web server reads. The server does not know about your source files in my-site. It only knows what is in public.

What the deploy script must do

A deploy script takes the packaged site and puts it where the server can serve it. For this course, the steps are:

  • Create a fresh archive of the site files.
  • Copy the archive into the server folder.
  • Remove the old site files from the server folder.
  • Extract the archive in the server folder.
  • Restart the web server.

You already have a script that creates the archive: package.sh. You will write a new script called deploy.sh that uses package.sh and then does the rest.

Write the deploy script

Create a new file called deploy.sh in the my-site folder:

touch deploy.sh

Open deploy.sh in your editor and add these lines:

#!/bin/bash
set -e

./package.sh

cp site.tar.gz public/

cd public
rm -f *.html test.sh
tar -xzf site.tar.gz
cd ..

echo "Deployed site to public/"

The first line tells the shell to run the script with Bash. The second line makes the script stop if any command fails. Then the script runs package.sh to create a fresh archive. It copies the archive into public. It changes into public, removes the old site files, and extracts the archive. Finally it returns to the my-site folder and prints a message.

Make the script executable:

chmod +x deploy.sh

Restart the server

The deploy script does not restart the server. The server is a separate process. If the server is running, it will serve the new files after they are extracted, because the Python HTTP server reads files from disk on each request. You do not need to restart it for static files.

If you want the script to restart the server, you would need to stop the running server and start it again. That is more complex and not needed for this course. The server will pick up the new files automatically.

Test the deploy script

Before you run the script, make sure the server is not running. If you still have it running from the previous lesson, stop it with Ctrl+C in the terminal where it runs.

Run the deploy script:

./deploy.sh

You should see the output from package.sh and then the message Deployed site to public/.

Now start the server again:

./serve.sh

Open http://localhost:8080 in your browser. You should see your site. The server is serving the files from public, which now contains the extracted archive.

Stop the server with Ctrl+C.

Commit the deploy script

Add the new script to Git and commit it:

git add deploy.sh
git commit -m "Add deploy script"

The post-commit hook runs test.sh and should pass. Check the log:

git log --oneline

You should see the new commit at the top.

What you have now

You have a deploy.sh script that packages the site and copies it to the server folder. The server folder is public. The script removes old files and extracts the new ones. The server serves the new files without a restart.

In the next lesson you will change the site, run the checks, deploy, and confirm the change is live.

Key takeaways

  • The server folder is the folder the web server reads, in this course public.
  • A deploy script automates copying the packaged site to the server folder and extracting it.
  • The Python HTTP server serves files from disk on each request, so it does not need a restart after deployment.
  • Use set -e in scripts to stop on the first error.

Try it

Add a new page called news.html to the my-site folder with a heading that says "News" and a short paragraph. Run ./deploy.sh to package and deploy the site. Start the server with ./serve.sh and open http://localhost:8080/news.html in your browser. You should see the new page. Stop the server with Ctrl+C. Commit news.html with the message "Add news page". The post-commit hook should pass, and git status should show a clean working directory because public/ is ignored.

Official documentation

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.

    Write a deploy script | Deploy a Static Site: Commit to Local Production | Rolebound