From c9c91121556c9857e4ad902a3071fa05b26b221f Mon Sep 17 00:00:00 2001 From: Fahmi Akbar Wildana Date: Sun, 8 Sep 2019 21:38:25 +0700 Subject: [PATCH] Build and publish docker img along with nu plugins * Add Package.Dockerfile as flexible build source * Add docker-compose.package.yml as intermediary config * CI will use new github action YAML format it only publish the docker image on git tag * Add debian:latest, debian:slim, and alpine as base image * Add documentation --- .editorconfig | 7 ++- .github/main.workflow | 8 --- .github/workflows/docker-publish.yml | 80 ++++++++++++++++++++++++++++ docker/Package.Dockerfile | 5 ++ docker/docker-compose.package.yml | 10 ++++ docs/docker.md | 46 ++++++++++++++++ 6 files changed, 147 insertions(+), 9 deletions(-) delete mode 100644 .github/main.workflow create mode 100644 .github/workflows/docker-publish.yml create mode 100644 docker/Package.Dockerfile create mode 100644 docker/docker-compose.package.yml create mode 100644 docs/docker.md diff --git a/.editorconfig b/.editorconfig index f6fb9f98d9..c5d100a733 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,4 +6,9 @@ indent_size = 4 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = false -end_of_line = lf \ No newline at end of file +end_of_line = lf + +[*.{yml,yaml}] +indent_size = 2 +charset = utf-8 +insert_final_newline = true \ No newline at end of file diff --git a/.github/main.workflow b/.github/main.workflow deleted file mode 100644 index cbbe6aea81..0000000000 --- a/.github/main.workflow +++ /dev/null @@ -1,8 +0,0 @@ -workflow "New workflow" { - resolves = ["GitHub Action for Docker"] - on = "push" -} - -action "GitHub Action for Docker" { - uses = "actions/docker/cli@fe7ed3ce992160973df86480b83a2f8ed581cd50" -} diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000000..a3c3e0f2af --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,80 @@ +name: Publish consumable Docker images + +on: + push: + tags: ['*.*.*'] + +jobs: + compile: + runs-on: ubuntu-latest + strategy: + matrix: + arch: + - x86_64-unknown-linux-musl + - x86_64-unknown-linux-gnu + steps: + - uses: actions/checkout@v1 + - run: cargo install cross + - name: compile for specific target + env: { arch: '${{ matrix.arch }}' } + run: | + cross build --target ${{ matrix.arch }} --release + # leave only the executable file + rm -rd target/${{ matrix.arch }}/release/{*/*,*.d,*.rlib,.fingerprint} + find . -empty -delete + - uses: actions/upload-artifact@master + with: + name: ${{ matrix.arch }} + path: target/${{ matrix.arch }}/release + + docker: + name: Build and publish docker images + needs: compile + runs-on: ubuntu-latest + strategy: + matrix: + base-image: [debian, 'debian:stable-slim', alpine] + include: + - { tag: alpine, base-image: alpine, arch: x86_64-unknown-linux-musl } + - { tag: slim, base-image: 'debian:stable-slim', arch: x86_64-unknown-linux-gnu } + - { tag: debian, base-image: debian, arch: x86_64-unknown-linux-gnu } + steps: + - uses: actions/checkout@v1 + - uses: actions/download-artifact@master + with: { name: '${{ matrix.arch }}', path: target/release } + - name: Build and publish exact version + run: | + REGISTRY=${REGISTRY,,}; export TAG=${GITHUB_REF##*/}-${{ matrix.tag }}; + + echo ${{ secrets.DOCKER_REGISTRY }} | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin + docker-compose --file docker/docker-compose.package.yml build + docker-compose --file docker/docker-compose.package.yml push # exact version + env: + BASE_IMAGE: ${{ matrix.base-image }} + REGISTRY: docker.pkg.github.com/${{ github.repository }} + + #region semantics tagging + - name: Retag and push without suffixing version + run: | + VERSION=${GITHUB_REF##*/} + docker tag ${REGISTRY,,}/nu:${VERSION}-${{ matrix.tag }} ${REGISTRY,,}/nu:${{ matrix.tag }} + docker tag ${REGISTRY,,}/nu:${VERSION}-${{ matrix.tag }} ${REGISTRY,,}/nu:${VERSION%%.*}-${{ matrix.tag }} + docker tag ${REGISTRY,,}/nu:${VERSION}-${{ matrix.tag }} ${REGISTRY,,}/nu:${VERSION%.*}-${{ matrix.tag }} + docker push ${REGISTRY,,}/nu:${VERSION%.*}-${{ matrix.tag }} # latest patch + docker push ${REGISTRY,,}/nu:${VERSION%%.*}-${{ matrix.tag }} # latest features + docker push ${REGISTRY,,}/nu:${{ matrix.tag }} # latest version + env: { REGISTRY: 'docker.pkg.github.com/${{ github.repository }}' } + - name: Retag and push debian as latest + if: matrix.tag == 'debian' + run: | + VERSION=${GITHUB_REF##*/} + docker tag ${REGISTRY,,}/nu:${{ matrix.tag }} ${REGISTRY,,}/nu:latest + docker tag ${REGISTRY,,}/nu:${VERSION}-${{ matrix.tag }} ${REGISTRY,,}/nu:${VERSION%.*} + docker tag ${REGISTRY,,}/nu:${VERSION}-${{ matrix.tag }} ${REGISTRY,,}/nu:${VERSION%%.*} + docker tag ${REGISTRY,,}/nu:${VERSION}-${{ matrix.tag }} ${REGISTRY,,}/nu:${VERSION} + docker push ${REGISTRY,,}/nu:${VERSION} # exact version + docker push ${REGISTRY,,}/nu:${VERSION%%.*} # latest features + docker push ${REGISTRY,,}/nu:${VERSION%.*} # latest patch + docker push ${REGISTRY,,}/nu:latest # latest version + env: { REGISTRY: 'docker.pkg.github.com/${{ github.repository }}' } + #endregion semantics tagging diff --git a/docker/Package.Dockerfile b/docker/Package.Dockerfile new file mode 100644 index 0000000000..8d61b03342 --- /dev/null +++ b/docker/Package.Dockerfile @@ -0,0 +1,5 @@ +ARG base +FROM ${base} + +COPY target/release/nu* /bin/ +ENTRYPOINT ["nu"] \ No newline at end of file diff --git a/docker/docker-compose.package.yml b/docker/docker-compose.package.yml new file mode 100644 index 0000000000..2192da8879 --- /dev/null +++ b/docker/docker-compose.package.yml @@ -0,0 +1,10 @@ +version: '3' + +services: + nushell: + image: ${REGISTRY}/nu:${TAG} + build: + context: .. + dockerfile: docker/Package.Dockerfile + args: + base: ${BASE_IMAGE} diff --git a/docs/docker.md b/docs/docker.md new file mode 100644 index 0000000000..dc8d37988c --- /dev/null +++ b/docs/docker.md @@ -0,0 +1,46 @@ +# Docker Guide + +| tag | base image | plugins | package manager | libs & bins | size | +| ------------------ | -------------------- | ------- | --------------- | ----------------------------------------------------------------------- | ----------- | +| `latest`,`debian` | `debian:latest` | yes | apt | **a lot**, including _glibc_ | ~(48+62) MB | +| `slim` | `debian:stable-slim` | yes | apt | all `nu:debian` image but exclude [this list][.slimify-excludes] | ~(26+62) MB | +| `alpine` | `alpine:latest` | yes | apk | all `nu:musl-busybox` image but include libcrypto, libssl, libtls, libz | ~(3+61) MB | + +[.slimify-excludes]: https://github.com/debuerreotype/debuerreotype/blob/master/scripts/.slimify-excludes +[distroless/base]: https://github.com/GoogleContainerTools/distroless/blob/master/base/README.md + +## Image Variants + +### `nu:` +This is the defacto image. If you are unsure about what your needs are, you probably want to use this one. It is designed to be used both as a throw away container (mount your source code and start the container to start your app), as well as the base to build other images off of. + +
example + +Let say you create a plugin in Rust. +- create a Dockerfile in your root project +```dockerfile +FROM nu:0.2 + +COPY /target/debug/nu_plugin_cowsay /bin/ +ENTRYPOINT ["nu"] +``` +- build your project first then run it via docker +```console +cargo build +docker run -it . +``` +
+ +### `nu:-slim` + +This image does not contain the common packages contained in the default tag and only contains the minimal packages needed to run `nu`. Unless you are working in an environment where only the `nu` image will be deployed and you have space constraints, we highly recommend using the alpine image if you aim for small image size. Only use this image if you really need **both** `glibc` and small image size. + +### `nu:-alpine` +This image is based on the popular [Alpine Linux project](http://alpinelinux.org/), available in [the alpine official image][alpine]. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general. + +This variant is highly recommended when final image size being as small as possible is desired. The main caveat to note is that it does use `musl` libc instead of `glibc` and friends, so certain software might run into issues depending on the depth of their libc requirements. However, most software doesn't have an issue with this, so this variant is usually a very safe choice. See [this Hacker News comment thread](https://news.ycombinator.com/item?id=10782897) for more discussion of the issues that might arise and some pro/con comparisons of using Alpine-based images. + +To minimize image size, it's uncommon for additional related tools (such as `git` or `bash`) to be included in Alpine-based images. Using this image as a base, add the things you need in your own Dockerfile (see the [alpine image description][alpine] for examples of how to install packages if you are unfamiliar). + +[musl]: http://www.musl-libc.org/ +[alpine]: https://hub.docker.com/_/alpine/ \ No newline at end of file