From e5765289012147c3a7cf875d21c41ae7d31e98df Mon Sep 17 00:00:00 2001 From: Vince Grassia <593223+vgrassia@users.noreply.github.com> Date: Thu, 9 Nov 2023 13:32:03 -0500 Subject: [PATCH] Change logic for building --- .github/workflows/build.yml | 38 ++++++++- build.Dockerfile | 151 ++++++++++++++++++++++++++++++++++++ src/Admin/Dockerfile | 63 +++------------ 3 files changed, 196 insertions(+), 56 deletions(-) create mode 100644 build.Dockerfile diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 503c5045c5..4343afd31d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -195,12 +195,41 @@ jobs: path: ${{ matrix.base_path }}/${{ matrix.project_name }}/${{ matrix.project_name }}.zip if-no-files-found: error - build-docker: - name: Build Docker images + build: + name: Build artifacts runs-on: ubuntu-22.04 # needs: # - lint # - testing + steps: + - name: Checkout repo + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + + - name: Set up QEMU emulators + uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # v3.0.0 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 + + - name: Build Docker image + uses: docker/build-push-action@1104d471370f9806843c095c1db02b5a90c5f8b6 # v3.3.1 + with: + context: . + file: build.Dockerfile + platforms: | + linux/amd64, + linux/arm/v7, + linux/arm64/v8 + push: false + tags: bitwarden-build + # secrets: | + # "GH_PAT=${{ steps.retrieve-secret-pat.outputs.github-pat-bitwarden-devops-bot-repo-scope }}" + + build-docker: + name: Build Docker images + runs-on: ubuntu-22.04 + needs: + - build strategy: fail-fast: false matrix: @@ -254,6 +283,11 @@ jobs: echo "is_publish_branch=false" >> $GITHUB_ENV fi + - name: Docker Test Step + run: | + docker image ls + exit 1 + ########## Set up Docker ########## - name: Set up QEMU emulators uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # v3.0.0 diff --git a/build.Dockerfile b/build.Dockerfile new file mode 100644 index 0000000000..a04449c372 --- /dev/null +++ b/build.Dockerfile @@ -0,0 +1,151 @@ +############################################### +# Build stage # +############################################### +FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:6.0 + +# Docker buildx supplies the value for this arg +ARG TARGETPLATFORM +ENV NODE_VERSION=16.20.2 +ENV NVM_DIR /usr/local/nvm + +# Determine proper runtime value for .NET +# We put the value in a file to be read by later layers. +RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \ + RID=linux-x64 ; \ + elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then \ + RID=linux-arm64 ; \ + elif [ "$TARGETPLATFORM" = "linux/arm/v7" ]; then \ + RID=linux-arm ; \ + fi \ + && echo "RID=$RID" > /tmp/rid.txt + +# Add packages +RUN apt-get update && apt-get install -y --no-install-recommends \ + curl \ + && rm -rf /var/lib/apt/lists/* + +# Set up Node +RUN mkdir -p $NVM_DIR +RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash \ + && . $NVM_DIR/nvm.sh \ + && nvm install $NODE_VERSION \ + && nvm alias default $NODE_VERSION \ + && nvm use default +ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules +ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH + +# Install gulp +RUN npm install -g gulp + +# Copy csproj files as distinct layers +WORKDIR /source +COPY src/Admin/*.csproj ./src/Admin/ +COPY src/Api/*.csproj ./src/Api/ +COPY src/Events/*.csproj ./src/Events/ +COPY src/Icons/*.csproj ./src/Icons/ +COPY src/Identity/*.csproj ./src/Identity/ +COPY src/Notifications/*.csproj ./src/Notifications/ +COPY bitwarden_license/src/Sso/*.csproj ./bitwarden_license/src/Sso/ +COPY bitwarden_license/src/Scim/*.csproj ./bitwarden_license/src/Scim/ +COPY src/Core/*.csproj ./src/Core/ +COPY src/Infrastructure.Dapper/*.csproj ./src/Infrastructure.Dapper/ +COPY src/Infrastructure.EntityFramework/*.csproj ./src/Infrastructure.EntityFramework/ +COPY src/SharedWeb/*.csproj ./src/SharedWeb/ +COPY util/Migrator/*.csproj ./util/Migrator/ +COPY util/MySqlMigrations/*.csproj ./util/MySqlMigrations/ +COPY util/PostgresMigrations/*.csproj ./util/PostgresMigrations/ +COPY util/SqliteMigrations/*.csproj ./util/SqliteMigrations/ +COPY bitwarden_license/src/Commercial.Core/*.csproj ./bitwarden_license/src/Commercial.Core/ +COPY bitwarden_license/src/Commercial.Infrastructure.EntityFramework/*.csproj ./bitwarden_license/src/Commercial.Infrastructure.EntityFramework/ +COPY Directory.Build.props . + +# Restore Admin project dependencies and tools +WORKDIR /source/src/Admin +RUN . /tmp/rid.txt && dotnet restore -r $RID + +# Restore Api project dependencies and tools +WORKDIR /source/src/Api +RUN . /tmp/rid.txt && dotnet restore -r $RID + +# Restore Events project dependencies and tools +WORKDIR /source/src/Events +RUN . /tmp/rid.txt && dotnet restore -r $RID + +# Restore Icons project dependencies and tools +WORKDIR /source/src/Icons +RUN . /tmp/rid.txt && dotnet restore -r $RID + +# Restore Identity project dependencies and tools +WORKDIR /source/src/Identity +RUN . /tmp/rid.txt && dotnet restore -r $RID + +# Restore Notifications project dependencies and tools +WORKDIR /source/src/Notifications +RUN . /tmp/rid.txt && dotnet restore -r $RID + +# Restore Sso project dependencies and tools +WORKDIR /source/bitwarden_license/src/Sso +RUN . /tmp/rid.txt && dotnet restore -r $RID + +# Restore Scim project dependencies and tools +WORKDIR /source/bitwarden_license/src/Scim +RUN . /tmp/rid.txt && dotnet restore -r $RID + +# Copy required project files +WORKDIR /source +COPY src/Admin/. ./src/Admin/ +COPY src/Api/. ./src/Api/ +COPY src/Events/. ./src/Events/ +COPY src/Icons/. ./src/Icons/ +COPY src/Identity/. ./src/Identity/ +COPY src/Notifications/. ./src/Notifications/ +COPY bitwarden_license/src/Sso/. ./bitwarden_license/src/Sso/ +COPY bitwarden_license/src/Scim/. ./bitwarden_license/src/Scim/ +COPY src/Core/. ./src/Core/ +COPY src/Infrastructure.Dapper/. ./src/Infrastructure.Dapper/ +COPY src/Infrastructure.EntityFramework/. ./src/Infrastructure.EntityFramework/ +COPY src/SharedWeb/. ./src/SharedWeb/ +COPY util/Migrator/. ./util/Migrator/ +COPY util/MySqlMigrations/. ./util/MySqlMigrations/ +COPY util/PostgresMigrations/. ./util/PostgresMigrations/ +COPY util/SqliteMigrations/. ./util/SqliteMigrations/ +COPY util/EfShared/. ./util/EfShared/ +COPY bitwarden_license/src/Commercial.Core/. ./bitwarden_license/src/Commercial.Core/ +COPY bitwarden_license/src/Commercial.Infrastructure.EntityFramework/. ./bitwarden_license/src/Commercial.Infrastructure.EntityFramework/ +COPY .git/. ./.git/ + +# Build Admin app +WORKDIR /source/src/Admin +RUN npm install +RUN gulp --gulpfile "gulpfile.js" build +RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Admin --no-restore --no-self-contained -r $RID + +# Build Api app +WORKDIR /source/src/Api +RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Api --no-restore --no-self-contained -r $RID + +# Build Events app +WORKDIR /source/src/Events +RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Events --no-restore --no-self-contained -r $RID + +# Build Icons app +WORKDIR /source/src/Icons +RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Icons --no-restore --no-self-contained -r $RID + +# Build Identity app +WORKDIR /source/src/Identity +RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Identity --no-restore --no-self-contained -r $RID + +# Build Notifications app +WORKDIR /source/src/Notifications +RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Notifications --no-restore --no-self-contained -r $RID + +# Build Sso app +WORKDIR /source/bitwarden_license/src/Sso +RUN npm install +RUN gulp --gulpfile "gulpfile.js" build +RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Sso --no-restore --no-self-contained -r $RID + +# Build Scim app +WORKDIR /source/bitwarden_license/src/Scim +RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Scim --no-restore --no-self-contained -r $RID diff --git a/src/Admin/Dockerfile b/src/Admin/Dockerfile index 6e994d8702..f6c1a50439 100644 --- a/src/Admin/Dockerfile +++ b/src/Admin/Dockerfile @@ -1,57 +1,13 @@ -FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:6.0 AS dotnet-build - -# Docker buildx supplies the value for this arg -ARG TARGETPLATFORM -ENV NODE_VERSION=16.20.2 -ENV NVM_DIR /usr/local/nvm - -# Determine proper runtime value for .NET -# We put the value in a file to be read by later layers. -RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \ - RID=linux-x64 ; \ - elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then \ - RID=linux-arm64 ; \ - elif [ "$TARGETPLATFORM" = "linux/arm/v7" ]; then \ - RID=linux-arm ; \ - fi \ - && echo "RID=$RID" > /tmp/rid.txt - -# Add packages -RUN apt-get update && apt-get install -y --no-install-recommends \ - curl \ - && rm -rf /var/lib/apt/lists/* - -# Add packages -RUN mkdir -p $NVM_DIR -RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash \ - && . $NVM_DIR/nvm.sh \ - && nvm install $NODE_VERSION \ - && nvm alias default $NODE_VERSION \ - && nvm use default -ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules -ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH - -RUN npm install -g gulp - -WORKDIR /source -COPY src/Admin/*.csproj . -COPY Directory.Build.props . - -# Restore Admin project dependencies and tools -RUN . /tmp/rid.txt && dotnet restore -r $RID - -COPY src/Admin/. . -#COPY ../../.git/. ./.git/ - -# Build Admin app -RUN npm install -RUN gulp --gulpfile "gulpfile.js" build -RUN . /tmp/rid.txt && dotnet publish -c release -o /app/Admin --no-restore --no-self-contained -r $RID +############################################### +# Build stage # +############################################### +FROM --platform=$BUILDPLATFORM bitwarden-build AS bitwarden-build ############################################### # App stage # ############################################### FROM mcr.microsoft.com/dotnet/aspnet:6.0 + ARG TARGETPLATFORM LABEL com.bitwarden.product="bitwarden" ENV ASPNETCORE_ENVIRONMENT=Production @@ -60,15 +16,14 @@ EXPOSE 5000 RUN apt-get update \ && apt-get install -y --no-install-recommends \ - curl \ gosu \ + curl \ && rm -rf /var/lib/apt/lists/* -# Copy all apps from dotnet-build stage +# Copy all apps from the build stage WORKDIR /app -COPY --from=dotnet-build /app ./ - -COPY src/Admin/entrypoint.sh / +COPY --from=bitwarden-build /app/Admin ./ +COPY entrypoint.sh / RUN chmod +x /entrypoint.sh HEALTHCHECK CMD curl -f http://localhost:5000 || exit 1