From f92cd864e626c949d9264b5ef4a6b6cea1ac85d2 Mon Sep 17 00:00:00 2001 From: JP Simard Date: Tue, 17 Oct 2023 15:16:30 -0400 Subject: [PATCH] Add Dockerfile (#1211) * Add Dockerfile To simplify building on Linux: ```console docker build -t sourcery-image . docker run sourcery-image sourcery --help ``` I've kept the resulting Docker image small(-ish) at the expense of probably removing some useful functionality. I haven't tested this thoroughly but I'll leave it to other contributors can expand this as needed. * Simplfy build flags Since the runtime image is also Swift. * Add CI job to build and publish docker images to GitHub Packages Will run on commits to the `master` branch and tags. --- .github/workflows/docker.yml | 46 ++++++++++++++++++++++++++++++++++++ Dockerfile | 45 +++++++++++++++++++++++++++++++++++ LINUX.md | 14 +++++++++++ 3 files changed, 105 insertions(+) create mode 100644 .github/workflows/docker.yml create mode 100644 Dockerfile diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 000000000..274420395 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,46 @@ +name: Docker + +on: + push: + branches: + - master + tags: + - '*' + +jobs: + build: + runs-on: ubuntu-22.04 + + steps: + - uses: actions/checkout@v3 + + - name: Extract DOCKER_TAG using tag name + if: startsWith(github.ref, 'refs/tags/') + run: | + echo "DOCKER_TAG=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV + + - name: Use default DOCKER_TAG + if: startsWith(github.ref, 'refs/tags/') != true + run: | + echo "DOCKER_TAG=latest" >> $GITHUB_ENV + + - name: Set lowercase repository name + run: | + echo "REPOSITORY_LC=${REPOSITORY,,}" >>${GITHUB_ENV} + env: + REPOSITORY: '${{ github.repository }}' + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to GitHub registry + uses: docker/login-action@v1 + with: + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + registry: ghcr.io + + - uses: docker/build-push-action@v2 + with: + push: true + tags: ghcr.io/${{ env.REPOSITORY_LC }}:${{ env.DOCKER_TAG }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..0840cec04 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,45 @@ +ARG BUILDER_IMAGE=swift:5.9-jammy +ARG RUNTIME_IMAGE=swift:5.9-jammy-slim + +# Builder image +FROM ${BUILDER_IMAGE} AS builder +RUN apt-get update && apt-get install -y \ + build-essential \ + libffi-dev \ + libncurses5-dev \ + libsqlite3-dev \ + && rm -r /var/lib/apt/lists/* +WORKDIR /workdir/ +COPY Sourcery Sourcery/ +COPY SourceryExecutable SourceryExecutable/ +COPY SourceryFramework SourceryFramework/ +COPY SourceryJS SourceryJS/ +COPY SourceryRuntime SourceryRuntime/ +COPY SourceryStencil SourceryStencil/ +COPY SourcerySwift SourcerySwift/ +COPY SourceryTests SourceryTests/ +COPY SourceryUtils SourceryUtils/ +COPY Plugins Plugins/ +COPY Templates Templates/ +COPY Tests Tests/ +COPY Package.* ./ + +RUN swift package update +ARG SWIFT_FLAGS="-c release" +RUN swift build $SWIFT_FLAGS --product sourcery +RUN mv `swift build $SWIFT_FLAGS --show-bin-path`/sourcery /usr/bin +RUN sourcery --version + +# Runtime image +FROM ${RUNTIME_IMAGE} +LABEL org.opencontainers.image.source https://github.com/krzysztofzablocki/Sourcery +RUN apt-get update && apt-get install -y \ + libcurl4 \ + libsqlite3-0 \ + libxml2 \ + && rm -r /var/lib/apt/lists/* +COPY --from=builder /usr/bin/sourcery /usr/bin + +RUN sourcery --version + +CMD ["sourcery"] diff --git a/LINUX.md b/LINUX.md index af044175b..35333eeaa 100644 --- a/LINUX.md +++ b/LINUX.md @@ -13,6 +13,20 @@ All issues related to Linux will be mentioned in #1198 Simply add package dependency of Sourcery as described in the [README](README.md). +## Using Sourcery with Docker + +You can build Docker container with Sourcery installed using this command: + +```console +docker build -t sourcery-image . +``` + +Then you can run this Docker image passing the arguments you'd like: + +```console +docker run sourcery-image sourcery --help +``` + ## Contributing ### Installation of Linux Environment