From 2e67879fc0d2f58f28c344b07fdee99db1f2f578 Mon Sep 17 00:00:00 2001 From: Evan Rosson Date: Sun, 26 May 2024 03:31:50 +0000 Subject: [PATCH] build a website docker image; experimental --- .devcontainer/devcontainer.json | 3 +- .github/workflows/build-docker-image.yml | 59 ++++++++++++++++++++++++ www/.dockerignore | 6 +++ www/Dockerfile | 21 +++++++++ www/docker-compose.yml | 9 ++++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/build-docker-image.yml create mode 100644 www/.dockerignore create mode 100644 www/Dockerfile create mode 100644 www/docker-compose.yml diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index c59c7e6b..c6348d6f 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -7,7 +7,8 @@ // Features to add to the dev container. More info: https://containers.dev/features. "features": { "ghcr.io/devcontainers/features/java:1": {}, - "ghcr.io/akhildevelops/devcontainer-features/android-cli:0": {} + "ghcr.io/akhildevelops/devcontainer-features/android-cli:0": {}, + "ghcr.io/devcontainers/features/docker-in-docker:2": {} }, // expose ports from this image to the local network, so my phone can see the expo go server. // `yarn start --host tunnel` would work too, but this adds a dependency on ngrok, and is also much slower. diff --git a/.github/workflows/build-docker-image.yml b/.github/workflows/build-docker-image.yml new file mode 100644 index 00000000..c34b5f60 --- /dev/null +++ b/.github/workflows/build-docker-image.yml @@ -0,0 +1,59 @@ +# https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions +# This docker image is for the website, nothing to do with the app build +on: + push: + branches: ["master"] + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + #IMAGE_NAME: ${{ github.repository }} + IMAGE_NAME: freecbt.erosson.org + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + id-token: write # https://github.com/marketplace/actions/attest-build-provenance + attestations: write + # + steps: + - name: Checkout repository + uses: actions/checkout@v4 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + id: push + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + # careful - context and dockerfile are special, because this dockerfile copies things from a parent dir + context: . + file: www/Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + # This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)." + - name: Generate artifact attestation + uses: actions/attest-build-provenance@v1 + with: + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}} + subject-digest: ${{ steps.push.outputs.digest }} + push-to-registry: true diff --git a/www/.dockerignore b/www/.dockerignore new file mode 100644 index 00000000..e1664a1c --- /dev/null +++ b/www/.dockerignore @@ -0,0 +1,6 @@ +**/node_modules +build +.yarn +.svelte-kit +Dockerfile +docker-compose.yml \ No newline at end of file diff --git a/www/Dockerfile b/www/Dockerfile new file mode 100644 index 00000000..c6388ae9 --- /dev/null +++ b/www/Dockerfile @@ -0,0 +1,21 @@ +# Build a node-based static site +# TODO: more recent versions of node break the build :( figure out why! +FROM node:16 AS build +WORKDIR /app/webapp +COPY www/webapp/package.json /app/webapp/ +RUN yarn --frozen-lockfile +WORKDIR /app +COPY www/package.json /app/ +RUN yarn --frozen-lockfile +# copying from a parent dir requires special docker context; we use translation files from a parent dir +# docker-compose.yml and .github/workflows/build-docker-image.yml both set this context, but be careful if you're trying to do something new +COPY expo47/src/locals /expo47/src/locals +# `.dockerignore` is important to cache this copy properly +COPY www /app/ +# RUN yarn test run +RUN yarn build + +# Run the static site we just built. No Caddyfile or other config, just static files. +# "The default config file simply serves files from /usr/share/caddy" - https://hub.docker.com/_/caddy +FROM caddy:2.8 +COPY --from=build /app/build/ /usr/share/caddy \ No newline at end of file diff --git a/www/docker-compose.yml b/www/docker-compose.yml new file mode 100644 index 00000000..010f6a0b --- /dev/null +++ b/www/docker-compose.yml @@ -0,0 +1,9 @@ +# Run the static site built by the Dockerfile. +# `docker compose up --build`, then visit http://localhost:2015 +services: + main: + build: + context: .. + dockerfile: www/Dockerfile + ports: + - "2015:80"