|
| 1 | +# ================================ |
| 2 | +# Build image |
| 3 | +# ================================ |
| 4 | +FROM swift:5.9-jammy as build |
| 5 | + |
| 6 | +# Install OS updates |
| 7 | +RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \ |
| 8 | + && apt-get -q update \ |
| 9 | + && apt-get -q dist-upgrade -y\ |
| 10 | + && rm -rf /var/lib/apt/lists/* |
| 11 | + |
| 12 | +# Set up a build area |
| 13 | +WORKDIR /build |
| 14 | + |
| 15 | +# First just resolve dependencies. |
| 16 | +# This creates a cached layer that can be reused |
| 17 | +# as long as your Package.swift/Package.resolved |
| 18 | +# files do not change. |
| 19 | +COPY ./Package.* ./ |
| 20 | +RUN swift package resolve --skip-update \ |
| 21 | + $([ -f ./Package.resolved ] && echo "--force-resolved-versions" || true) |
| 22 | + |
| 23 | +# Copy entire repo into container |
| 24 | +COPY . . |
| 25 | + |
| 26 | +# Go into the NotifierBot subdirectory for building |
| 27 | +WORKDIR /build/NotifierBot |
| 28 | + |
| 29 | +# Build everything, with optimizations |
| 30 | +RUN swift build -c release --static-swift-stdlib \ |
| 31 | + # Workaround for https://github.com/apple/swift/pull/68669 |
| 32 | + # This can be removed as soon as 5.9.1 is released, but is harmless if left in. |
| 33 | + -Xlinker -u -Xlinker _swift_backtrace_isThunkFunction |
| 34 | + |
| 35 | +# Switch to the staging area |
| 36 | +WORKDIR /staging |
| 37 | + |
| 38 | +# Copy main executable to staging area |
| 39 | +RUN cp "$(swift build --package-path /build/NotifierBot -c release --show-bin-path)/App" ./ |
| 40 | + |
| 41 | +# Copy resources bundled by SPM to staging area |
| 42 | +RUN find -L "$(swift build --package-path /build/NotifierBot -c release --show-bin-path)/" -regex '.*\.resources$' -exec cp -Ra {} ./ \; |
| 43 | + |
| 44 | +# ================================ |
| 45 | +# Run image |
| 46 | +# ================================ |
| 47 | +FROM swift:5.9-jammy-slim |
| 48 | + |
| 49 | +# Make sure all system packages are up to date, and install only essential packages. |
| 50 | +RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \ |
| 51 | + && apt-get -q update \ |
| 52 | + && apt-get -q dist-upgrade -y \ |
| 53 | + && apt-get -q install -y \ |
| 54 | + ca-certificates \ |
| 55 | + tzdata \ |
| 56 | +# If your app or its dependencies import FoundationNetworking, also install `libcurl4`. |
| 57 | + libcurl4 \ |
| 58 | +# If your app or its dependencies import FoundationXML, also install `libxml2`. |
| 59 | + # libxml2 \ |
| 60 | + && rm -r /var/lib/apt/lists/* |
| 61 | + |
| 62 | +# Create a bot user and group with /app as its home directory |
| 63 | +RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app bot |
| 64 | + |
| 65 | +# Switch to the new home directory |
| 66 | +WORKDIR /app |
| 67 | + |
| 68 | +# Copy built executable and any staged resources from builder |
| 69 | +COPY --from=build --chown=bot:bot /staging /app |
| 70 | + |
| 71 | +# Provide configuration needed by the built-in crash reporter and some sensible default behaviors. |
| 72 | +ENV SWIFT_ROOT=/usr SWIFT_BACKTRACE=enable=yes,sanitize=yes,threads=all,images=all,interactive=no |
| 73 | + |
| 74 | +# Ensure all further commands run as the bot user |
| 75 | +USER bot:bot |
| 76 | + |
| 77 | +# Start the bot when the image is run |
| 78 | +ENTRYPOINT ["./App"] |
| 79 | +CMD [] |
0 commit comments