From 181f85303a08da57f3430b7b508ae7f9cdb04403 Mon Sep 17 00:00:00 2001 From: Mo Balaa Date: Thu, 22 Feb 2024 16:05:01 -0600 Subject: [PATCH] Add CI Github Action (#36) * Generate link with Github action on commit --------- Co-authored-by: Justin --- .github/workflows/ci.yaml | 27 +++++++++++++++++++++++++++ .gitignore | 1 + Makefile | 7 +++++-- ci/Dockerfile | 8 ++++++++ ci/create-link-ci.sh | 11 +++++++++++ ci/docker-compose.yaml | 21 +++++++++++++++++++++ src/create-link/entrypoint.sh | 11 ++++++++++- 7 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 .gitignore create mode 100644 ci/Dockerfile create mode 100755 ci/create-link-ci.sh create mode 100644 ci/docker-compose.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..ffc97d7 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,27 @@ +name: CI + +on: + push: + branches: + - '**' # This will run the workflow on every push to any branch + +jobs: + build_and_test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Build images + run: make docker + + - name: Launch gateway and create link on gateway + run: make link-ci GATEWAY=gateway-sshd FQDN=app.example.com EXPOSE=app:3000 + + #- name: Ensure link works by making request to app.example.com + # run: curl --resolve app.example.com:443:127.0.0.1 http://app.example.com:443 + + + - name: Cleanup + run: docker compose -f ./ci/docker-compose.yaml down \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7254627 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +gateway-sim-key* diff --git a/Makefile b/Makefile index 0fca764..9e8e4e3 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: docker link setup gateway +.PHONY: docker link setup gateway ci docker: docker build -t fractalnetworks/selfhosted-gateway:latest ./src/gateway/ @@ -16,4 +16,7 @@ link: docker run -e SSH_AGENT_PID=$$SSH_AGENT_PID -e SSH_AUTH_SOCK=$$SSH_AUTH_SOCK -v $$SSH_AUTH_SOCK:$$SSH_AUTH_SOCK --rm -it fractalnetworks/gateway-cli:latest $(GATEWAY) $(FQDN) $(EXPOSE) link-macos: - docker run -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock -e SSH_AUTH_SOCK="/run/host-services/ssh-auth.sock" --rm -it fractalnetworks/gateway-cli:latest $(GATEWAY) $(FQDN) $(EXPOSE} + docker run -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock -e SSH_AUTH_SOCK="/run/host-services/ssh-auth.sock" --rm -it fractalnetworks/gateway-cli:latest $(GATEWAY) $(FQDN) $(EXPOSE) + +link-ci: + ./ci/create-link-ci.sh $(GATEWAY) $(FQDN) $(EXPOSE) \ No newline at end of file diff --git a/ci/Dockerfile b/ci/Dockerfile new file mode 100644 index 0000000..a19620d --- /dev/null +++ b/ci/Dockerfile @@ -0,0 +1,8 @@ +FROM alpine:latest +RUN apk add openssh-server bash docker-cli curl +RUN ssh-keygen -A + +COPY gateway-sim-key.pub /root/.ssh/authorized_keys + +ENTRYPOINT [ "/usr/sbin/sshd", "-D", "-e" ] + diff --git a/ci/create-link-ci.sh b/ci/create-link-ci.sh new file mode 100755 index 0000000..802f3b5 --- /dev/null +++ b/ci/create-link-ci.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +cd ci/ +ssh-keygen -t ed25519 -f ./gateway-sim-key -N "" +# create docker network if not exists +docker network create gateway || true +docker compose up -d --build +eval $(ssh-agent -s) +ssh-add ./gateway-sim-key +docker run --network gateway -e SSH_AGENT_PID=$SSH_AGENT_PID -e SSH_AUTH_SOCK=$SSH_AUTH_SOCK -v $SSH_AUTH_SOCK:$SSH_AUTH_SOCK --rm fractalnetworks/gateway-cli:latest $1 $2 $3 \ No newline at end of file diff --git a/ci/docker-compose.yaml b/ci/docker-compose.yaml new file mode 100644 index 0000000..400241d --- /dev/null +++ b/ci/docker-compose.yaml @@ -0,0 +1,21 @@ +version: '3.9' +services: + gateway-sshd: + build: . + volumes: + - /var/run/docker.sock:/var/run/docker.sock + networks: + - gateway + gateway: + image: fractalnetworks/selfhosted-gateway:latest + environment: + NGINX_ENVSUBST_OUTPUT_DIR: /etc/nginx + networks: + - gateway + + +networks: + gateway: + external: true + + diff --git a/src/create-link/entrypoint.sh b/src/create-link/entrypoint.sh index 8c458ab..b876254 100755 --- a/src/create-link/entrypoint.sh +++ b/src/create-link/entrypoint.sh @@ -4,6 +4,15 @@ set -e SSH_HOST=$1 +SSH_PORT=22 +# split port from SSH_HOST if SSH_HOST contains : +if [[ $SSH_HOST == *":"* ]]; then + IFS=':' read -ra ADDR <<< "$SSH_HOST" + SSH_HOST=${ADDR[0]} + SSH_PORT=${ADDR[1]} +fi +echo $SSH_HOST +echo $SSH_PORT export LINK_DOMAIN=$2 export EXPOSE=$3 export WG_PRIVKEY=$(wg genkey) @@ -16,7 +25,7 @@ export CONTAINER_NAME=$(echo $LINK_DOMAIN|python3 -c 'fqdn=input();print("-".joi LINK_CLIENT_WG_PUBKEY=$(echo $WG_PRIVKEY|wg pubkey) -LINK_ENV=$(ssh $SSH_HOST "bash -s" -- < ./remote.sh $CONTAINER_NAME $LINK_CLIENT_WG_PUBKEY) +LINK_ENV=$(ssh -o StrictHostKeyChecking=accept-new $SSH_HOST -p $SSH_PORT "bash -s" -- < ./remote.sh $CONTAINER_NAME $LINK_CLIENT_WG_PUBKEY) # convert to array RESULT=($LINK_ENV)