Skip to content

Lesson 3 of 6

Package the site

Create a compressed archive of the site files, ready for deployment.

9 minDevOps

After this lesson you will be able to package all your site files into one compressed archive, ready to copy to a server.

What packaging means

Your site is a folder with HTML files and a test script. To move it to another machine, you can copy the files one by one, but that is slow and easy to get wrong. Packaging means putting all the files into one archive file. An archive keeps the folder structure and file contents together, and compression makes the archive smaller so it copies faster.

You will use the tar command, which is available on Linux and macOS. tar can create an archive and compress it in one step. The result is a single file with a .tar.gz extension.

Create an archive

Make sure you are in the my-site folder that you created in lesson 1. Run this command:

tar -czf site.tar.gz *.html test.sh

The options mean: -c create an archive, -z compress it with gzip, -f write to the named file. The wildcard *.html matches every HTML file in the current folder. If you did the exercise in lesson 1, this includes about.html; if not, it includes only index.html. The final argument test.sh includes the test script.

List the files in the folder to see the archive:

ls -l

You should see site.tar.gz along with your site files. The archive contains your HTML files and the test script. The exact list depends on which HTML files are in your folder.

Check the archive contents

Before you rely on an archive, check what is inside it. Run:

tar -tzf site.tar.gz

The -t option lists the contents instead of extracting them. You should see index.html and test.sh. If you have about.html, it is listed too. This confirms that the archive has exactly the files you expect.

Exclude files you do not need

Your folder also contains the .git directory and the .gitignore file. You do not need these on the server, because the server only serves the site files. If you later change the packaging command to include the whole folder, these would be copied too. To keep them out, use the --exclude option:

tar -czf site.tar.gz --exclude='.git' --exclude='.gitignore' *.html test.sh

This creates a new archive with the same name, overwriting the old one. Because the command only lists HTML files and test.sh, the excludes are not needed right now, but they protect you if you change the file list later. Check the contents again:

tar -tzf site.tar.gz

The output should be the same as before.

Use a script to package

Typing the file list every time is tedious and error-prone. Create a script called package.sh that runs the packaging command for you.

Create the file:

touch package.sh

Open it in your editor and add these lines:

#!/bin/bash
set -e

tar -czf site.tar.gz --exclude='.git' --exclude='.gitignore' *.html test.sh

echo "Created site.tar.gz"

The first line tells the system to run this script with Bash. The second line makes the script stop if any command fails. Then it creates the archive and prints a message. The *.html wildcard means the script packages every HTML file in the folder, so if you add another page later, the script already includes it.

Make the script executable:

chmod +x package.sh

Run it:

./package.sh

You should see the message Created site.tar.gz. Check the archive contents again to confirm it worked.

Add the archive to .gitignore

The archive is a generated file. You do not want to commit it to version control, because it can be recreated from the source files at any time. Open .gitignore and add a line:

.DS_Store
*.log
site.tar.gz

Now Git will ignore the archive. Run git status to confirm it is not listed as an untracked file. The package.sh file will appear because it is not yet tracked, but site.tar.gz should not appear.

Commit the packaging script

The package.sh script is part of your project and should be committed. Stage and commit it:

git add package.sh .gitignore
git commit -m "Add packaging script"

The post-commit hook from lesson 2 will run test.sh automatically. It should pass, because you have not changed the site files.

You now have a repeatable way to package your site into one archive file, ready for deployment.

Key takeaways

  • Packaging puts all site files into one compressed archive for easy copying.
  • Use tar -czf to create a .tar.gz archive, and tar -tzf to list its contents.
  • The wildcard *.html means the command includes every HTML file, even ones you add later.
  • Exclude version control files with --exclude to keep the archive clean if you widen the file list.
  • Generated archives should be listed in .gitignore so they are not committed.

Try it

Create a new page called contact.html with a heading that says "Contact" and a short paragraph. Because package.sh uses *.html, you do not need to edit the script. Run ./package.sh and then tar -tzf site.tar.gz to confirm that contact.html is listed. Commit the new page with a suitable message. The post-commit hook should pass, and git status should show a clean working directory.

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.