Skip to content

Commit c458170

Browse files
raspdevpyraspdevpy
authored andcommitted
Merge pull request #102 from raspdevpy/docker
Add deployment to docker
1 parent b3fb3c5 commit c458170

8 files changed

Lines changed: 174 additions & 30 deletions

File tree

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
**/*
2+
!package.json
3+
!package-lock.json
4+
!guide
5+
!nginx.conf

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DOCKER_REGISTRY=registry.mydomain.com

.github/workflows/main.yml

Lines changed: 0 additions & 29 deletions
This file was deleted.

.github/workflows/release.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Build and Release Docker Image
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
tags:
8+
- 'v*'
9+
pull_request:
10+
branches:
11+
- main
12+
13+
env:
14+
REGISTRY: ${{ secrets.DOCKER_REGISTRY || 'docker.io' }}
15+
IMAGE_NAME: raspdevpy/${{ github.event.repository.name }}
16+
17+
jobs:
18+
build-and-push:
19+
runs-on: ubuntu-latest
20+
permissions:
21+
contents: read
22+
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v4
26+
with:
27+
fetch-depth: 0
28+
29+
- name: Set up Docker Buildx
30+
uses: docker/setup-buildx-action@v3
31+
32+
- name: Log in to Private Registry
33+
if: ${{ github.event_name != 'pull_request' }}
34+
uses: docker/login-action@v3
35+
with:
36+
registry: ${{ env.REGISTRY }}
37+
username: ${{ secrets.DOCKER_USERNAME }}
38+
password: ${{ secrets.DOCKER_PASSWORD }}
39+
40+
- name: Extract metadata
41+
id: meta
42+
uses: docker/metadata-action@v5
43+
with:
44+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
45+
tags: |
46+
type=ref,event=branch
47+
type=ref,event=pr,prefix=pr-
48+
type=semver,pattern={{version}}
49+
type=semver,pattern={{major}}.{{minor}}
50+
type=semver,pattern={{major}}
51+
type=raw,value=latest,enable={{is_default_branch}}
52+
53+
- name: Build and push Docker image
54+
uses: docker/build-push-action@v5
55+
timeout-minutes: 20
56+
with:
57+
context: .
58+
platforms: linux/amd64
59+
push: ${{ github.event_name != 'pull_request' }}
60+
tags: ${{ steps.meta.outputs.tags }}
61+
labels: ${{ steps.meta.outputs.labels }}
62+
build-args: |
63+
BUILD_DATE=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
64+
GIT_COMMIT=${{ github.sha }}
65+
VERSION=${{ steps.meta.outputs.version }}
66+
cache-from: ${{ github.event_name == 'pull_request' && 'type=local,src=/tmp/.buildx-cache' || format('type=registry,ref={0}/{1}:cache', env.REGISTRY, env.IMAGE_NAME) }}
67+
cache-to: ${{ github.event_name == 'pull_request' && 'type=local,dest=/tmp/.buildx-cache-new,mode=max' || format('type=registry,ref={0}/{1}:cache,mode=max', env.REGISTRY, env.IMAGE_NAME) }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ ignore/*
99
.DS_Store
1010
guide/.vuepress/public/images/*
1111
.vscode
12-
OnTemplate.md
12+
OnTemplate.md
13+
.env

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Stage 1 — build the static assets
2+
FROM node:20-alpine3.21 AS builder
3+
WORKDIR /app
4+
5+
COPY package*.json ./
6+
RUN npm ci
7+
8+
COPY . .
9+
# adjust the build command if your project uses a different script
10+
RUN npm run build
11+
12+
# Stage 2 — serve with nginx
13+
FROM nginx:1.25-alpine
14+
ARG BUILD_DIR=guide/.vuepress/dist
15+
16+
# Remove default config and add a simple SPA-friendly config
17+
COPY nginx.conf /etc/nginx/conf.d/default.conf
18+
19+
# Copy built static files from the builder stage
20+
COPY --from=builder /app/${BUILD_DIR} /usr/share/nginx/html
21+
22+
EXPOSE 80
23+
CMD ["nginx", "-g", "daemon off;"]

Makefile

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
include .env
2+
3+
# Variables
4+
# DOCKER_REGISTRY ?=
5+
DOCKER_REPO ?= raspdevpy/$(shell basename `git remote get-url origin`)
6+
VERSION ?= $(shell git describe --tags --always --dirty)
7+
LATEST_TAG := latest
8+
BUILD_DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
9+
GIT_COMMIT := $(shell git rev-parse HEAD)
10+
11+
# Docker image tags
12+
IMAGE_TAG := $(DOCKER_REGISTRY)/$(DOCKER_REPO):$(VERSION)
13+
LATEST_IMAGE := $(DOCKER_REGISTRY)/$(DOCKER_REPO):$(LATEST_TAG)
14+
15+
.PHONY: help build push release clean login test
16+
17+
help: ## Show this help message
18+
@echo "Available targets:"
19+
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " %-15s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
20+
21+
build: ## Build Docker image
22+
@echo "Building Docker image: $(IMAGE_TAG)"
23+
docker build \
24+
--build-arg BUILD_DATE=$(BUILD_DATE) \
25+
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
26+
--build-arg VERSION=$(VERSION) \
27+
-t $(IMAGE_TAG) \
28+
-t $(LATEST_IMAGE) \
29+
.
30+
@echo "Built: $(IMAGE_TAG)"
31+
@echo "Built: $(LATEST_IMAGE)"
32+
33+
test: ## Test the Docker image
34+
@echo "Testing Docker image: $(IMAGE_TAG)"
35+
# docker run --rm $(IMAGE_TAG) curl http://localhost:80/index.html
36+
37+
test-interactive: ## Run an interactive shell in the Docker image for testing
38+
@echo "Starting interactive shell in Docker image: $(IMAGE_TAG)"
39+
docker run --rm -it $(IMAGE_TAG) sh
40+
41+
login: ## Login to Docker registry
42+
@echo "Logging into Docker registry: $(DOCKER_REGISTRY)"
43+
docker login $(DOCKER_REGISTRY)
44+
45+
push: ## Push Docker image to registry
46+
@echo "Pushing Docker image: $(IMAGE_TAG)"
47+
docker push $(IMAGE_TAG)
48+
docker push $(LATEST_IMAGE)
49+
@echo "Pushed: $(IMAGE_TAG)"
50+
@echo "Pushed: $(LATEST_IMAGE)"
51+
52+
release: build test push ## Build, test and push Docker image
53+
@echo "Release completed for version: $(VERSION)"
54+
55+
clean: ## Remove local Docker images
56+
@echo "Cleaning up local Docker images"
57+
-docker rmi $(IMAGE_TAG) 2>/dev/null || true
58+
-docker rmi $(LATEST_IMAGE) 2>/dev/null || true
59+
60+
# CI/CD helpers
61+
ci-info: ## Show build information for CI
62+
@echo "VERSION=$(VERSION)"
63+
@echo "IMAGE_TAG=$(IMAGE_TAG)"
64+
@echo "BUILD_DATE=$(BUILD_DATE)"
65+
@echo "GIT_COMMIT=$(GIT_COMMIT)"

nginx.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server {
2+
listen 80;
3+
server_name _;
4+
5+
root /usr/share/nginx/html;
6+
index index.html;
7+
8+
location / {
9+
try_files $uri $uri/ /index.html;
10+
}
11+
}

0 commit comments

Comments
 (0)