From 015541da211725e946439a4faf180c4d62473dd7 Mon Sep 17 00:00:00 2001 From: Philipp Grathwohl Date: Wed, 10 Jul 2024 20:16:24 +0000 Subject: [PATCH] Build docker container for tags --- .dockerignore | 7 ++++ .github/workflows/build_and_validate.yml | 49 ++++++++++++++---------- Dockerfile | 16 +++++++- 3 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0b29cdd --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +**/bin/ +**/obj/ +**/out/ +**/.git +Dockerfile +.github +.vscode \ No newline at end of file diff --git a/.github/workflows/build_and_validate.yml b/.github/workflows/build_and_validate.yml index 559b10c..ded356d 100644 --- a/.github/workflows/build_and_validate.yml +++ b/.github/workflows/build_and_validate.yml @@ -6,6 +6,8 @@ name: .NET on: push: branches: [ "main" ] + tags: + - '*' pull_request: branches: [ "main" ] @@ -28,10 +30,31 @@ jobs: PUBLISH_IMAGE=false TAGS="$IMAGE_NAME_LOWERCASE:dev" + # Define the image that will be used as a cached image + # to speed up the build process + BUILD_CACHE_IMAGE_NAME=${TAGS} + + if [[ ${GITHUB_REF_NAME} == ref/tags/* ]]; then + LATEST_IMAGE=$IMAGE_NAME_LOWERCASE:latest + VERSION=${GITHUB_REF_NAME#ref/tags/} + TAGS="${LATEST_IMAGE},$IMAGE_NAME_LOWERCASE:v${VERSION}" + PUBLISH_IMAGE=true + fi + echo "tags: $TAGS" echo "publish_image: $PUBLISH_IMAGE" + echo "cache_name: $BUILD_CACHE_IMAGE_NAME" + echo "tags=$TAGS" >> $GITHUB_OUTPUT echo "publish_image=$PUBLISH_IMAGE" >> $GITHUB_OUTPUT + echo "cache_name=$BUILD_CACHE_IMAGE_NAME" >> $GITHUB_OUTPUT + + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} - uses: docker/build-push-action@v4 with: @@ -39,29 +62,15 @@ jobs: file: "Dockerfile" tags: ${{ steps.prep.outputs.tags }} load: true - cache-from: type=gha - cache-to: type=gha,mode=max - push: false + cache-from: type=registry,ref=${{ steps.prep.outputs.cache_name }} + cache-to: type=inline + push: ${{ steps.prep.outputs.publish_image == 'true' }} labels: | org.opencontainers.image.title=${{ github.event.repository.name }} org.opencontainers.image.description=${{ github.event.repository.description }} org.opencontainers.image.url=${{ github.event.repository.html_url }} org.opencontainers.image.revision=${{ github.sha }} - - name: Validate json schemas - uses: addnab/docker-run-action@v3 - with: - image: ${{ steps.prep.outputs.tags }} - options: -v ${{ github.workspace }}:/workspaces/levelmeter - run: cd /workspaces/levelmeter && find /workspaces/levelmeter/scales/definitions/*.json | xargs -I % check-jsonschema % --schemafile /workspaces/levelmeter/scales/definitions/schemas/scale-config - - name: Build and test - uses: addnab/docker-run-action@v3 - with: - image: ${{ steps.prep.outputs.tags }} - options: -v ${{ github.workspace }}:/workspaces/levelmeter - run: cd /workspaces/levelmeter && dotnet build --configuration=Release && dotnet test --configuration=Release + - name: Test recreating all cylinder definitions - uses: addnab/docker-run-action@v3 - with: - image: ${{ steps.prep.outputs.tags }} - options: -v ${{ github.workspace }}:/workspaces/levelmeter - run: cd /workspaces/levelmeter && find /workspaces/levelmeter/scales/definitions/cylinder*.json | xargs -I % dotnet run --project /workspaces/levelmeter/src/dotnet-levelmeter/dotnet-levelmeter.csproj -- -c % + run: | + find ./scales/definitions/cylinder*.json | xargs -I % docker run -v $(pwd):/config $IMAGE_NAME -c % \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 9adbc48..c330eab 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,4 +20,18 @@ ENV PATH=$GEM_HOME/bin:$PATH USER vscode RUN gem install jekyll bundler -USER root \ No newline at end of file +USER root + + + +FROM dev as build +WORKDIR /build +COPY . . +RUN find ./scales/definitions/*.json | xargs -I % check-jsonschema % --schemafile ./scales/definitions/schemas/scale-config +RUN dotnet build --configuration=Release && dotnet test --no-build --configuration=Release + +FROM mcr.microsoft.com/dotnet/runtime:8.0 +WORKDIR /app +COPY --from=build /build/src/dotnet-levelmeter/bin/Release/net8.0 . +WORKDIR /config +ENTRYPOINT [ "dotnet", "/app/dotnet-levelmeter.dll", "--"] \ No newline at end of file