merge ci steps

This commit is contained in:
Aleksandr Trushkin
2021-01-10 15:52:45 +03:00
parent e120989e5c
commit dd53a86c2d
5 changed files with 257 additions and 29 deletions

89
.drone.jsonnet Normal file
View File

@ -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",
// }
}

View File

@ -1,42 +1,96 @@
--- ---
kind: pipeline kind: pipeline
type: docker type: docker
name: build-check-deploy name: default
platform: platform:
os: "linux" os: linux
arch: "arm" arch: arm
steps: steps:
# Fetch deps and check the code - name: validate
- name: "validate" image: rust:1.49
image: "rust:1.49"
volumes:
- name: "cache"
path: "./target"
commands: commands:
- "apt-get update && apt-get install -yqq musl-tools" - cargo test --release --target=armv7-unknown-linux-gnueabihf
- "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: environment:
SSH_PRIVATE_KEY: APP_NAME: deploytest
from_secret: SSH_PRIVATE_KEY CARGO_TARGET_DIR: /cache/target
SSH_PARAMS: '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/tmp/known_hosts' volumes:
TARGET_DIR: "./.target/armv7-unknown-linux-musleabihf/release" - name: cargo
path: /usr/local/cargo
- name: target
path: /cache/target
- name: rustup
path: /usr/local/rustup
- name: test
image: rust:1.49
commands: commands:
# Start the SSH Agent and insert ssh private key. - cargo test --release --target=armv7-unknown-linux-gnueabihf
- "eval $(ssh-agent -s)" environment:
- "echo '${SSH_PRIVATE_KEY}' | tr -d '\r' | ssh-add -" APP_NAME: deploytest
# Copy the binary file to it. CARGO_TARGET_DIR: /cache/target
- "scp ${SSH_PARAMS} ${TARGET_DIR}/alterego ${SSH_USER}@${SSH_HOST}:/home/pi/alterego" 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: volumes:
- name: "cache" - name: target
temp: {} temp: {}
- name: cargo
host:
path: /home/pi/.cargo
- name: rustup
host:
path: /home/pi/.rustup
---
kind: signature
hmac: 5d42ee9863b09aa2c5b1140903b659ec25660acdab89f20a6d070c30d46898b0
...

42
.drone.yml.bak Normal file
View File

@ -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: {}

View File

@ -7,6 +7,10 @@ DOCKERFLAGS:=-it --rm \
DOCKERIMG:="rust-build-env:V1" DOCKERIMG:="rust-build-env:V1"
APP_NAME:=altherego
IMAGE:=rust:1.49
TARGET_ARCH:=armv7-unknown-linux-gnueabihf
image: image:
docker build -t rust-build-env:V1 . docker build -t rust-build-env:V1 .
.PHONY: image .PHONY: image
@ -25,3 +29,12 @@ build_release_arm:
docker_build_release_arm: docker_build_release_arm:
docker run ${DOCKERFLAGS} ${DOCKERIMG} make 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

30
scripts/deploy.sh Normal file
View File

@ -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}"