diff --git a/.drone.jsonnet b/.drone.jsonnet new file mode 100644 index 0000000..ad0e83d --- /dev/null +++ b/.drone.jsonnet @@ -0,0 +1,89 @@ +local image = std.extVar("image"); +local app_name = std.extVar("app_name"); +local target_arch = std.extVar("target_arch"); + +local flags = " --release --target=" + target_arch; + +local volume(name, path) = { + name: name, + path: path, +}; + +local volumes = [ + volume("cargo", "/usr/local/cargo"), + volume("target", "/cache/target"), + volume("rustup", "/usr/local/rustup"), +]; + +local step(name, depends=[], commands=[], env={}) = { + name: name, + volumes: volumes, + image: image, + depends_on: depends, + commands: commands, + environment: env + { + CARGO_TARGET_DIR: "/cache/target", + APP_NAME: "deploytest", + }, +}; + +local temp_volume(name) = { + name: name, + temp: {}, +}; + +local host_volume(name, path) = { + name: name, + host: { + path: path, + }, +}; + +{ + kind: "pipeline", + type: "docker", + name: "default", + platform: { + "os": "linux", + "arch": "arm", + }, + + steps: [ + step( + "validate", + commands=["cargo test" + flags], + ), + step( + "test", + depends=["validate"], + commands=["cargo test" + flags], + ), + step( + "build", + depends=["test"], + commands=["cargo build" + flags], + ), + step( + "deploy", + depends=["build"], + commands=["sh scripts/deploy.sh"], + env={ + "TARGET_DIR": "/cache/target/" + target_arch + "/release", + "SSH_PRIVATE_KEY": {"from_secret": "ssh_pk_base64"}, + "SSH_USER": {"from_secret": "SSH_USER"}, + }, + ), + ], + + volumes: [ + temp_volume("target"), + host_volume("cargo", "/home/pi/.cargo"), + host_volume("rustup", "/home/pi/.rustup"), + ], + + // BUG: thid does not add. + // environment: { + // CARGO_TARGET_DIR: "/cache/target/", + // APP_NAME: "deploytest", + // } +} diff --git a/.drone.yml b/.drone.yml index d69d375..67fd199 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,42 +1,96 @@ --- kind: pipeline type: docker -name: build-check-deploy +name: default platform: - os: "linux" - arch: "arm" + os: linux + arch: arm steps: - # Fetch deps and check the code -- name: "validate" - image: "rust:1.49" - volumes: - - name: "cache" - path: "./target" +- name: validate + image: rust:1.49 commands: - - "apt-get update && apt-get install -yqq musl-tools" - - "rustup target add armv7-unknown-linux-musleabihf" - - "CC=musl-gcc cargo check --release --target=armv7-unknown-linux-musleabihf" - - "CC=musl-gcc cargo test --release --target=armv7-unknown-linux-musleabihf" - - "CC=musl-gcc cargo build --release --target=armv7-unknown-linux-musleabihf" - # Deploy it somewhere. -- name: "deploy" - image: "rust:1.49" - depends_on: - - "validate" + - cargo test --release --target=armv7-unknown-linux-gnueabihf environment: - SSH_PRIVATE_KEY: - from_secret: SSH_PRIVATE_KEY - SSH_PARAMS: '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/tmp/known_hosts' - TARGET_DIR: "./.target/armv7-unknown-linux-musleabihf/release" + APP_NAME: deploytest + CARGO_TARGET_DIR: /cache/target + volumes: + - name: cargo + path: /usr/local/cargo + - name: target + path: /cache/target + - name: rustup + path: /usr/local/rustup + +- name: test + image: rust:1.49 commands: - # Start the SSH Agent and insert ssh private key. - - "eval $(ssh-agent -s)" - - "echo '${SSH_PRIVATE_KEY}' | tr -d '\r' | ssh-add -" - # Copy the binary file to it. - - "scp ${SSH_PARAMS} ${TARGET_DIR}/alterego ${SSH_USER}@${SSH_HOST}:/home/pi/alterego" + - cargo test --release --target=armv7-unknown-linux-gnueabihf + environment: + APP_NAME: deploytest + CARGO_TARGET_DIR: /cache/target + volumes: + - name: cargo + path: /usr/local/cargo + - name: target + path: /cache/target + - name: rustup + path: /usr/local/rustup + depends_on: + - validate + +- name: build + image: rust:1.49 + commands: + - cargo build --release --target=armv7-unknown-linux-gnueabihf + environment: + APP_NAME: deploytest + CARGO_TARGET_DIR: /cache/target + volumes: + - name: cargo + path: /usr/local/cargo + - name: target + path: /cache/target + - name: rustup + path: /usr/local/rustup + depends_on: + - test + +- name: deploy + image: rust:1.49 + commands: + - sh scripts/deploy.sh + environment: + APP_NAME: deploytest + CARGO_TARGET_DIR: /cache/target + SSH_PRIVATE_KEY: + from_secret: ssh_pk_base64 + SSH_USER: + from_secret: SSH_USER + TARGET_DIR: /cache/target/armv7-unknown-linux-gnueabihf/release + volumes: + - name: cargo + path: /usr/local/cargo + - name: target + path: /cache/target + - name: rustup + path: /usr/local/rustup + depends_on: + - build volumes: -- name: "cache" +- name: target temp: {} +- name: cargo + host: + path: /home/pi/.cargo +- name: rustup + host: + path: /home/pi/.rustup + +--- +kind: signature +hmac: 5d42ee9863b09aa2c5b1140903b659ec25660acdab89f20a6d070c30d46898b0 + +... diff --git a/.drone.yml.bak b/.drone.yml.bak new file mode 100644 index 0000000..d69d375 --- /dev/null +++ b/.drone.yml.bak @@ -0,0 +1,42 @@ +--- +kind: pipeline +type: docker +name: build-check-deploy + +platform: + os: "linux" + arch: "arm" + +steps: + # Fetch deps and check the code +- name: "validate" + image: "rust:1.49" + volumes: + - name: "cache" + path: "./target" + commands: + - "apt-get update && apt-get install -yqq musl-tools" + - "rustup target add armv7-unknown-linux-musleabihf" + - "CC=musl-gcc cargo check --release --target=armv7-unknown-linux-musleabihf" + - "CC=musl-gcc cargo test --release --target=armv7-unknown-linux-musleabihf" + - "CC=musl-gcc cargo build --release --target=armv7-unknown-linux-musleabihf" + # Deploy it somewhere. +- name: "deploy" + image: "rust:1.49" + depends_on: + - "validate" + environment: + SSH_PRIVATE_KEY: + from_secret: SSH_PRIVATE_KEY + SSH_PARAMS: '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/tmp/known_hosts' + TARGET_DIR: "./.target/armv7-unknown-linux-musleabihf/release" + commands: + # Start the SSH Agent and insert ssh private key. + - "eval $(ssh-agent -s)" + - "echo '${SSH_PRIVATE_KEY}' | tr -d '\r' | ssh-add -" + # Copy the binary file to it. + - "scp ${SSH_PARAMS} ${TARGET_DIR}/alterego ${SSH_USER}@${SSH_HOST}:/home/pi/alterego" + +volumes: +- name: "cache" + temp: {} diff --git a/makefile b/makefile index 8139814..1b28bd6 100755 --- a/makefile +++ b/makefile @@ -7,6 +7,10 @@ DOCKERFLAGS:=-it --rm \ DOCKERIMG:="rust-build-env:V1" +APP_NAME:=altherego +IMAGE:=rust:1.49 +TARGET_ARCH:=armv7-unknown-linux-gnueabihf + image: docker build -t rust-build-env:V1 . .PHONY: image @@ -25,3 +29,12 @@ build_release_arm: docker_build_release_arm: docker run ${DOCKERFLAGS} ${DOCKERIMG} make build_release_arm + +dronefile: + drone jsonnet \ + --format \ + -V image=${APP_NAME} \ + -V image=${IMAGE} \ + -V target_arch=${TARGET_ARCH} + drone sign frx/altherego --save +.PHONY: dronefile diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100644 index 0000000..680c1b3 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,30 @@ +# !/bin/sh + +set -e +set -x + +# Setup SSH Agent +eval $(ssh-agent -s) +printenv SSH_PRIVATE_KEY | base64 -d | ssh-add - + +# Approve home.loyso.art +mkdir ~/.ssh +ssh-keyscan home.loyso.art > $HOME/.ssh/known_hosts +chmod 644 ~/.ssh/known_hosts +# Setup useful vars. +SRC_DIR="/opt/${APP_NAME}" +SRC_APP="${SRC_DIR}/${APP_NAME}" +SSH_PREFIX="${SSH_USER}@home.loyso.art" + +echo $SSH_PREFIX +echo $SRC_APP + +ssh $SSH_PREFIX -C "sudo systemctl stop ${APP_NAME}" || true +ssh $SSH_PREFIX -C "mv ${SRC_APP} ${SRC_APP}_$(date +'%s')" || true +# Copy binary to host machine. +scp ${TARGET_DIR}/${APP_NAME} ${SSH_USER}@home.loyso.art:${SRC_APP} + +# Keep only last 3 versions. +ssh $SSH_PREFIX -C "cd ${SRC_DIR};echo \$(ls ${APP_NAME}_* | sort | head -n -2)" + +ssh $SSH_PREFIX -C "sudo systemctl start ${APP_NAME}"