Nasser Setti

Streamline Your Deployment Process with App Platform, Container Registry, and GitHub/GitLab Workflow

Published 30-12-2024

Last Modified 6 days ago

Whenever I attempted to deploy an application, I encountered two primary challenges:
  • How could I accomplish the task in a straightforward manner?
  • How could I automate the process?
This combination, which is commonly used in DigitalOcean resources, addresses these concerns.
By employing this combination, your workflow will undoubtedly become more efficient.
I will provide you with the simplest method to implement this process:
Configure your Docker image:
Ensure that your Docker image is properly configured.
# Dockerfile Nest.js Application
# Production stage
FROM node:lts
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install only production dependencies
RUN npm ci --only=production
# Copy built application from builder stage
COPY --from=builder /app/dist ./dist
# Expose the application port
EXPOSE 3000
# Start the application
CMD ["npm", "run", "start:prod"]
Set up your workflow to include an option to upload the image to the DO container registry:
Configure your workflow to include an option to upload the image to the DigitalOcean container registry.
name: Build and Deploy NestJS
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
env:
REGISTRY: "registry.digitalocean.com/your-registry-name"
IMAGE_NAME: "your-app-name"
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Build application
run: npm run build
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Log in to DigitalOcean Container Registry
run: doctl registry login --expiry-seconds 600
- name: Build container image
run: docker build -t $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) -t $(echo $REGISTRY)/$(echo $IMAGE_NAME):latest .
- name: Push image to DigitalOcean Container Registry
run: |
docker push $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
docker push $(echo $REGISTRY)/$(echo $IMAGE_NAME):latest
Create your application and specify the DO container registry as its source:
Create your application and provide it with the DigitalOcean container registry as its source. You will be prompted to select the desired container and images for your application.More informations can be found [https://docs.digitalocean.com/products/app-platform/](https://docs.digitalocean.com/products/app-platform/)
Enable the “Upload new image” trigger:
Activate the “Upload new image” trigger.
Congratulations! You have successfully deployed your application and automated your workflow simultaneously.