Last active
July 18, 2024 11:12
-
-
Save nikdoof/b7349437de028e4b74e10be57b02bccf to your computer and use it in GitHub Desktop.
Docker/Hugo/Nginx - Building a Hugo site into a simple Nginx container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build - Build Container | |
"on": | |
push: | |
branches: | |
- main | |
jobs: | |
docker: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: downcase REPO | |
run: | | |
echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV} | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to GHCR | |
uses: docker/login-action@v3 | |
if: github.event_name != 'pull_request' | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push | |
id: docker_build | |
uses: docker/build-push-action@v6 | |
with: | |
platforms: linux/amd64 | |
push: true | |
build-args: | | |
BUILDKIT_CONTEXT_KEEP_GIT_DIR=true | |
GH_ACCESS_TOKEN=${{ secrets.GH_ACCESS_TOKEN }} | |
tags: | | |
ghcr.io/${{ env.REPO }}:${{ github.ref_name }} | |
ghcr.io/${{ env.REPO }}:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
server_name _; | |
root /usr/share/nginx/html; | |
index index.html index.htm; | |
location ~ \.(jpg|jpeg|gif|png|webp)$ { | |
add_header Cache-Control "public, s-maxage=7776000, max-age=86400"; | |
} | |
location ~ \.(css|js)$ { | |
add_header Cache-Control "public, max-age=31536000"; | |
} | |
error_page 404 /404.html; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Builder | |
FROM ghcr.io/hugomods/hugo:dart-sass AS build | |
ARG GH_ACCESS_TOKEN | |
WORKDIR /tmp/hugo | |
COPY . . | |
RUN apk add --no-cache make && git config --global url.https://${GH_ACCESS_TOKEN}@github.com/.insteadOf https://github.com/ && make build | |
# Ngnix container with site | |
FROM nginx:alpine-slim | |
WORKDIR /usr/share/nginx/html | |
COPY docker/gzip.conf /etc/nginx/conf.d | |
COPY docker/default.conf /etc/nginx/conf.d | |
COPY --from=build /tmp/hugo/public . | |
EXPOSE 80/tcp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gzip on; | |
gzip_disable "msie6"; | |
gzip_vary on; | |
gzip_proxied any; | |
gzip_comp_level 6; | |
gzip_buffers 16 8k; | |
gzip_http_version 1.1; | |
gzip_min_length 256; | |
gzip_types application/atom+xml application/geo+json application/javascript application/x-javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/xhtml+xml application/xml font/eot font/otf font/ttf image/svg+xml text/css text/javascript text/plain text/xml; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: serve | |
serve: | |
hugo serve -D --gc -w -F --disableFastRender --printUnusedTemplates --printPathWarnings --bind 0.0.0.0 | |
.PHONY: build | |
build: | |
HUGO_ENVIRONMENT=production HUGO_ENV=production hugo --gc --minify --cleanDestinationDir --logLevel INFO | |
.PHONY: clean | |
clean: | |
rm -rf public resources |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment