Automate Bun Server Deployments on Ubuntu: A Docker & GitLab CI/CD Tutorial
Step-by-Step Guide to Automating Bun Server Deployments on Ubuntu using Docker & GitLab CI/CD
Introduction:
Bun.sh has taken the JavaScript world by storm with its lightning-fast performance and comprehensive toolkit. But deploying Bun.sh apps to production, especially if you're new to the game, can seem daunting. Don't worry, we've got your back! This guide will walk you through deploying your Bun.sh application to an Ubuntu server using GitLab CI/CD and Docker, streamlining the process for smooth sailing.
Why Bun.sh with GitLab CI/CD and Docker?
Bun.sh shines with its hot reloading, blazing-fast builds, and built-in tooling. But traditional deployment methods don't always play nice with its unique architecture. This guide tackles that challenge, leveraging GitLab CI/CD for automated deployments and Docker for streamlined containerization.
Here's What You'll Need:
A GitLab Account: Manage your codebase and run automated deployment pipelines
Ubuntu Server with Docker: Get a free instance on Oracle Cloud and follow Docker’s official installation guide.
GitLab CI/CD: Let GitLab handle your automated deployments.
GitLab Container Registry: Store and manage your Docker images securely.
Steps:
Create a Repository:
Set up a Git repository for your Bun.sh project.
Develop your Bun application code.
Dockerfile:
- Include a Dockerfile in your repository with the following contents:
FROM oven/bun:1
# Copy the lock and package file
COPY bun.lockb package.json ./
# Install dependencies
RUN bun install --frozen-lockfile
# Copy your source code
COPY src ./src
EXPOSE 8080
CMD ["bun", "start"]
CI/CD Pipeline:
Create a
.gitlab-ci.yml
file in the root directory of your project. This file defines the CI/CD pipeline.Use the following basic example for building and deploying a Docker image:
# Define stages
stages:
- build
- deploy
# Set variables
variables:
IMAGE_TAG: "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
# Build job configuration
build-job:
stage: build
image: docker:20.10.16
services:
- docker:20.10.16-dind
script:
# Log in to docker registry
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
# Build and push image
- docker build -t "$IMAGE_TAG" .
- docker push "$IMAGE_TAG"
# Deployment configuration
deploy:
stage: deploy
before_script:
# Set appropriate permissions for SSH key
- chmod 400 $SSH_KEY
script:
# Execute remote deployment script over SSH
- |
ssh -o StrictHostKeyChecking=no -i $SSH_KEY ubuntu@$SERVER_IP << EOF
# Stop and remove existing container if running
sudo docker stop bun-server || true &&
sudo docker rm bun-server || true &&
# Run new container with specified configurations
sudo docker run -d --name bun-server -p 8020:8080 --restart always $IMAGE_TAG
EOF
GitLab automatically provides certain variables like CI_REGISTRY_USER
, CI_REGISTRY_PASSWORD
, CI_REGISTRY
, CI_COMMIT_REF_SLUG
, and CI_REGISTRY_IMAGE
. Ensure these variables are properly configured in your GitLab project settings.
To securely access the deployment server, you need to add the SSH private key to your GitLab CI/CD settings. You can follow this tutorial to learn how to add the SSH key.
Commit and Push Changes
Commit the
.gitlab-ci.yml
file to your repository and push the changes to GitLab.git add .gitlab-ci.yml git commit -m "Add CI/CD pipeline configuration" git push origin main
Monitor Pipeline Execution
Once the changes are pushed to GitLab, the CI/CD pipeline will automatically trigger. You can monitor the pipeline's progress and view the logs in the GitLab dashboard.
Remember:
This guide provides a basic setup. Consider your specific needs and explore advanced configurations later.
Bun.sh and GitLab offers extensive documentation and resources for deeper dives.
You can find further information by following this GitHub repository: https://gitlab.com/probir-sarkar/bun-server
Now, sit back, relax, and enjoy the smooth sailing of Bun.sh deployments with GitLab CI/CD and Docker! Remember, this is just the beginning of your Bun.sh adventure. Keep exploring and unleashing its full potential!
Reference: