Skip to content

Commit

Permalink
Merge pull request #962 from compdemocracy/heroku-docker-deploy
Browse files Browse the repository at this point in the history
Heroku docker deploy
  • Loading branch information
metasoarous committed Apr 19, 2021
2 parents f731bb6 + 99a8cee commit 38d4950
Show file tree
Hide file tree
Showing 89 changed files with 1,504 additions and 5,681 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TAG=dev
41 changes: 41 additions & 0 deletions .github/workflows/deploy-preprod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
on:
push:
branches:
- dev
- main
- edge
- stable

paths:
- .github/workflows/deploy-preprod.yml
- client-admin
- client-participation
- client-report


jobs:
deploy-admin:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2.3.4

- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch

#- uses: DeLaGuardo/setup-clojure@3.2
#with:
#tools-deps: 1.10.1
#cli: 1.10.1.693
#lein: 2.9.4

- name: Build
# Runs all except integration tests which require database and setup/teardown
run: ./bin/build-static-assets.clj

#- name: Push
## Runs all except integration tests which require database and setup/teardown
#run: ./bin/deploy-static-assets.clj

Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# Changes to workflow name require changes to badge URL in README.md
name: Nightly Docker Builds
name: Docker image builds

on:
schedule:
# Every night at 4am ET
- cron: '0 8 * * *'
push:
branches:
- main
- stable
- edge
- dev

jobs:
build:
runs-on: ubuntu-latest
env:
GHA_BRANCH: dev
# Use native docker command within docker-compose
COMPOSE_DOCKER_CLI_BUILD: 1
# Use buildkit to speed up docker command
DOCKER_BUILDKIT: 1
steps:

- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch

- name: Checkout
uses: actions/checkout@v2.3.4
with:
ref: ${{ env.GHA_BRANCH }}
ref: ${{ steps.extranct_branch.outputs.branch }}

- name: Login to Docker Hub
uses: azure/docker-login@v1
Expand Down
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM alpine:latest

ENV BUILD_DEPS="bash build-base libpng-dev zlib-dev autoconf automake libtool nasm curl" \
RUN_DEPS="openssh-client openjdk8-jre"

WORKDIR /build
COPY . .

# for build scripting
RUN \
bash < <(curl -s https://raw.githubusercontent.com/babashka/babashka/master/install)

#apk del $BUILD_DEPS && \
#rm -rf /tmp/* /var/cache/*
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
pol.is an AI powered sentiment gathering platform. More organic than surveys, less effort than focus groups.

<!-- Changes to badge text in URLs below, require changes to "name" value in .github/workflows/*.yml -->
[![Nightly Docker Builds](https://github.com/compdemocracy/polis/workflows/Nightly%20Docker%20Builds/badge.svg)][nightlies]
[![Docker Image Builds](https://github.com/compdemocracy/polis/workflows/Docker%20image%20Builds/badge.svg)][docker-image-builds]
[![E2E Tests](https://github.com/compdemocracy/polis/workflows/E2E%20Tests/badge.svg)][e2e-tests]

[nightlies]: https://hub.docker.com/u/polisdemo
[nightlies]: https://hub.docker.com/u/compdem
[e2e-tests]: https://github.com/compdemocracy/polis/actions?query=workflow%3A%22E2E+Tests%22

## :construction: Disclaimer
Expand All @@ -16,7 +16,7 @@ We do **NOT** make guarantees of easy setup or management, push-button deploymen

Having said this, some of the core pieces of infrastructure described below are potentially useful in a production context, if used correctly.
In particular, each subdirectory of the project has its own `Dockerfile` which could potentially be used as part of a deploy strategy.
The `docker-compose.yml` is specifically focused on development environment flow.
The `docker-compose.yml` is currently focused on development environment, but will soon transition to being production focused, with development conveniences extracted as a separate compose file.

- See also: [Deployment: About SSL/HTTPS](docs/deployment.md#about-sslhttps)

Expand Down
113 changes: 113 additions & 0 deletions bin/build-static-assets.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env bb

(require '[babashka.pods :as pods]
'[babashka.deps :as deps]
'[babashka.process :as process]
'[clojure.core.async :as async]
'[clojure.pprint :as pp]
'[clojure.tools.cli :as cli]
'[clojure.java.io :as io]
'[clojure.string :as string])


;; basic example using the process library
;(-> (process/process '[ls -a] {:dir "math"}) :out slurp)

(def timestamp (System/currentTimeMillis))

(defn logged-command [& args]
(println "Executing command:" (pr-str args))
(apply process/process args))


(defn image-name [client-dir]
(str "polis/" client-dir))

(defn container-name [client-dir]
(str "polis-" client-dir "-cp-container-" timestamp))

(defn build-client
"Build the client container, and give it a unique name"
[client-dir]
(logged-command ['docker 'build '-t (image-name client-dir) '.]
{:dir client-dir}))


(defn run-client-container
[client-dir]
"Run sleep in the client container so that it is running when we try to copy stuff out of it
(docker cp only works when the container is running)"
(let [command (concat '[docker run --name]
[(container-name client-dir) (image-name client-dir)]
;; we sleep in the container so that it doesn't just shut down immediately, before we
;; copy anything
'[sleep 10])]
(logged-command command {:dir client-dir})))

(defn clean-build
"Remove local build/dist files"
[client-dir]
(logged-command '[rm -fr dist] {:dir client-dir}))


(defn cp-client
"Copy contents out of the running docker image"
[client-dir]
(logged-command ['docker 'cp (str (container-name client-dir) ":/app/dist/") 'dist]
{:dir client-dir}))

(defn monitor-execution
"Monitor the execution, and return an error object if the process does not execute properly"
[proc]
(let [{:keys [exit out err]} @proc]
(when (> exit 0)
(println "PROCESS FAILED TO EXECUTE SUCCESSFULLY!"))
(println "Exit status:" exit)
(let [out-str (slurp out)
err-str (slurp err)]
(when-not (empty? out-str)
(println "Std out" out-str))
(when-not (empty? err-str)
(println "Std err" err-str)))))

(defn stop-containers
"Stop the containers, so that we can remove them"
[client-dir]
(logged-command ['docker 'stop (container-name client-dir)]))

(defn clean-containers
"Remove the old container to clean up after ourselves"
[client-dir]
(logged-command ['docker 'rm (container-name client-dir)]))



(defn build-and-cp-client
"Build the clients with docker and copy out the assets"
[client-dir]
;; build the client itself, and block till complete
(monitor-execution (build-client client-dir))
;; start client container and let run (should have softer monitoring of this
(run-client-container client-dir)
;; once that is started clean the build dir
(monitor-execution (build-client client-dir))
(monitor-execution (clean-build client-dir))
(monitor-execution (cp-client client-dir))
(monitor-execution (stop-containers client-dir))
(monitor-execution (clean-containers client-dir)))
;; once that has completed,

(def processes
(for [client-dir ["client-admin" "client-participation"]];; "client-report"]] ; leaving client-report off for now
(async/thread
(build-and-cp-client client-dir)
(println "Finished building:" client-dir))))

;; Initiate all of the processes, since for is a lazy list
(doall processes)
;; for each process, wait until the process completes
(doseq [proc processes]
(async/<!! proc))

;; QED

0 comments on commit 38d4950

Please sign in to comment.