From b770b90ae1dc3dffb72a41deba20dfe4246085f6 Mon Sep 17 00:00:00 2001 From: Marcio Date: Tue, 23 Jul 2024 12:40:34 -0300 Subject: [PATCH] Add GH action / Dockerfile adjust --- .github/workflows/dotnet.yml | 101 ++++++++++++++++++++++++ ActiveDirectoryToRestApi.sln | 1 + src/ActiveDirectoryToRestApi/Dockerfile | 32 ++++---- 3 files changed, 120 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/dotnet.yml diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml new file mode 100644 index 0000000..46a4cc2 --- /dev/null +++ b/.github/workflows/dotnet.yml @@ -0,0 +1,101 @@ +name: Publish + +on: + release: + types: [published] + +jobs: + release: + name: Release + strategy: + matrix: + kind: ['linux', 'linux-arm', 'linux-arm64', 'windows', 'macOS'] + include: + - kind: linux + os: ubuntu-latest + target: linux-x64 + - kind: linux-arm + os: ubuntu-latest + target: linux-arm + - kind: linux-arm64 + os: ubuntu-latest + target: linux-arm64 + - kind: windows + os: windows-latest + target: win-x64 + - kind: macOS + os: macos-latest + target: osx-x64 + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup dotnet + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + + - name: Build + shell: bash + run: | + tag=$(git describe --tags --abbrev=0) + release_name="ActiveDirectoryToRestApi-$tag-${{ matrix.target }}" + + dotnet publish src/ActiveDirectoryToRestApi/ActiveDirectoryToRestApi.csproj --framework net8.0 --runtime "${{ matrix.target }}" --no-self-contained -p:PublishSingleFile=true -c Release -o "$release_name" + + if [ "${{ matrix.target }}" == "win-x64" ]; then + 7z a -tzip "${release_name}.zip" "./${release_name}/*" + else + tar czvf "${release_name}.tar.gz" "$release_name" + fi + + rm -r "$release_name" + + - name: Publish + uses: softprops/action-gh-release@v2 + with: + files: "ActiveDirectoryToRestApi-*" + + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + build: + name: Build image + runs-on: ubuntu-latest + steps: + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: | + zimbres/activedirectory-to-restapi + flavor: | + latest=true + tags: | + type=ref,event=tag + + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to container registry + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + registry: docker.io + + - name: Build and Push Image + uses: docker/build-push-action@v5 + with: + context: . + file: src/ActiveDirectoryToRestApi/Dockerfile + tags: ${{ steps.meta.outputs.tags }} + platforms: linux/amd64,linux/arm64,linux/arm/v7 + push: true diff --git a/ActiveDirectoryToRestApi.sln b/ActiveDirectoryToRestApi.sln index ff0c216..0dfe307 100644 --- a/ActiveDirectoryToRestApi.sln +++ b/ActiveDirectoryToRestApi.sln @@ -7,6 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ActiveDirectoryToRestApi", EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{69C1A7CC-CE13-44EC-924A-104C576C2D34}" ProjectSection(SolutionItems) = preProject + .github\workflows\dotnet.yml = .github\workflows\dotnet.yml LICENSE = LICENSE README.md = README.md EndProjectSection diff --git a/src/ActiveDirectoryToRestApi/Dockerfile b/src/ActiveDirectoryToRestApi/Dockerfile index 7edf0f7..92bbf9d 100644 --- a/src/ActiveDirectoryToRestApi/Dockerfile +++ b/src/ActiveDirectoryToRestApi/Dockerfile @@ -1,20 +1,24 @@ -FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base -RUN apt update && apt install -y --no-install-recommends libldap-2.4-2 && rm -rf /var/lib/apt/lists/* -WORKDIR /app -EXPOSE 80 - -FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build +FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build WORKDIR /src COPY ["src/ActiveDirectoryToRestApi/ActiveDirectoryToRestApi.csproj", "src/ActiveDirectoryToRestApi/"] -RUN dotnet restore "src/ActiveDirectoryToRestApi/ActiveDirectoryToRestApi.csproj" +RUN dotnet restore "./src/ActiveDirectoryToRestApi/./ActiveDirectoryToRestApi.csproj" COPY . . -WORKDIR "/src/src/ActiveDirectoryToRestApi" -RUN dotnet build "ActiveDirectoryToRestApi.csproj" -c Release -o /app/build -FROM build AS publish -RUN dotnet publish "ActiveDirectoryToRestApi.csproj" -c Release -o /app/publish /p:UseAppHost=false +ARG TARGETPLATFORM -FROM base AS final +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 \ + && dotnet publish "src/ActiveDirectoryToRestApi/ActiveDirectoryToRestApi.csproj" -c Release -o /app/publish -r $RID --self-contained false + +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final +RUN apt update +RUN apt install -y libldap-2.5-0 WORKDIR /app -COPY --from=publish /app/publish . -ENTRYPOINT ["dotnet", "ActiveDirectoryToRestApi.dll"] \ No newline at end of file +COPY --from=build /app/publish . + +ENTRYPOINT ["./ActiveDirectoryToRestApi"] \ No newline at end of file