1
Fork 0

Update workflow using Dockerfile. (#1003)

This commit is contained in:
Googol Lee 2024-08-03 23:17:54 +02:00 committed by GitHub
parent 4d9ce30583
commit ebcbe6c057
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 155 additions and 151 deletions

View File

@ -17,6 +17,7 @@ Makefile
photos_path photos_path
screenshots screenshots
storage storage
photoview.db*
ui/node_modules/ ui/node_modules/
ui/build/ ui/build/

View File

@ -1,2 +0,0 @@
PHOTOVIEW_DATABASE_DRIVER=mysql
PHOTOVIEW_MYSQL_URL='photoview:photosecret@tcp(127.0.0.1)/photoview_test'

View File

@ -1,2 +0,0 @@
PHOTOVIEW_DATABASE_DRIVER=postgres
PHOTOVIEW_POSTGRES_URL=postgres://photoview:photosecret@127.0.0.1/photoview_test

View File

@ -1,2 +0,0 @@
PHOTOVIEW_DATABASE_DRIVER=sqlite
PHOTOVIEW_SQLITE_PATH=photoview_test.db

View File

@ -9,12 +9,11 @@ on:
jobs: jobs:
test-api: test-api:
name: Test API name: Test API
runs-on: ubuntu-20.04 runs-on: ubuntu-latest
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
database: ['mysql', 'postgres', 'sqlite'] database: ['sqlite', 'mysql', 'postgres']
services: services:
mariadb: mariadb:
@ -46,109 +45,68 @@ jobs:
ports: ports:
- 5432:5432 - 5432:5432
defaults:
run:
working-directory: api
steps: steps:
- name: Check out code into the Go module directory - name: Set up Docker Buildx
uses: actions/checkout@v4 uses: docker/setup-buildx-action@v3
- name: Fetch branches - name: Checkout repo
run: git fetch --all uses: actions/checkout@v4
- name: Set up Go - name: Build test image
uses: actions/setup-go@v5 uses: docker/build-push-action@v6
id: go with:
with: pull: true
go-version-file: ${{ github.workspace }}/api/go.mod push: false
cache: false load: true
target: api
tags: photoview/api
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Cache Go dependencies - name: Test
uses: actions/cache@v2 run: |
with: docker run --name test --network host \
path: ~/go/pkg/mod -e PHOTOVIEW_DATABASE_DRIVER=${{ matrix.database }} \
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} -e PHOTOVIEW_MYSQL_URL='photoview:photosecret@tcp(localhost:3306)/photoview_test' \
restore-keys: | -e PHOTOVIEW_POSTGRES_URL='postgres://photoview:photosecret@localhost:5432/photoview_test' \
${{ runner.os }}-go- -e PHOTOVIEW_SQLITE_PATH=/tmp/photoview.db \
photoview/api \
go test ./... -v -database -filesystem -p 1 -coverprofile=coverage.txt -covermode=atomic
docker cp test:/app/api/coverage.txt ./api/
- name: Get C dependencies and 3rd-party tools - name: Upload coverage
run: | uses: codecov/codecov-action@v4
sudo add-apt-repository ppa:strukturag/libheif with:
sudo add-apt-repository ppa:strukturag/libde265 flags: api-${{ matrix.database }}
sudo apt-get update
sudo apt-get install -y libdlib-dev libblas-dev libatlas-base-dev liblapack-dev libjpeg-turbo8-dev libheif-dev ffmpeg exiftool
- name: Get GO dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Build
run: go build -v .
- name: Configure MySQL
if: ${{ matrix.database == 'mysql' }}
run: |
cp ../.github/mysql.testing.env testing.env
- name: Configure Postgres
if: ${{ matrix.database == 'postgres' }}
run: |
cp ../.github/postgres.testing.env testing.env
- name: Configure Sqlite
if: ${{ matrix.database == 'sqlite' }}
run: |
touch photoview_test.db
cp ../.github/sqlite.testing.env testing.env
- name: Test
run: go test ./... -v -database -filesystem -p 1 -coverprofile=coverage.txt -covermode=atomic
- name: Upload coverage
uses: codecov/codecov-action@v1
with:
flags: api
test-ui: test-ui:
name: Test UI name: Test UI
runs-on: ubuntu-latest runs-on: ubuntu-latest
defaults:
run:
working-directory: ui
strategy:
matrix:
node-version: [18.x]
steps: steps:
- uses: actions/checkout@v2 - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Cache NPM dependencies - name: Checkout repo
uses: actions/cache@v2 uses: actions/checkout@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Use Node.js ${{ matrix.node-version }} - name: Build test image
uses: actions/setup-node@v1 uses: docker/build-push-action@v6
with: with:
node-version: ${{ matrix.node-version }} pull: true
push: false
load: true
target: ui
tags: photoview/ui
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Install dependencies - name: Test
run: npm clean-install run: |
docker run --name test photoview/ui npm run test:ci
docker cp test:/app/ui/coverage ./ui/
- name: Test - name: Upload coverage
run: npm run test:ci uses: codecov/codecov-action@v4
with:
- name: Upload coverage flags: ui
uses: codecov/codecov-action@v1
with:
flags: ui

3
.gitignore vendored
View File

@ -4,8 +4,7 @@ cache/
/media_cache/ /media_cache/
/api/media_cache/ /api/media_cache/
/photos_path /photos_path
photoview.db photoview.db*
photoview.db-*
.env .env
testing.env testing.env

View File

@ -2,7 +2,7 @@
FROM --platform=${BUILDPLATFORM:-linux/amd64} node:18 AS ui FROM --platform=${BUILDPLATFORM:-linux/amd64} node:18 AS ui
# See for details: https://github.com/hadolint/hadolint/wiki/DL4006 # See for details: https://github.com/hadolint/hadolint/wiki/DL4006
SHELL ["/bin/bash", "-o", "pipefail", "-c"] SHELL ["/bin/bash", "-euo", "pipefail", "-c"]
ARG REACT_APP_API_ENDPOINT ARG REACT_APP_API_ENDPOINT
ENV REACT_APP_API_ENDPOINT=${REACT_APP_API_ENDPOINT} ENV REACT_APP_API_ENDPOINT=${REACT_APP_API_ENDPOINT}
@ -23,22 +23,21 @@ ARG COMMIT_SHA
ENV COMMIT_SHA=${COMMIT_SHA:-} ENV COMMIT_SHA=${COMMIT_SHA:-}
ENV REACT_APP_BUILD_COMMIT_SHA=${COMMIT_SHA:-} ENV REACT_APP_BUILD_COMMIT_SHA=${COMMIT_SHA:-}
# Download dependencies
COPY ui /app/ui
WORKDIR /app/ui WORKDIR /app/ui
RUN npm ci --omit=dev --ignore-scripts \
# Build frontend COPY ui/package.json ui/package-lock.json /app/ui
&& npm run build -- --base=$UI_PUBLIC_URL RUN npm ci
COPY ui/ /app/ui
RUN npm run build -- --base=$UI_PUBLIC_URL
### Build API ### ### Build API ###
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.22-bookworm AS api FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.22-bookworm AS api
ARG TARGETPLATFORM ARG TARGETPLATFORM
# See for details: https://github.com/hadolint/hadolint/wiki/DL4006 # See for details: https://github.com/hadolint/hadolint/wiki/DL4006
SHELL ["/bin/bash", "-o", "pipefail", "-c"] SHELL ["/bin/bash", "-euo", "pipefail", "-c"]
COPY scripts /app/scripts
COPY api /app/api
WORKDIR /app/api WORKDIR /app/api
ENV GOPATH="/go" ENV GOPATH="/go"
@ -46,9 +45,13 @@ ENV PATH="${GOPATH}/bin:${PATH}"
ENV CGO_ENABLED=1 ENV CGO_ENABLED=1
# Download dependencies # Download dependencies
COPY scripts/*.sh /app/scripts/
RUN chmod +x /app/scripts/*.sh \ RUN chmod +x /app/scripts/*.sh \
&& source /app/scripts/set_compiler_env.sh \
&& /app/scripts/install_build_dependencies.sh \ && /app/scripts/install_build_dependencies.sh \
&& /app/scripts/install_runtime_dependencies.sh
COPY api/go.mod api/go.sum /app/api/
RUN source /app/scripts/set_compiler_env.sh \
&& go env \ && go env \
&& go mod download \ && go mod download \
# Patch go-face # Patch go-face
@ -56,38 +59,29 @@ RUN chmod +x /app/scripts/*.sh \
# Build dependencies that use CGO # Build dependencies that use CGO
&& go install \ && go install \
github.com/mattn/go-sqlite3 \ github.com/mattn/go-sqlite3 \
github.com/Kagami/go-face \ github.com/Kagami/go-face
# Build api source
&& go build -v -o photoview .
### Build dev image for UI ### COPY api /app/api
FROM ui AS dev-ui
### Build dev image for API ###
FROM api AS dev-api
RUN source /app/scripts/set_compiler_env.sh \ RUN source /app/scripts/set_compiler_env.sh \
&& /app/scripts/install_runtime_dependencies.sh \ && go build -v -o photoview .
## Install dev tools
&& apt update \
&& apt install -y reflex sqlite3
### Build release image ### ### Build release image ###
FROM --platform=${BUILDPLATFORM:-linux/amd64} debian:bookworm-slim AS release FROM --platform=${BUILDPLATFORM:-linux/amd64} debian:bookworm-slim AS release
ARG TARGETPLATFORM ARG TARGETPLATFORM
# See for details: https://github.com/hadolint/hadolint/wiki/DL4006 # See for details: https://github.com/hadolint/hadolint/wiki/DL4006
SHELL ["/bin/bash", "-o", "pipefail", "-c"] SHELL ["/bin/bash", "-euo", "pipefail", "-c"]
COPY scripts/install_runtime_dependencies.sh /app/scripts/ COPY scripts/install_runtime_dependencies.sh /app/scripts/
RUN chmod +x /app/scripts/install_runtime_dependencies.sh \
# Create a user to run Photoview server # Create a user to run Photoview server
RUN groupadd -g 999 photoview \ && groupadd -g 999 photoview \
&& useradd -r -u 999 -g photoview -m photoview \ && useradd -r -u 999 -g photoview -m photoview \
# Required dependencies # Required dependencies
&& chmod +x /app/scripts/*.sh \
&& /app/scripts/install_runtime_dependencies.sh && /app/scripts/install_runtime_dependencies.sh
WORKDIR /home/photoview WORKDIR /home/photoview
COPY api/data /app/data COPY api/data /app/data
COPY --from=ui /app/ui/dist /app/ui COPY --from=ui /app/ui/dist /app/ui
COPY --from=api /app/api/photoview /app/photoview COPY --from=api /app/api/photoview /app/photoview

View File

@ -33,7 +33,8 @@ Password: **demo**
- [Getting started — Setup with Docker](#getting-started--setup-with-docker) - [Getting started — Setup with Docker](#getting-started--setup-with-docker)
- [Advanced setup](#advanced-setup) - [Advanced setup](#advanced-setup)
- [Contributing](#contributing) - [Contributing](#contributing)
- [Set up development environment](#set-up-development-environment) - [Set up Docker development environment](#set-up-docker-development-environment)
- [Set up local development environment](#set-up-local-development-environment)
- [Sponsors](#sponsors) - [Sponsors](#sponsors)
## Main features ## Main features
@ -182,12 +183,20 @@ We recommend to use Docker development environment. If Docker environment doesn'
It may take a long time to build dependencies when launching servers first time. It may take a long time to build dependencies when launching servers first time.
```sh ```sh
$ docker compose -f dev-compose.yaml build dev-ui dev-api # Build images for development $ docker compose -f dev-compose.yaml build # Build images for development
$ docker compose -f dev-compose.yaml up dev-api dev-ui # Run API and UI servers $ docker compose -f dev-compose.yaml up # Launch API and UI servers
``` ```
The graphql playground can now be accessed at [localhost:4001](http://localhost:4001). The site can now be accessed at [localhost:1234](http://localhost:1234). Both servers will be relaunched after the code is changed. The graphql playground can now be accessed at [localhost:4001](http://localhost:4001). The site can now be accessed at [localhost:1234](http://localhost:1234). Both servers will be relaunched after the code is changed.
By default, it uses sqlite3 as database. To run servers with other database, please update `PHOTOVIEW_DATABASE_DRIVER` value in `dev-compose.yaml` file and run:
```sh
$ docker compose -f dev-compose.yaml --profile mysql up # Run with mysql database
or
$ docker compose -f dev-compose.yaml --profile postgres up # Run with postgresql database
```
### Start API server with Docker ### Start API server with Docker
If you don't want to depend on Docker Compose but only Docker, you can launch server as below. If you don't want to depend on Docker Compose but only Docker, you can launch server as below.
@ -195,25 +204,31 @@ If you don't want to depend on Docker Compose but only Docker, you can launch se
It may take a long time to build dependencies when launching servers first time. It may take a long time to build dependencies when launching servers first time.
```sh ```sh
$ docker build --target dev-api -t photoview-api . # Build image for development $ docker build --target api -t photoview/api . # Build image for development
$ cp api/example.env api/.env $ docker run --rm -it -v `pwd`:/app --network host --env-file api/example.env photoview/api \
$ docker run --rm -it -v `pwd`:/app --network host photoview-api # Monitor source code and (re)launch API server reflex -g '*.go' -s -- go run . # Monitor source code and (re)launch API server
``` ```
The graphql playground can now be accessed at [localhost:4001](http://localhost:4001). The graphql playground can now be accessed at [localhost:4001](http://localhost:4001).
> [!NOTE]
> The server runs on the host network as `--network host` flag. It's easy to communicate between API server and UI server. If you don't want to do that, please check [Docker Network](https://docs.docker.com/network/) to create a new network to run servers.
### Start UI server with Docker ### Start UI server with Docker
It may take a long time to build dependencies when launching servers first time. It may take a long time to build dependencies when launching servers first time.
```sh ```sh
$ docker build --target dev-ui -t photoview-ui . $ docker build --target ui -t photoview/ui . # Build image for development
$ cp ./ui/example.env ./ui/.env $ docker run --rm -it -v `pwd`:/app --network host --env-file ui/example.env photoview/ui \
$ docker run --rm -it -v `pwd`:/app --network host photoview-ui # Monitor source code and (re)launch UI server npm run mon # Monitor source code and (re)launch UI server
``` ```
The site can now be accessed at [localhost:1234](http://localhost:1234). The site can now be accessed at [localhost:1234](http://localhost:1234).
> [!NOTE]
> The server runs on the host network as `--network host` flag. It's easy to communicate between API server and UI server. If you don't want to do that, please check [Docker Network](https://docs.docker.com/network/) to create a new network to run servers.
## Set up local development environment ## Set up local development environment
### Install dependencies ### Install dependencies

View File

@ -2,11 +2,11 @@ name: photoview
services: services:
dev-ui: dev-ui:
image: photoview/photoview-ui image: photoview/ui
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
target: dev-ui target: ui
env_file: ui/example.env env_file: ui/example.env
volumes: volumes:
- .:/app:rw - .:/app:rw
@ -20,12 +20,17 @@ services:
npm run mon npm run mon
dev-api: dev-api:
image: photoview/photoview-api image: photoview/api
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
target: dev-api target: api
env_file: api/example.env env_file: api/example.env
environment:
PHOTOVIEW_DATABASE_DRIVER: sqlite # Change to the right database driver
PHOTOVIEW_SQLITE_PATH: api/photoview.db
PHOTOVIEW_MYSQL_URL: photoview:photosecret@tcp(mysql)/photoview_test
PHOTOVIEW_POSTGRES_URL: postgres://photoview:photosecret@postgres:5432/photoview_test
volumes: volumes:
- .:/app:rw - .:/app:rw
ports: ports:
@ -36,3 +41,43 @@ services:
- | - |
source /app/scripts/set_compiler_env.sh source /app/scripts/set_compiler_env.sh
reflex -g '*.go' -s -- go run . reflex -g '*.go' -s -- go run .
mariadb:
image: mariadb:lts
profiles:
- mysql
environment:
MYSQL_DATABASE: photoview_test
MYSQL_USER: photoview
MYSQL_PASSWORD: photosecret
MYSQL_RANDOM_ROOT_PASSWORD: yes
healthcheck:
test:
- CMD
- mariadb-admin
- --user=photoview
- --password=photosecret
- ping
interval: 20s
timeout: 5s
retries: 10
expose:
- "3306"
postgres:
image: postgres:16-alpine
profiles:
- postgres
environment:
POSTGRES_DB: photoview_test
POSTGRES_USER: photoview
POSTGRES_PASSWORD: photosecret
healthcheck:
test:
- CMD
- pg_isready
interval: 20s
timeout: 5s
retries: 10
expose:
- "5432"

View File

@ -38,6 +38,5 @@ apt-get install -y \
libjpeg-dev:${DEBIAN_ARCH} \ libjpeg-dev:${DEBIAN_ARCH} \
libheif-dev:${DEBIAN_ARCH} libheif-dev:${DEBIAN_ARCH}
# Cleanup # Install tools for development
apt-get clean apt-get install -y reflex sqlite3
rm -rf /var/lib/apt/lists/*

View File

@ -1,16 +1,15 @@
#!/bin/bash #!/bin/bash
BUILD_DEPENDS=(gnupg2 gpg)
apt-get update apt-get update
apt-get install -y ${BUILD_DEPENDS[@]} curl libdlib19.1 ffmpeg exiftool libheif1 apt-get install -y curl libdlib19.1 ffmpeg exiftool libheif1
# Install Darktable if building for a supported architecture # Install Darktable if building for a supported architecture
if [ "${TARGETPLATFORM}" = "linux/amd64" ] || [ "${TARGETPLATFORM}" = "linux/arm64" ]; then if [ "${TARGETPLATFORM}" = "linux/amd64" ] || [ "${TARGETPLATFORM}" = "linux/arm64" ]; then
echo 'deb https://download.opensuse.org/repositories/graphics:/darktable/Debian_12/ /' \ echo 'deb [trusted=true] https://download.opensuse.org/repositories/graphics:/darktable/Debian_12/ /' > /etc/apt/sources.list.d/darktable.list
| tee /etc/apt/sources.list.d/graphics:darktable.list # Release key is invalid, just trust the repo
curl -fsSL https://download.opensuse.org/repositories/graphics:/darktable/Debian_12/Release.key \ curl -fsSL https://download.opensuse.org/repositories/graphics:/darktable/Debian_12/Release.key \
| gpg --dearmor | tee /etc/apt/trusted.gpg.d/graphics_darktable.gpg > /dev/null | gpg --dearmor -o /etc/apt/trusted.gpg.d/darktable.gpg
gpg --show-keys --with-fingerprint --dry-run /etc/apt/trusted.gpg.d/darktable.gpg
apt-get update apt-get update
apt-get install -y darktable apt-get install -y darktable