How to Deploy Your Astro Site to Netlify
This guide will show you how to take your Astro website project and make it live on the internet using Netlify. We will cover setting up a GitHub repository, connecting it to Netlify, and getting your site published. By following these steps, you’ll have your Astro site accessible to anyone online.
Prerequisites
- An Astro project created and saved on your local computer.
- A GitHub account.
- A Netlify account (a free account is sufficient).
- Basic knowledge of using your computer’s terminal or command prompt.
Step 1: Create a GitHub Repository
First, we need a place to store your Astro project code online. GitHub is a popular service for this. Go to GitHub and create a new repository.
You can name it anything you like, for example, ‘Astro Book Bites’. You can also choose to make the repository private, though it’s not required for this process. After naming it, scroll down and initialize the repository.
Once the repository is created, copy its URL. This link is like the address for your online code storage. You’ll need it to connect your local project to this new online location.
Step 2: Connect Your Local Project to GitHub
Now, we need to tell your local Astro project where to send its code. Open your computer’s terminal or command prompt. You should navigate to your Astro project’s folder using commands like ‘cd your-project-folder’.
If you haven’t already, make sure your project is set up as a Git repository. Astro often gives you the option to do this when you first create a project.
To link your local project to the GitHub repository, type the command git remote add origin YOUR_REPO_URL, replacing YOUR_REPO_URL with the link you copied from GitHub. Press Enter. This command tells your project where the ‘origin’ or main online location is.
Next, push your project’s code to GitHub. Type git push origin main and press Enter.
This sends all your project’s files and changes from your computer to the GitHub repository you just set up. Refresh your GitHub page, and you should see your project files there.
Step 3: Sign Up for Netlify
Head over to the Netlify website. If you don’t have an account, sign up for a free one. Netlify is a service that makes it easy to host websites.
Once you’re logged in, you’ll likely see a dashboard. It might look a little different over time, but the main functions remain the same.
It’s a good idea to connect your GitHub account to Netlify. You can usually do this by going to your account settings and finding an option to link your GitHub profile. This connection makes it simple for Netlify to access your code repositories.
Step 4: Import Your Project to Netlify
On your Netlify dashboard, look for an option to add a new project or site. Click on the button that allows you to import an existing project from Git. Netlify works best when connected to services like GitHub, so select the option to deploy from GitHub.
You might need to give Netlify permission to access your GitHub account. A new browser window might pop up asking you to authorize this.
Once authorized, you can select the specific GitHub repository that contains your Astro project. Search for the repository name you created earlier and choose it.
Step 5: Configure Your Netlify Deployment
After selecting your repository, Netlify will ask for some details about your project. Give your Netlify project a name; ‘Book Bites’ is a good example. Then, you’ll see several settings:
- Branch to deploy: Keep this set to ‘main’ if that’s the branch you pushed your code to.
- Base directory: Leave this blank unless your project is in a subfolder within the repository.
- Build command: For Astro projects, Netlify usually detects the correct build command automatically. Astro builds your site into a ‘dist’ folder.
- Publish directory: This should also be set to ‘dist’ by default for Astro projects.
You can also set up environment variables or functions if your project needs them, but for a basic Astro site, these are often not needed. Once you’ve checked these settings, click the ‘Deploy’ button.
Step 6: Publish and Preview Your Site
Netlify will now start building your Astro project. This process involves taking your code, running the build command, and preparing your site to be live. This usually takes a minute or two.
When the deployment is finished, Netlify will show a ‘Published’ status. You’ll also see a link to your live site, which will turn green.
Click on this link to open your Astro website in a new tab. Congratulations, your site is now live on the internet!
This entire process makes deploying your Astro sites very straightforward. Netlify handles the building and hosting, so you can focus on creating your content. If you enjoyed this guide and want to learn more advanced Astro topics, consider looking for more in-depth tutorials or courses.
Source: Astro Crash Course #13 – Deploying to Netlify (YouTube)