Report this

What is the reason for this report?

How to deploy webpack-ed static content while container image is build, e. g. after npm build was run

Posted on November 5, 2021

Hello! I am new to DigitalOcean, and yet I was able to finally got working my experimental app just in few hours (mostly due to unexpected limitation of dev databases to only Postgres while I was planning to use MongoDb, otherwise it would be even faster). This is anyway much f***ng faster than inability getting verification call from AWS at all after waiting for 2 days. My app is yet too small to scale out with Spaces, but I want to experiment a bit with following: my static assets webpack-ed during npm build as part of image creation, and what I would like to do is to upload the webpack-ed content to a space 'project-name-staticat path like/static/{config-name}/{git-tag}`, with autogeneration of env image variable like

STATIC_CONTENT_PATH="/static/${configuration_name}/${app_git_tag-or-other-deployment-id-value}"

where ${configuration_name} is my CONFIGURATION_NAME variable I set at app component where repo content is deployed to.

Binding prefix like /static/* to a Space looks to be the easiest part of all this, I guess?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hey!

Here’s a guide on how to achieve reliable deployment of your webpack-ed static content to a DigitalOcean Space during the container image build process:

1. Dockerfile Setup:

  • Multi-Stage Build: Employ a multi-stage build process in your Dockerfile to optimize efficiency and reduce the image size.

Here’s an example Dockerfile:

# Stage 1: Build stage
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY ./ ./
RUN npm run build

# Stage 2: Production stage
FROM nginx:alpine
COPY --from=build-stage /app/build /usr/share/nginx/html # Copy build output

# -- Optional: Configure Nginx as needed --

2. Workflow Automation (CI/CD)

  • Integrate with CI/CD: Set up a CI/CD pipeline (e.g., GitHub Actions, GitLab CI/CD) to automate the build and deployment process.

Here’s a basic example using GitHub Actions:

name: Build and Deploy to App Platform and Spaces

on:
  push:
    branches: [ main ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Build Docker Image
        run: docker build -t my-image:latest .
      - name: Login to DigitalOcean Container Registry
        uses: docker/login-action@v2
        with:
          registry: registry.digitalocean.com
          username: ${{ secrets.DO_ACCESS_TOKEN }}
          password: ${{ secrets.DO_ACCESS_TOKEN }}
      - name: Push to Container Registry
        run: docker push my-image:latest
      - name: Deploy to App Platform
        uses: digitalocean/action-doctl@v2
        with:
          token: ${{ secrets.DO_ACCESS_TOKEN }}
          app: ${{ secrets.DO_APP_ID }} # Replace with your App ID
          spec: ${{ path/to/your/app.yaml }}
      - name: Upload to Spaces
        uses: digitalocean/spaces-upload-action@v1
        with:
          spaces_access_id: ${{ secrets.DO_SPACES_ACCESS_ID }}
          spaces_secret_key: ${{ secrets.DO_SPACES_SECRET_KEY }}
          space: ${{ secrets.DO_SPACES_NAME }}
          region: ${{ secrets.DO_SPACES_REGION }}
          source_dir: build/  # Output from Webpack
          dest_dir: /static/${{ github.event.repository.name }}/${{ github.sha }}

Explanation:

  • Environment Variables: Store your DigitalOcean API keys, Space details, app ID, etc. as secrets within your CI/CD pipeline.
  • Spaces Upload Action: The ‘spaces-upload-action’ simplifies the upload to your Space.
  • Dynamic Path: The dest_dir ensures assets from each build are in unique folders, helping with versioning if needed.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.