Skip to content

Commit aa88b3e

Browse files
committed
ci: set up auto deployment
1 parent 9428f34 commit aa88b3e

7 files changed

Lines changed: 26277 additions & 15 deletions

File tree

.github/workflows/workflow.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Build and Deploy
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
jobs:
8+
build-and-deploy:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
13+
- name: Build Docker image
14+
run: docker build -t qcext-server .
15+
16+
- name: Save Docker image
17+
run: docker save -o qcext-server.tar qcext-server
18+
19+
- name: Compress Docker image
20+
run: bzip2 -z qcext-server.tar
21+
22+
- name: Deploy
23+
run: docker-compose run deploy
24+
env:
25+
DEPLOY_KEY: ${{ secrets.deploy_key }}
26+
DEPLOY_TARGET: ${{ secrets.deploy_target }}

.npmrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

Dockerfile

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
FROM rust:latest AS build
1+
##################
2+
# Rust image #
3+
##################
4+
5+
FROM rust:latest AS rust
26

37
WORKDIR /usr/src/qcext-server
48

59
# Run SQLX in offline mode
610
ENV SQLX_OFFLINE=true
711

8-
# Make sure we have npm and nodejs
9-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
10-
nodejs npm
11-
RUN nodejs --version
12-
RUN npm --version
13-
1412
# Build the dependencies in a separate step to avoid rebuilding all of them
1513
# every time the source code changes. This takes advantage of Docker's layer
1614
# caching, and it works by doing a build using the Cargo.{toml,lock} files with
@@ -29,22 +27,46 @@ RUN mkdir -p /usr/src/qcext-server/shared/src && \
2927
RUN cargo fetch
3028
RUN cargo build --release
3129

32-
# Next, let's run npm install
33-
COPY package.json .npmrc /usr/src/qcext-server/
34-
RUN npm install
35-
3630
# Dependencies are now cached, copy the actual source code and do another full
3731
# build. The touch on all the .rs files is needed, otherwise cargo assumes the
3832
# source code didn't change thanks to mtime weirdness.
3933
RUN rm -rf /usr/src/qcext-server/src /usr/src/qcext-server/database/src /usr/src/qcext-server/shared/src
4034
COPY src /usr/src/qcext-server/src
35+
RUN rm -rf /usr/src/qcext-server/src/client
4136
COPY database/src /usr/src/qcext-server/database/src
4237
COPY shared/src /usr/src/qcext-server/shared/src
4338
RUN find src -name "*.rs" -exec touch {} \; && \
4439
find database/src -name "*.rs" -exec touch {} \; && \
4540
find shared/src -name "*.rs" -exec touch {} \; && \
4641
cargo build --release
4742

43+
##################
44+
# NodeJS image #
45+
##################
46+
47+
FROM debian:bullseye AS nodejs
48+
49+
WORKDIR /usr/src/qcext-server
50+
51+
# Make sure we have npm and nodejs
52+
RUN apt-get update
53+
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y curl
54+
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
55+
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs
56+
RUN node --version
57+
RUN npm --version
58+
59+
# Next, let's run npm install
60+
COPY package.json package-lock.json /usr/src/qcext-server/
61+
RUN npm install
62+
63+
# Copy only files relevant for Node (i.e. no Rust files)
64+
RUN mkdir /usr/src/qcext-server/src
65+
COPY src/client /usr/src/qcext-server/src/client
66+
COPY src/index.js /usr/src/qcext-server/src
67+
68+
RUN npx browserslist@latest --update-db
69+
4870
COPY public /usr/src/qcext-server/public
4971
RUN npm run build
5072

@@ -55,11 +77,11 @@ RUN npm run build
5577
FROM debian:bullseye-slim AS binary
5678

5779
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
58-
ca-certificates
80+
ca-certificates rsync
5981
RUN apt-get clean
6082

61-
COPY --from=build /usr/src/qcext-server/target/release/qcext-server /usr/local/bin/
62-
COPY --from=build /usr/src/qcext-server/build /build
83+
COPY --from=rust /usr/src/qcext-server/target/release/qcext-server /usr/local/bin/
84+
COPY --from=nodejs /usr/src/qcext-server/build /build
6385

6486
ENV RUST_LOG=info
6587
CMD ["/usr/local/bin/qcext-server"]

deploy.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
# Instantly exits our script whenever an error occurs
4+
set -e
5+
6+
# Pipe our environmental SSH key variable into a file
7+
mkdir -p $HOME/.ssh
8+
echo "${deploy_key}" > $HOME/.ssh/deploy_key
9+
chmod 600 $HOME/.ssh/deploy_key # SSH keys need to be readonly
10+
11+
# Where to deploy our site on our server
12+
target="/home/ec2-user/qcext-server/staging"
13+
14+
# The actual deployment
15+
sh -c "rsync -azvh -e 'ssh -i $HOME/.ssh/deploy_key -o StrictHostKeyChecking=no' qcext-server.tar.bz2 ${deploy_target}:${target}"
16+
17+
# Run update script
18+
sh -c "ssh -i $HOME/.ssh/deploy_key -o StrictHostKeyChecking=no ${deploy_target} 'cd ${target}; ./update.sh'"
19+
20+
# Remove our deploy_key again since it's no longer needed
21+
rm $HOME/.ssh/deploy_key

docker-compose.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3'
2+
3+
services:
4+
deploy:
5+
image: 'instrumentisto/rsync-ssh'
6+
volumes:
7+
- .:/home/site
8+
working_dir: /home/site
9+
environment:
10+
deploy_key: $DEPLOY_KEY
11+
deploy_target: $DEPLOY_TARGET
12+
command: sh deploy.sh

0 commit comments

Comments
 (0)