diff options
Diffstat (limited to 'src/content/tils/2020/11/12')
-rw-r--r-- | src/content/tils/2020/11/12/diy-nix-bash-ci.adoc | 74 | ||||
-rw-r--r-- | src/content/tils/2020/11/12/git-bisect-automation.adoc | 35 | ||||
-rw-r--r-- | src/content/tils/2020/11/12/useful-bashvars.adoc | 72 |
3 files changed, 181 insertions, 0 deletions
diff --git a/src/content/tils/2020/11/12/diy-nix-bash-ci.adoc b/src/content/tils/2020/11/12/diy-nix-bash-ci.adoc new file mode 100644 index 0000000..3336482 --- /dev/null +++ b/src/content/tils/2020/11/12/diy-nix-bash-ci.adoc @@ -0,0 +1,74 @@ +--- + +title: DIY bare bones CI server with Bash and Nix + +date: 2020-11-12 3 + +layout: post + +lang: en + +ref: diy-bare-bones-ci-server-with-bash-and-nix + +eu_categories: ci + +--- + +With a server with Nix installed (no need for NixOS), you can leverage its build +isolation for running CI jobs by adding a [post-receive][post-receive] Git hook +to the server. + +In most of my project I like to keep a `test` attribute which runs the test with +`nix-build -A test`. This way, a post-receive hook could look like: + +```shell +#!/usr/bin/env bash +set -Eeuo pipefail +set -x + +LOGS_DIR="/data/static/ci-logs/libedn" +mkdir -p "$LOGS_DIR" +LOGFILE="${LOGS_DIR}/$(date -Is)-$(git rev-parse master).log" +exec &> >(tee -a "${LOGFILE}") + +unset GIT_DIR +CLONE="$(mktemp -d)" +git clone . "$CLONE" +pushd "$CLONE" + +finish() { + printf "\n\n>>> exit status was %s\n" "$?" +} +trap finish EXIT + +nix-build -A test +``` + +We initially (lines #5 to #8) create a log file, named after *when* the run is +running and for *which* commit it is running for. The `exec` and `tee` combo +allows the output of the script to go both to `stdout` *and* the log file. This +makes the logs output show up when you do a `git push`. + +Lines #10 to #13 create a fresh clone of the repository and line #20 runs the +test command. + +After using a similar post-receive hook for a while, I now even generate a +simple HTML file to make the logs available ([example project][ci-logs]) +through the browser. + +[post-receive]: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks +[ci-logs]: https://euandreh.xyz/remembering/ci.html + +## Upsides + +No vendor lock-in, as all you need is a server with Nix installed. + +And if you pin the Nixpkgs version you're using, this very simple setup yields +extremely sandboxed runs on a very hermetic environment. + +## Downsides + +Besides the many missing shiny features of this very simplistic CI, `nix-build` +can be very resource intensive. Specifically, it consumes too much memory. So if +it has to download too many things, or the build closure gets too big, the +server might very well run out of memory. diff --git a/src/content/tils/2020/11/12/git-bisect-automation.adoc b/src/content/tils/2020/11/12/git-bisect-automation.adoc new file mode 100644 index 0000000..9c34b2a --- /dev/null +++ b/src/content/tils/2020/11/12/git-bisect-automation.adoc @@ -0,0 +1,35 @@ +--- + +title: Git bisect automation + +date: 2020-11-12 2 + +layout: post + +lang: en + +ref: git-bisect-automation + +eu_categories: git + +--- + +It is good to have an standardized way to run builds and tests on the repository +of a project, so that you can find when a bug was introduced by using +`git bisect run`. + +I've already been in the situation when a bug was introduced and I didn't know +how it even was occurring, and running Git bisect over hundreds of commits to +pinpoint the failing commit was very empowering: + +``` +$ GOOD_COMMIT_SHA=e1fd0a817d192c5a5df72dd7422e36558fa78e46 +$ git bisect start HEAD $GOOD_COMMIT_SHA +$ git bisect run sn -c './build.sh && ./run-failing-case.sh' +``` + +Git will than do a binary search between the commits, and run the commands you +provide it with to find the failing commit. + +Instead of being afraid of doing a bisect, you should instead leverage it, and +make Git help you dig through the history of the repository to find the bad code. diff --git a/src/content/tils/2020/11/12/useful-bashvars.adoc b/src/content/tils/2020/11/12/useful-bashvars.adoc new file mode 100644 index 0000000..33a072e --- /dev/null +++ b/src/content/tils/2020/11/12/useful-bashvars.adoc @@ -0,0 +1,72 @@ +--- + +title: Useful Bash variables + +date: 2020-11-12 1 + +layout: post + +lang: en + +ref: useful-bash-variables + +eu_categories: shell + +--- + +[GNU Bash][gnu-bash] has a few two letter variables that may be useful when +typing on the terminal. + +[gnu-bash]: https://www.gnu.org/software/bash/ + +## `!!`: the text of the last command + +The [`!!` variable][previous-command] refers to the previous command, and I find +useful when following chains for symlinks: + +[previous-command]: https://www.gnu.org/software/bash/manual/bash.html#Event-Designators + +```shell +$ which git +/run/current-system/sw/bin/git +$ readlink $(!!) +readlink $(which git) +/nix/store/5bgr1xpm4m0r72h9049jbbhagxdyrnyb-git-2.28.0/bin/git +``` + +It is also useful when you forget to prefix `sudo` to a command that requires +it: + +```shell +$ requires-sudo.sh +requires-sudo.sh: Permission denied +$ sudo !! +sudo ./requires-sudo.sh +# all good +``` + +Bash prints the command expansion before executing it, so it is better for you +to follow along what it is doing. + +## `$_`: most recent parameter + +The [`$_` variable][recent-parameter] will give you the most recent parameter +you provided to a previous argument, which can save you typing sometimes: + +```shell +# instead of... +$ mkdir -p a/b/c/d/ +$ cd a/b/c/d/ + +# ...you can: +$ mkdir -p a/b/c/d/ +$ cd $_ +``` + +[recent-parameter]: https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters + +## Conclusion + +I wouldn't use those in a script, as it would make the script terser to read, I +find those useful shortcut that are handy when writing at the interactive +terminal. |