Flutter Web brings the power of Flutter's declarative approach to web development, enabling developers to construct high-quality web applications. This open-source framework utilizes the Dart programming language, offering a seamless transition for developers familiar with Flutter for mobile development.
In this post, we'll walk you through the step-by-step process of deploying your Flutter Web projects using Docker and GitHub Actions.
Prerequisites
Before you start, ensure you have the following:
- Docker installed on your local machine. We will need to test our script in the local environment before deploying.
- A Docker Hub account, as we will deploy our images to Docker Hub. You can register here.
Setting Up a Cloud Server
If you don't already have a cloud server, you can set one up. In this tutorial, we'll use Digital Ocean for cloud hosting, but feel free to use your preferred cloud provider like AWS, GCP, etc.
Step 1: Set Up Digital Ocean
You can get a new Digital Ocean account here. Digital Ocean offers $200 USD in credits for your first signup.
After registering successfully, on the dashboard, click the Create Droplet button. A droplet is a virtual computer with a public IP address that is hosted by Digital Ocean.
Step 2: Configure Your Droplet
Here are my recommended configurations:
- Region: Singapore (Choose the region closest to your location)
- Image: Ubuntu (Ubuntu is a common Linux distribution)
- Size: Choose the cheapest option (1 CPU, 1GB RAM, $6/month)
- Authentication Method: SSH (Set up SSH for remote access)
Creating a New Flutter Web Project
To create your first Flutter web project, use the following command:
Navigate to your project directory and run:
You should now see your web app running on Chrome.
Deploying the Flutter Web Project
Before we start, let's discuss how to deploy our Flutter Web project.
If you trigger the build process using this command:
You will see a build/web/index.html file in the output directory. This directory contains the contents of your webpage, with index.html as the entry point.
Step 1: Setting Up Nginx
Create file nginx.conf, copy and paste the following configuration:
Step 2: Writing a Dockerfile
If you're not familiar with Docker, you can learn more here.
Now, in your project directory, create a file called Dockerfile.
Here's a breakdown of the Dockerfile instructions:
- Build Step: Set up the Flutter SDK and build the Flutter web project.
- Run Step: Install Nginx, copy the build output, and start Nginx.
Dockerfile example
Step 3: Testing on Your Local Machine
In your project directory, build the Docker image:
Once the build process is complete, run the Docker container:
Visit localhost:3000 to see your web app.
Configuring GitHub Actions for Continuous Deployment
GitHub Actions allows us to automate the build and deployment process.
Step 1: Allow GitHub Actions to Perform SSH
To enable GitHub Actions to connect to your cloud server via SSH, generate an SSH key on your server:
Next, retrieve your private key:
Copy the output, then open your GitHub repository settings, and navigate to Secrets and Variables.
Create a new repository secret and enter the following information:
- SSH_PRIVATE_KEY: Your private SSH key
- SSH_HOST: The public IP address of your cloud server
- DOCKER_REGISTRY_USERNAME: Your Docker Hub username
- DOCKER_REGISTRY_PASSWORD: Your Docker Hub password
Step 2: Creating the GitHub Workflow
In your project directory, create a .github/workflows/ci.yaml file.
Here's an example ci.yaml file:
Now, let's push the code to Github. Then we should able to see the our workflow available in tab Actions
Start your workflow, and I wish you best luck !
Final Thoughts
Thank you for reading! I hope this guide helps you deploy your Flutter Web projects with Docker and GitHub Actions. Stay tuned for more tutorials on optimizing your web deployment with CDN integration.