Compare commits

..

3 Commits

Author SHA1 Message Date
39f9b997ed
Fix: Only master branch should push the docker image
All checks were successful
Build and deploy automation_rs / Build (push) Successful in 4m57s
Build and deploy automation_rs / Create container (push) Successful in 57s
Build and deploy automation_rs / Deploy Docker container (push) Has been skipped
2023-11-23 00:26:24 +01:00
cdb02eb5dd
Feature: Deploy Docker container after it is created
All checks were successful
Build and deploy automation_rs / Build (push) Successful in 4m47s
Build and deploy automation_rs / Create container (push) Successful in 1m2s
Build and deploy automation_rs / Deploy Docker container (push) Has been skipped
2023-11-22 01:17:30 +01:00
c77064b5b9
Feature: Use Gitea Actions to build automation_rs
All checks were successful
Build and deploy automation_rs / Build (push) Successful in 6m39s
Build and deploy automation_rs / Create Docker container (push) Successful in 1m1s
Builds automation_rs and the corresponding docker image.
The binary is uploaded as an artifact and the image is uploaded to the
registry.

In order to improve caching the nightly version is locked using
rust-toolchain.toml
2023-11-22 00:40:05 +01:00
4 changed files with 109 additions and 108 deletions

View File

@ -1,49 +0,0 @@
kind: pipeline
type: docker
name: default
steps:
- name: build
image: docker
volumes:
- name: socket
path: /var/run/docker.sock
commands:
- DOCKER_BUILDKIT=1 docker build -t automation_rs .
- name: deploy
image: docker
volumes:
- name: socket
path: /var/run/docker.sock
environment:
MQTT_PASSWORD:
from_secret: MQTT_PASSWORD
HUE_TOKEN:
from_secret: HUE_TOKEN
NTFY_TOPIC:
from_secret: NTFY_TOPIC
RUST_LOG:
from_secret: RUST_LOG
commands:
- docker stop automation_rs || true
- docker rm automation_rs || true
# Networks need to be setup to to allow broadcasts: https://www.devwithimagination.com/2020/06/15/homebridge-docker-and-wake-on-lan/ https://github.com/dhutchison/container-images/blob/0c2d7d96bab751fb0a008cc91ba2990724bbd11f/homebridge/configure_docker_networks_for_wol.sh
# Needs to be done for ALL networks, because we can't seem to control which interface gets used to send the broadcast
- docker create -e RUST_LOG=$RUST_LOG -e MQTT_PASSWORD=$MQTT_PASSWORD -e HUE_TOKEN=$HUE_TOKEN -e NTFY_TOPIC=$NTFY_TOPIC --network mqtt --restart unless-stopped --name automation_rs automation_rs
- docker network connect web automation_rs
- docker start automation_rs
when:
branch:
- master
event:
exclude:
- pull_request
volumes:
- name: socket
host:
path: /var/run/docker.sock

102
.gitea/workflows/build.yml Normal file
View File

@ -0,0 +1,102 @@
# Based on: https://pastebin.com/99Fq2b2w
name: Build and deploy automation_rs
on:
push:
branches:
- main
- feature/actions
jobs:
build:
name: Build
runs-on: ubuntu-latest
container: catthehacker/ubuntu:act-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
rustflags: ""
- name: Formatting
uses: actions-rust-lang/rustfmt@v1
- name: Clippy
run: cargo clippy --all-targets --all -- -D warnings
- name: Build
run: cargo build --release
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: build
path: target/x86_64-unknown-linux-gnu/release/automation
container:
name: Create container
runs-on: ubuntu-latest
needs: [build]
container: catthehacker/ubuntu:act-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: build
- name: Set permissions
run: |
chown 65532:65532 ./build/*
chmod 0755 ./build/*
- name: Login to registry
uses: https://github.com/docker/login-action@v3
with:
registry: git.huizinga.dev
username: ${{ gitea.actor }}
password: ${{ secrets.REGISTRY_TOKEN }}
- name: Build & Push Docker Image
uses: https://github.com/docker/build-push-action@v5
with:
context: .
push: ${{ gitea.ref == 'refs/heads/master' }}
# TODO: Automatically add the correct tags here
tags: git.huizinga.dev/dreaded_x/automation_rs:latest
deploy:
name: Deploy Docker container
runs-on: ubuntu-latest
container: catthehacker/ubuntu:act-latest
needs: [container]
if: gitea.ref == 'refs/heads/master'
steps:
- name: Stop and remove current container
run: |
docker stop automation_rs || true
docker rm automation_rs || true
- name: Create container
run: |
docker create \
--pull always \
--restart unless-stopped \
--name automation_rs \
-e RUST_LOG=automation=debug \
-e MQTT_PASSWORD=${{ secrets.MQTT_PASSWORD }} \
-e HUE_TOKEN=${{ secrets.HUE_TOKEN }} \
-e NTFY_TOPIC=${{ secrets.NTFY_TOPIC }} \
git.huizinga.dev/dreaded_x/automation_rs:latest
docker network connect mqtt automation_rs
docker network connect web automation_rs
- name: Start container
run: docker start automation_rs
# TODO: Perform a healthcheck

View File

@ -1,64 +1,8 @@
FROM rust:bookworm AS build FROM gcr.io/distroless/cc-debian12:nonroot
# Create user
ENV USER=automation
ENV UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
"${USER}"
# Create basic project structure
RUN cargo new --bin /app
RUN cargo new --lib /app/impl_cast && truncate -s 0 /app/impl_cast/src/lib.rs
RUN cargo new --lib /app/google-home
# Get the correct version of the compiler
RUN rustup default nightly
# Copy cargo config
COPY .cargo/config.toml /app/.cargo/config.toml
# Copy the Cargo.toml files
COPY impl_cast/Cargo.toml /app/impl_cast
COPY google-home/Cargo.toml /app/google-home
COPY Cargo.toml Cargo.lock /app/
# Download and build all the dependencies
WORKDIR /app
RUN --mount=type=cache,target=/usr/local/cargo/registry cargo build --release
# Build impl_cast
COPY impl_cast/src/ /app/impl_cast/src/
RUN --mount=type=cache,target=/usr/local/cargo/registry set -e; touch /app/impl_cast/src/lib.rs; cargo build --release --package impl_cast
# Build google-home
COPY google-home/src/ /app/google-home/src/
RUN --mount=type=cache,target=/usr/local/cargo/registry set -e; touch /app/google-home/src/lib.rs; cargo build --release --package google-home
# Build automation
COPY src/ /app/src/
RUN --mount=type=cache,target=/usr/local/cargo/registry set -e; touch /app/src/main.rs /app/src/lib.rs /app/google-home/src/lib.rs /app/impl_cast/src/lib.rs; cargo build --release
CMD ["/app/target/release/automation"]
# FINAL IMAGE
FROM gcr.io/distroless/cc-debian12:latest
COPY --from=build /etc/passwd /etc/passwd
COPY --from=build /etc/group /etc/group
ENV AUTOMATION_CONFIG=/app/config.yml ENV AUTOMATION_CONFIG=/app/config.yml
COPY config/config.yml /app/config.yml COPY ./config/config.yml /app/config.yml
WORKDIR /app COPY ./build/automation /app/automation
COPY --from=build /app/target/x86_64-unknown-linux-gnu/release/automation ./
USER automation:automation
CMD ["/app/automation"] CMD ["/app/automation"]

4
rust-toolchain.toml Normal file
View File

@ -0,0 +1,4 @@
[toolchain]
channel = "nightly-2023-11-15"
components = ["rustfmt", "clippy"]
profile = "minimal"