Skip to content

Commit

Permalink
chore: update build and deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Apr 15, 2024
1 parent d764294 commit ac5889f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 17 deletions.
34 changes: 19 additions & 15 deletions .github/workflows/deploy-rolling.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
name: Deploy
name: Deploy production

on:
push:
branches:
- master
- main

jobs:
deploy:
runs-on: self-hosted
runs-on: ubuntu-latest
environment:
name: Production
url: https://myrt.co
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v3

- name: Install dependencies
run: npm ci
- name: Build tar image
run: sh -c scripts/build-tar.sh

- name: Rebuild image
run: npm run build:docker

- name: Stop container
run: npm run stop:docker

- name: Start container
run: npm run start:docker
- name: Run deploy playbook
uses: dawidd6/action-ansible-playbook@v2
with:
playbook: deploy/playbook.yaml
key: ${{secrets.SSH_PRIVATE_KEY}}
options: |
--inventory deploy/hosts.yaml
--extra-vars target=production
--verbose
22 changes: 20 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
FROM caddy:alpine
ARG NODE_VERSION=21-alpine
ARG PNPM_VERSION=8.13.1
ARG CADDY_VERSION=2.7-alpine

FROM node:$NODE_VERSION as builder
WORKDIR /build
COPY src ./src
COPY public ./public
COPY \
package.json pnpm-lock.yaml \
tsconfig.json astro.config.mjs \
./
RUN npm install -g pnpm@$PNPM_VERSION
RUN pnpm install --frozen-lockfile
RUN pnpm build

FROM caddy:$CADDY_VERSION
COPY --from=builder /build/dist /data
COPY Caddyfile /etc/caddy/Caddyfile
COPY ./dist /data
EXPOSE 80
EXPOSE 443
12 changes: 12 additions & 0 deletions deploy/hosts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
all:
hosts:
production:
hostnames: myrt.co
# preview:
# hostnames: preview.myrt.co
vars:
ansible_host: myrt.co
ansible_user: runner
ansible_ssh_common_args: -o ControlMaster=auto -o ControlPersist=1200s
ansible_command_timeout: 30
ansible_ssh_retries: 3
42 changes: 42 additions & 0 deletions deploy/playbook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
- hosts: "{{ target }}"
name: Deploy myrt.co

tasks:
- name: Create temporary directory
tempfile:
state: directory
suffix: web
register: tempdir

- name: Copy image archive
retries: 3
delay: 1
synchronize:
rsync_timeout: 60
src: "../dist/myrt-co.docker.tar"
dest: "{{ tempdir.path }}/myrt-co.docker.tar"

- name: Load image from the tar file
community.docker.docker_image_load:
path: "{{ tempdir.path }}/myrt-co.docker.tar"

- name: Start container
docker_container:
image: "myrt-co:latest"
recreate: true
state: started
name: "myrt-co-{{ target }}"
ports:
- 80:80
- 443:443
# labels:
# caddy: "{{ hostnames }}"
# caddy.reverse_proxy: "{% raw %}{{upstreams 80}}{% endraw %}"
# networks:
# - name: "caddy_proxy"

- name: Remove temporary directory
file:
path: "{{ tempdir.path }}"
state: absent
10 changes: 10 additions & 0 deletions scripts/build-tar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

IMAGE_NAME="myrt-co"
SERVER_PLATFORM="linux/amd64"
DIST_DIR="dist"
TAR_FILENAME="myrt-co.docker.tar"

docker buildx build --load --platform "$SERVER_PLATFORM" -t "$IMAGE_NAME:latest" .
mkdir -p "$DIST_DIR"
docker save "$IMAGE_NAME:latest" > "${DIST_DIR}/${TAR_FILENAME}"

0 comments on commit ac5889f

Please sign in to comment.