Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit 90dd696

Browse files
authored
feat: init publish-postgres-with-data-docker-action
0 parents  commit 90dd696

File tree

12 files changed

+967
-0
lines changed

12 files changed

+967
-0
lines changed

.devcontainer/Dockerfile

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Update the VARIANT arg in docker-compose.yml to pick a Node version: 10, 12, 14
2+
ARG VARIANT=12
3+
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}
4+
5+
# Update args in docker-compose.yaml to set the UID/GID of the "node" user.
6+
ARG USER_UID=1000
7+
ARG USER_GID=$USER_UID
8+
RUN if [ "$USER_GID" != "1000" ] || [ "$USER_UID" != "1000" ]; then groupmod --gid $USER_GID node && usermod --uid $USER_UID --gid $USER_GID node; fi
9+
10+
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - \
11+
&& echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" |sudo tee /etc/apt/sources.list.d/pgdg.list
12+
13+
# [Optional] Uncomment this section to install additional OS packages.
14+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
15+
&& apt-get -y install --no-install-recommends postgresql-client-13 tmux neovim brotli awscli \
16+
&& curl -sL https://app.snaplet.dev/get-cli/ | bash
17+
18+
# Docker from docker setup
19+
20+
RUN apt-get update \
21+
&& apt-get install -y apt-transport-https ca-certificates curl gnupg2 lsb-release \
22+
&& curl -fsSL https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/gpg | apt-key add - 2>/dev/null \
23+
&& echo "deb [arch=amd64] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list \
24+
&& apt-get update \
25+
&& apt-get install -y docker-ce-cli
26+
27+
# Install Docker Compose
28+
RUN LATEST_COMPOSE_VERSION=$(curl -sSL "https://api.github.com/repos/docker/compose/releases/latest" | grep -o -P '(?<="tag_name": ").+(?=")') \
29+
&& curl -sSL "https://github.com/docker/compose/releases/download/${LATEST_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose \
30+
&& chmod +x /usr/local/bin/docker-compose
31+
32+
ARG NONROOT_USER=node
33+
34+
RUN echo "#!/bin/sh\n\
35+
sudoIf() { if [ \"\$(id -u)\" -ne 0 ]; then sudo \"\$@\"; else \"\$@\"; fi }\n\
36+
SOCKET_GID=\$(stat -c '%g' /var/run/docker.sock) \n\
37+
if [ \"${SOCKET_GID}\" != '0' ]; then\n\
38+
if [ \"\$(cat /etc/group | grep :\${SOCKET_GID}:)\" = '' ]; then sudoIf groupadd --gid \${SOCKET_GID} docker-host; fi \n\
39+
if [ \"\$(id ${NONROOT_USER} | grep -E \"groups=.*(=|,)\${SOCKET_GID}\(\")\" = '' ]; then sudoIf usermod -aG \${SOCKET_GID} ${NONROOT_USER}; fi\n\
40+
fi\n\
41+
exec \"\$@\"" > /usr/local/share/docker-init.sh \
42+
&& chmod +x /usr/local/share/docker-init.sh
43+
44+
# SSH setup - start
45+
46+
RUN wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip && unzip ngrok-stable-linux-amd64.zip && mv ./ngrok /usr/bin/ngrok
47+
RUN curl -sSL https://raw.githubusercontent.com/microsoft/vscode-dev-containers/master/script-library/sshd-debian.sh | sudo bash -s -- 2222 $(whoami) true random
48+
49+
# [Optional] Uncomment if you want to install an additional version of node using nvm
50+
# ARG EXTRA_NODE_VERSION=10
51+
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"
52+
53+
# [Optional] Uncomment if you want to install more global node modules
54+
# RUN su node -c "npm install -g <your-package-list-here>"
55+
56+
# Setting the ENTRYPOINT to docker-init.sh will configure non-root access
57+
# to the Docker socket. The script will also execute CMD as needed.
58+
ENTRYPOINT [ "/usr/local/share/docker-init.sh" ]
59+
CMD [ "sleep", "infinity" ]

.devcontainer/devcontainer.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2+
// https://github.com/microsoft/vscode-dev-containers/tree/v0.187.0/containers/javascript-node-postgres
3+
// Update the VARIANT arg in docker-compose.yml to pick a Node.js version: 10, 12, 14
4+
{
5+
"name": "Node.js & PostgreSQL (Community)",
6+
"dockerComposeFile": "docker-compose.yml",
7+
"service": "app",
8+
"workspaceFolder": "/workspace",
9+
10+
// Set *default* container specific settings.json values on container create.
11+
"settings": {
12+
"sqltools.connections": [
13+
{
14+
"name": "Videolet",
15+
"driver": "PostgreSQL",
16+
"previewLimit": 50,
17+
"server": "localhost",
18+
"port": 5432,
19+
"username": "postgres",
20+
"password": "postgres",
21+
"database": "videolet"
22+
}
23+
]
24+
},
25+
26+
// Add the IDs of extensions you want installed when the container is created.
27+
"extensions": [
28+
"dbaeumer.vscode-eslint",
29+
"mtxr.sqltools",
30+
"mtxr.sqltools-driver-pg"
31+
],
32+
33+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
34+
// ssh -p 2222 node@localhost when the codespace is running
35+
"forwardPorts": [5432, 2222],
36+
37+
// Use 'postCreateCommand' to run commands after the container is created.
38+
// "postCreateCommand": "yarn install",
39+
40+
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
41+
"remoteUser": "node"
42+
}

.devcontainer/docker-compose.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
version: "3"
2+
3+
services:
4+
app:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
args:
9+
# [Choice] Node.js version: 14, 12, 10
10+
VARIANT: 14
11+
# On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.
12+
USER_UID: 1000
13+
USER_GID: 1000
14+
15+
volumes:
16+
- /var/run/docker.sock:/var/run/docker.sock
17+
- ..:/workspace:cached
18+
19+
# Overrides default command so things don't shut down after the process ends.
20+
command: sleep infinity
21+
22+
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
23+
network_mode: service:db
24+
25+
# Uncomment the next line to use a non-root user for all processes.
26+
# user: node
27+
28+
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
29+
# (Adding the "ports" property to this file will not forward from a Codespace.)
30+
31+
db:
32+
image: postgres:latest
33+
# image: ghcr.io/snaplet/snaplet_database
34+
restart: unless-stopped
35+
volumes:
36+
- postgres-data:/var/lib/postgresql/data
37+
environment:
38+
POSTGRES_PASSWORD: postgres
39+
POSTGRES_USER: postgres
40+
POSTGRES_DB: videolet
41+
42+
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
43+
# (Adding the "ports" property to this file will not forward from a Codespace.)
44+
45+
volumes:
46+
postgres-data:
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Snaplet Restore
2+
on:
3+
push:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: '0 5 * * *' # 5 AM everyday
7+
8+
jobs:
9+
# Label of the runner job
10+
snaplet-restore:
11+
name: Snaplet Restore
12+
timeout-minutes: 10
13+
runs-on: ubuntu-latest
14+
15+
services:
16+
postgres:
17+
image: postgres:latest # Must match the database where snapshot was taken
18+
env:
19+
POSTGRES_PASSWORD: postgres
20+
POSTGRES_USER: postgres
21+
POSTGRES_DB: videolet
22+
options: >-
23+
--health-cmd pg_isready
24+
--health-interval 10s
25+
--health-timeout 5s
26+
--health-retries 5
27+
ports:
28+
- 5432:5432
29+
30+
steps:
31+
- name: Checkout Repository Code
32+
uses: actions/checkout@v2
33+
- uses: actions/setup-node@v2
34+
with:
35+
node-version: '16'
36+
cache: 'yarn'
37+
38+
- name: Install Dependencies
39+
run: yarn install --frozen-lockfile
40+
41+
- name: Build
42+
run: yarn build
43+
44+
- name: Login to Docker Hub
45+
uses: docker/login-action@v1
46+
with:
47+
registry: ${{ secrets.GHCR_CONTAINER_REGISTRY_SERVER }}
48+
username: ${{ secrets.GHCR_CONTAINER_REGISTRY_USER }}
49+
password: ${{ secrets.GHCR_CONTAINER_REGISTRY_PASSWORD }}
50+
51+
- name: Install Snaplet
52+
run: curl -sL https://app.snaplet.dev/get-cli/ | bash
53+
54+
- name: Snaplet Restore
55+
uses: ./
56+
with:
57+
docker-image-name: publish-postgres-with-data-docker-action_database
58+
env:
59+
SNAPLET_ACCESS_TOKEN: ${{ secrets.SNAPLET_ACCESS_TOKEN }}
60+
SNAPLET_DATABASE_ID: ckq46skzy358532hraos3ptc7p
61+
SNAPLET_RESTORE_DB_NAME: videolet
62+
PGHOST: localhost
63+
PGUSER: postgres
64+
PGPASSWORD: postgres
65+
PGPORT: 5432
66+
PGDATABASE: videolet

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
.gitignore
3+
.snaplet/
4+
data/

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Introduction
2+
3+
Snaplet's Github action lets your create safe database snapshots on-demand.
4+
5+
# Inputs
6+
7+
## `docker-image-tag`
8+
9+
The name of the docker image tag to use. Default `"snaplet_database"`.
10+
11+
## Example usage
12+
13+
```
14+
uses: snaplet/publish-postgres-with-data-docker-action@main
15+
with:
16+
docker-image-tag: snaplet_database
17+
```

action.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Postgres Docker image with "baked-in" data
2+
author: Snaplet, Inc.
3+
description: Creates a Postgres Docker Image, with data from a Snaplet snapshot, that is published to Github's container registry for restoration in Codespaces, Staging & Development environments.
4+
inputs:
5+
docker-image-name:
6+
description: Name of docker image
7+
required: false
8+
default: snaplet_database
9+
runs:
10+
using: "node12"
11+
main: "dist/index.js"

0 commit comments

Comments
 (0)