From 46769f09850fdf0bbdef59724021848f492a1121 Mon Sep 17 00:00:00 2001 From: Tim Morgan Date: Wed, 19 Jun 2024 16:29:03 -0700 Subject: [PATCH] Move to Fly.io --- .docker/default.conf | 45 +++++++++++++++++++++++++++++++++ .docker/nginx.conf | 32 ++++++++++++++++++++++++ .dockerignore | 5 ++++ .github/workflows/deploy.yml | 38 ++++++++++++++++++++++++++++ .yarn/releases/.gitkeep | 0 Dockerfile | 48 ++++++++++++++++++++++++++++++++++++ README.md | 4 +-- deploy.sh | 2 -- fly.toml | 22 +++++++++++++++++ src/store/modules/root.ts | 3 ++- 10 files changed, 194 insertions(+), 5 deletions(-) create mode 100644 .docker/default.conf create mode 100644 .docker/nginx.conf create mode 100644 .dockerignore create mode 100644 .github/workflows/deploy.yml create mode 100644 .yarn/releases/.gitkeep create mode 100644 Dockerfile delete mode 100755 deploy.sh create mode 100644 fly.toml diff --git a/.docker/default.conf b/.docker/default.conf new file mode 100644 index 0000000..6bf0186 --- /dev/null +++ b/.docker/default.conf @@ -0,0 +1,45 @@ +server { + listen 80; + server_name localhost; + + #access_log /var/log/nginx/host.access.log main; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + #error_page 404 /404.html; + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + # proxy the PHP scripts to Apache listening on 127.0.0.1:80 + # + #location ~ \.php$ { + # proxy_pass http://127.0.0.1; + #} + + # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 + # + #location ~ \.php$ { + # root html; + # fastcgi_pass 127.0.0.1:9000; + # fastcgi_index index.php; + # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; + # include fastcgi_params; + #} + + # deny access to .htaccess files, if Apache's document root + # concurs with nginx's one + # + #location ~ /\.ht { + # deny all; + #} +} + diff --git a/.docker/nginx.conf b/.docker/nginx.conf new file mode 100644 index 0000000..5e076aa --- /dev/null +++ b/.docker/nginx.conf @@ -0,0 +1,32 @@ + +user nginx; +worker_processes auto; + +error_log /var/log/nginx/error.log notice; +pid /var/run/nginx.pid; + + +events { + worker_connections 1024; +} + + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + #tcp_nopush on; + + keepalive_timeout 65; + + #gzip on; + + include /etc/nginx/conf.d/*.conf; +} diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..47719be --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +fly.toml +Dockerfile +.dockerignore +node_modules +.git diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..2630e4d --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,38 @@ +name: Deploy + +on: + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + + # Runs when CI passes + workflow_run: + workflows: [CI] + branches: [master] + types: [completed] + +# Allow one concurrent deployment +concurrency: + group: pages + cancel-in-progress: true + +env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} + +jobs: + deploy: + name: Deploy to Fly.io + if: ${{ github.event.workflow_run.conclusion == 'success' }} + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v3 + - name: Set up flyctl + uses: superfly/flyctl-actions/setup-flyctl@master + - name: Deploy to Fly.io + run: flyctl deploy --remote-only + - name: Notify Bugsnag + uses: psprings/bugsnag-release-action@v0.0.3 + with: + apiKey: ${{ secrets.BUGSNAG_API_KEY }} + appVersion: ${{ github.sha }} + releaseStage: production diff --git a/.yarn/releases/.gitkeep b/.yarn/releases/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..949f870 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,48 @@ +# syntax = docker/dockerfile:1 + +# Adjust NODE_VERSION as desired +ARG NODE_VERSION=22.3.0 +FROM node:${NODE_VERSION}-slim as base + +# Vite app lives here +WORKDIR /app + +# Set production environment +ENV NODE_ENV="production" +ARG YARN_VERSION=4.3.0 +RUN corepack enable yarn + + +# Throw-away build stage to reduce size of final image +FROM base as build + +# Install packages needed to build node modules +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3 + +# Install node modules +COPY --link .yarnrc.yml package.json yarn.lock ./ +COPY --link .yarn/releases/* .yarn/releases/ +RUN yarn install --immutable + +# Copy application code +COPY --link . . + +# Build application +RUN yarn run build + +# Remove development dependencies +RUN yarn workspaces focus --production + + +# Final stage for app image +FROM nginx +COPY .docker/nginx.conf /etc/nginx/nginx.conf +COPY .docker/default.conf /etc/nginx/conf.d/default.conf + +# Copy built application +COPY --from=build /app/dist /usr/share/nginx/html + +# Start the server by default, this can be overwritten at runtime +EXPOSE 80 +CMD [ "/usr/sbin/nginx", "-g", "daemon off;" ] diff --git a/README.md b/README.md index 3e89a64..cddea80 100644 --- a/README.md +++ b/README.md @@ -46,8 +46,8 @@ as an entire suite, in order. ### Deployment -The application is deployed using the `deploy.sh` script, which builds -production assets and copies them to the production server. +This application is deployed using Fly.io. The `deploy.yml` GitHub Actions +workflow runs automatically after CI completes. ## Architecture diff --git a/deploy.sh b/deploy.sh deleted file mode 100755 index 2a032aa..0000000 --- a/deploy.sh +++ /dev/null @@ -1,2 +0,0 @@ -yarn build -rsync -rvz --delete --force dist/ deploy@greenie.app:/var/www/greenie.app/ diff --git a/fly.toml b/fly.toml new file mode 100644 index 0000000..da9433a --- /dev/null +++ b/fly.toml @@ -0,0 +1,22 @@ +# fly.toml app configuration file generated for greenie on 2024-06-19T16:23:04-07:00 +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. +# + +app = 'greenie' +primary_region = 'sjc' + +[build] + +[http_service] + internal_port = 80 + force_https = true + auto_stop_machines = true + auto_start_machines = true + min_machines_running = 0 + processes = ['app'] + +[[vm]] + memory = '1gb' + cpu_kind = 'shared' + cpus = 1 diff --git a/src/store/modules/root.ts b/src/store/modules/root.ts index 82c5630..b3cb1c3 100644 --- a/src/store/modules/root.ts +++ b/src/store/modules/root.ts @@ -118,7 +118,8 @@ const actions: ActionTree = { fetch(secrets.APIURL + path, { method: (method || 'get'), body: serializedBody, - headers + headers, + credentials: 'include' }).then(response => { if (response.status === 401 && !skipResetAuth) { commit('RESET_AUTH')