From edbb8d02840543b7a6bcbb6ca6e49fbaeca79575 Mon Sep 17 00:00:00 2001
From: Timothee Gosselin <timothee@indie.host>
Date: Tue, 15 Feb 2022 11:44:47 +0000
Subject: [PATCH] Use upstream image

---
 .gitlab-ci.yml      |  72 +++++++++++++---------------
 Dockerfile          | 112 ++++++++++----------------------------------
 README.md           |   5 +-
 install/imagemagick |  62 ------------------------
 4 files changed, 60 insertions(+), 191 deletions(-)
 delete mode 100755 install/imagemagick

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 956af63..ad0a67b 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,56 +2,48 @@ variables:
   GIT_SUBMODULE_STRATEGY: recursive
   DOCKER_HUB_REPO: libresh/discourse
 
-test-beta:
-  tags:
-  - build
+.compute_version: &compute_version
+  - if [ "$CHANNEL" == "stable" ]; then export FILTER="grep -v beta"; else export FILTER="grep beta"; fi
+  - export DISCOURSE_VERSION=`wget -qO- https://api.github.com/repos/discourse/discourse/tags | grep name | cut -d'"' -f4 | $FILTER | head -n1`
+
+.test:
   image:
     name: gcr.io/kaniko-project/executor:debug
     entrypoint: [""]
+  variables:
+    DESTINATION: "--no-push"
   script:
-    - export DISCOURSE_VERSION=`wget -qO- https://api.github.com/repos/discourse/discourse/tags | grep name | cut -d'"' -f4 | grep beta | head -n1`
-    - /kaniko/executor --context . --no-push --build-arg DISCOURSE_VERSION=$DISCOURSE_VERSION
+    - *compute_version
+    - export DISCOURSE_MAJOR=`echo $DISCOURSE_VERSION | cut -d"." -f1-3`
+    - /kaniko/executor --context .  ${DESTINATION} --build-arg DISCOURSE_VERSION=$DISCOURSE_VERSION  
   except:
   - master
 
+test-beta:
+  extends: .test
+  variables:
+    CHANNEL: "beta"
+
 test-stable:
-  tags:
-  - build
-  image:
-    name: gcr.io/kaniko-project/executor:debug
-    entrypoint: [""]
-  script:
-    - export DISCOURSE_VERSION=`wget -qO- https://api.github.com/repos/discourse/discourse/tags | grep name | cut -d'"' -f4 | grep -v beta | head -n1`
-    - /kaniko/executor --context . --no-push --build-arg DISCOURSE_VERSION=$DISCOURSE_VERSION
-  except:
-  - master
+  extends: .test
+  variables:
+    CHANNEL: "stable"
 
-build-beta:
-  tags:
-  - build
-  image:
-    name: gcr.io/kaniko-project/executor:debug
-    entrypoint: [""]
-  script:
+.push:
+  extends: .test
+  before_script:
     - cp $DOCKER_SECRET_CONFIG /kaniko/.docker/config.json
-    - export DISCOURSE_VERSION=`wget -qO- https://api.github.com/repos/discourse/discourse/tags | grep name | cut -d'"' -f4 | grep beta | head -n1`
-    - export DISCOURSE_MAJOR=`echo $DISCOURSE_VERSION | cut -d"." -f1-3`
-    - export DESTINATION="--destination ${DOCKER_HUB_REPO}:${DISCOURSE_VERSION} --destination ${DOCKER_HUB_REPO}:${DISCOURSE_MAJOR} --destination ${DOCKER_HUB_REPO}:beta"
-    - /kaniko/executor --context .  ${DESTINATION} --build-arg DISCOURSE_VERSION=$DISCOURSE_VERSION
+    - export DESTINATION="--destination ${DOCKER_HUB_REPO}:${DISCOURSE_VERSION}-${DATE} --destination ${DOCKER_HUB_REPO}:${DISCOURSE_VERSION} --destination ${DOCKER_HUB_REPO}:${DISCOURSE_MAJOR} --destination ${DOCKER_HUB_REPO}:${CHANNEL}"
+  except: []
   only:
   - master
 
-build-stable:
-  tags:
-  - build
-  image:
-    name: gcr.io/kaniko-project/executor:debug
-    entrypoint: [""]
-  script:
-    - cp $DOCKER_SECRET_CONFIG /kaniko/.docker/config.json
-    - export DISCOURSE_VERSION=`wget -qO- https://api.github.com/repos/discourse/discourse/tags | grep name | cut -d'"' -f4 | grep -v beta | head -n1`
-    - export DISCOURSE_MAJOR=`echo $DISCOURSE_VERSION | cut -d"." -f1-2`
-    - export DESTINATION="--destination ${DOCKER_HUB_REPO}:${DISCOURSE_VERSION} --destination ${DOCKER_HUB_REPO}:${DISCOURSE_MAJOR} --destination ${DOCKER_HUB_REPO}:stable"
-    - /kaniko/executor --context .  ${DESTINATION} --build-arg DISCOURSE_VERSION=$DISCOURSE_VERSION
-  only:
-  - master
+push-beta:
+  extends: .push
+  variables:
+    CHANNEL: "beta"
+
+push-stable:
+  extends: .push
+  variables:
+    CHANNEL: "stable"
diff --git a/Dockerfile b/Dockerfile
index b3bd168..2f7e629 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,105 +1,43 @@
-FROM ruby:2.7.5
+ARG from=discourse/base
+ARG tag=slim
+ARG DISCOURSE_VERSION=test-passed
 
-ARG DISCOURSE_VERSION
+FROM $from:$tag
 
 ENV RAILS_ENV=production \
-    DEBIAN_VERSION=bullseye \
-    DISCOURSE_DB_HOST=postgres \
-    DISCOURSE_REDIS_HOST=redis \
     DISCOURSE_SERVE_STATIC_ASSETS=true \
-    DISCOURSE_VERSION=${DISCOURSE_VERSION} \
     EMBER_CLI_PROD_ASSETS=1 \
-    JEMALLOC_NEW=3.6.0 \
-    JEMALLOC_STABLE=5.2.1 \
-    PG_MAJOR=13 \
-    NODE_MAJOR=16 \
     RUBY_GLOBAL_METHOD_CACHE_SIZE=131072 \
     RUBY_GC_HEAP_GROWTH_MAX_SLOTS=40000 \
     RUBY_GC_HEAP_INIT_SLOTS=400000 \
     RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR=1.5 \
-    RUBY_GC_MALLOC_LIMIT=90000000 \
-    BUILD_DEPS="\
-      autoconf \
-      advancecomp \
-      libbz2-dev \
-      libfreetype6-dev \
-      libjpeg-dev \
-      libjpeg-turbo-progs \
-      libtiff-dev \
-      pkg-config"
+    RUBY_GC_MALLOC_LIMIT=90000000
 
-COPY install /tmp/install
-
-RUN ls /tmp/ && ls /tmp/install
 
-RUN curl http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | apt-key add - \
- && echo "deb http://apt.postgresql.org/pub/repos/apt/ ${DEBIAN_VERSION}-pgdg main" | \
-        tee /etc/apt/sources.list.d/postgres.list \
- && curl --silent --location https://deb.nodesource.com/setup_${NODE_MAJOR}.x | bash - \
- && apt-get update && apt-get install -y --no-install-recommends \
-      ${BUILD_DEPS} \
-      ca-certificates \
-      brotli \
-      ghostscript \
-      gsfonts \
-      jpegoptim \
-      liblqr-1-0 \
-      libxml2 \
-      nodejs \
-      optipng \
-      jhead \
-      pngcrush \
-      pngquant \
-      jq \
-      postgresql-client-${PG_MAJOR} \
-      postgresql-contrib-${PG_MAJOR} libpq-dev libreadline-dev \
- && npm install svgo uglify-js -g \
- && mkdir /jemalloc-stable && cd /jemalloc-stable &&\
-      wget https://github.com/jemalloc/jemalloc/releases/download/${JEMALLOC_STABLE}/jemalloc-${JEMALLOC_STABLE}.tar.bz2 &&\
-      tar -xjf jemalloc-${JEMALLOC_STABLE}.tar.bz2 && cd jemalloc-${JEMALLOC_STABLE} && ./configure --prefix=/usr && make && make install &&\
-      cd / && rm -rf /jemalloc-stable \
- && mkdir /jemalloc-new && cd /jemalloc-new &&\
-      wget https://github.com/jemalloc/jemalloc/releases/download/${JEMALLOC_NEW}/jemalloc-${JEMALLOC_NEW}.tar.bz2 &&\
-      tar -xjf jemalloc-${JEMALLOC_NEW}.tar.bz2 && cd jemalloc-${JEMALLOC_NEW} && ./configure --prefix=/usr --with-install-suffix=${JEMALLOC_NEW} && make build_lib && make install_lib &&\
-      cd / && rm -rf /jemalloc-new \
- && gem update --system \
- && gem install bundler --force \
- && rm -rf /usr/local/share/ri/${RUBY_VERSION}/system \
- && /tmp/install/imagemagick \
- # Validate install
- && ruby -Eutf-8 -e "v = \`convert -version\`; %w{png tiff jpeg freetype}.each { |f| unless v.include?(f); STDERR.puts('no ' + f +  ' support in imagemagick'); exit(-1); end }" \
- && addgroup --gid 1000 discourse \
- && adduser --system --uid 1000 --ingroup discourse --shell /bin/bash discourse \
- && cd /home/discourse \
- && mkdir -p tmp/pids \
- && mkdir -p ./tmp/sockets \
- && git clone --branch ${DISCOURSE_VERSION} https://github.com/discourse/discourse.git \
- && chown -R discourse:discourse . \
- && cd /home/discourse/discourse \
- && git remote set-branches --add origin tests-passed \
- && sed -i 's/daemonize true/daemonize false/g' ./config/puma.rb \
- && sed -i "s/msgpack (1.4.1)/msgpack (1.4.2)/" Gemfile.lock \
- && bundle config build.nokogiri --use-system-libraries \
- && bundle install --deployment --verbose --without test --without development --retry 3 --jobs 4 \
- && find /home/discourse/discourse/vendor/bundle -name tmp -type d -exec rm -rf {} + \
- #&& apt-get remove -y --purge ${BUILD_DEPS} \
- #&& apt-get autoremove -y \
- && rm -rf /var/lib/apt/lists/*
+# jq needed to create kubernetes CM
+RUN apt-get update && apt-get install -y jq
 
-RUN mkdir /oxipng-install && cd /oxipng-install &&\
-      wget https://github.com/shssoichiro/oxipng/releases/download/v5.0.0/oxipng-5.0.0-x86_64-unknown-linux-musl.tar.gz &&\
-      tar -xzf oxipng-5.0.0-x86_64-unknown-linux-musl.tar.gz && cd oxipng-5.0.0-x86_64-unknown-linux-musl &&\
-      cp oxipng /usr/local/bin &&\
-      cd / && rm -rf /oxipng-install
+WORKDIR /var/www/discourse
 
-RUN cd /home/discourse/discourse/plugins \
- && for plugin in $(cat /tmp/install/plugin-list); do \
-      git clone $plugin; \
-    done \
- && chown -R discourse:discourse .
+RUN git remote set-branches --add origin ${DISCOURSE_VERSION} &&\
+    git fetch --depth 1 origin ${DISCOURSE_VERSION} &&\
+    sudo -u discourse bundle config --local deployment true &&\
+    sudo -u discourse bundle config --local path ./vendor/bundle &&\
+    sudo -u discourse bundle config --local without test development &&\
+    sudo -u discourse bundle install --jobs 4 &&\
+    sudo -u discourse yarn install --production &&\
+    sudo -u discourse yarn cache clean &&\
+    bundle exec rake maxminddb:get &&\
+    find /var/www/discourse/vendor/bundle -name tmp -type d -exec rm -rf {} + &&\
+    chown -R discourse:discourse /var/www/discourse
 
-WORKDIR /home/discourse/discourse
+COPY install /tmp/install
 
 USER discourse
 
+RUN cd /var/www/discourse/plugins \
+ && for plugin in $(cat /tmp/install/plugin-list); do \
+      git clone $plugin; \
+    done
+
 CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
diff --git a/README.md b/README.md
index 983e9c1..b81641d 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,8 @@
 # docker-discourse
 
-discourse image for discourse service
+Discourse image for discourse service
+It is based on [upstream discourse image](https://hub.docker.com/r/discourse/base/)
 
 ## Discourse plugins
 
-This image supports installing Discourse plugins at build time, via the `DISCOURSE_ADDITIONAL_PLUGINS` [build arg](https://docs.docker.com/engine/reference/builder/#/arg). Set it to a whitespace (space, tab, newline) separated list if valid `git` URLs of plugins to be installed at build time.
\ No newline at end of file
+This image supports installing Discourse plugins at build time, via the `install/plugin-list` file. It lists`git` URLs of plugins to be installed at build time.
\ No newline at end of file
diff --git a/install/imagemagick b/install/imagemagick
deleted file mode 100755
index 023f09a..0000000
--- a/install/imagemagick
+++ /dev/null
@@ -1,62 +0,0 @@
-#!/bin/bash
-set -e
-
-# version check: https://github.com/ImageMagick/ImageMagick/releases
-IMAGE_MAGICK_VERSION="7.0.10-24"
-IMAGE_MAGICK_HASH="c555b4724127f9993500c9a7bab148e6075a7397957516b9e0dd9faa02d5c98e"
-
-# version check: https://libpng.sourceforge.io/index.html
-LIBPNG_VERSION="1.6.37"
-LIBPNG_HASH="daeb2620d829575513e35fecc83f0d3791a620b9b93d800b763542ece9390fb4"
-
-PREFIX=/usr/local
-WDIR=/tmp/imagemagick
-
-# Install build deps
-apt -y -q remove imagemagick
-
-mkdir -p $WDIR
-cd $WDIR
-
-# Build and install libpng
-wget -O $WDIR/libpng.tar.gz https://prdownloads.sourceforge.net/libpng/libpng-$LIBPNG_VERSION.tar.gz?download
-sha256sum $WDIR/libpng.tar.gz
-echo "$LIBPNG_HASH $WDIR/libpng.tar.gz" | sha256sum -c
-
-tar -xzvf $WDIR/libpng.tar.gz
-cd $WDIR/libpng-$LIBPNG_VERSION
-
-./configure --prefix=$PREFIX
-make all && make install
-
-# Build and install ImageMagick
-wget -O $WDIR/ImageMagick.tar.gz "https://github.com/ImageMagick/ImageMagick/archive/$IMAGE_MAGICK_VERSION.tar.gz"
-sha256sum $WDIR/ImageMagick.tar.gz
-echo "$IMAGE_MAGICK_HASH $WDIR/ImageMagick.tar.gz" | sha256sum -c
-IMDIR=$WDIR/$(tar tzf $WDIR/ImageMagick.tar.gz --wildcards "ImageMagick-*/configure" |cut -d/ -f1)
-tar zxf $WDIR/ImageMagick.tar.gz -C $WDIR
-cd $IMDIR
-PKG_CONF_LIBDIR=$PREFIX/lib LDFLAGS=-L$PREFIX/lib CFLAGS=-I$PREFIX/include ./configure \
-          --prefix=$PREFIX \
-          --enable-static \
-          --enable-bounds-checking \
-          --enable-hdri \
-          --enable-hugepages \
-          --with-threads \
-          --with-modules \
-          --with-quantum-depth=16 \
-          --without-magick-plus-plus \
-          --with-bzlib \
-          --with-zlib \
-          --without-autotrace \
-          --with-freetype \
-          --with-jpeg \
-          --without-lcms \
-          --with-lzma \
-          --with-png \
-          --with-tiff
-make all && make install
-
-cd $HOME
-rm -rf $WDIR
-ldconfig /usr/local/lib
-- 
GitLab