From 5ae2a99c14a0ebdddfbd50205b2477c7428153ec Mon Sep 17 00:00:00 2001 From: pixil98 <46978190+pixil98@users.noreply.github.com> Date: Sun, 18 Dec 2022 17:57:37 -0600 Subject: [PATCH] Docker workflow (#7) * Refactored workflows * Added docker build to release * Linux and MacOS now build on Linux --- .github/dependabot.yml | 8 ++ .github/workflows/build-linux.yml | 79 ++++++++++++++++++ .github/workflows/build-windows.yml | 82 +++++++++++++++++++ .github/workflows/build.yml | 30 +++++++ .github/workflows/docker.yml | 46 +++++++++++ .github/workflows/dotnet-build.yml | 44 ---------- .github/workflows/dotnet-release.yml | 65 --------------- .github/workflows/dotnet-validate.yml | 19 ----- .github/workflows/release.yml | 64 +++++++++++++++ .github/workflows/validate.yml | 14 ++++ Docker/liberate.sh | 68 +++++++++++++++ Dockerfile | 22 +++++ .../HangoverWinForms/HangoverWinForms.csproj | 1 + .../LibationWinForms/LibationWinForms.csproj | 1 + .../WindowsConfigApp/WindowsConfigApp.csproj | 1 + .../WindowsConfigApp/WindowsConfigApp.csproj | 1 + 16 files changed, 417 insertions(+), 128 deletions(-) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/build-linux.yml create mode 100644 .github/workflows/build-windows.yml create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/docker.yml delete mode 100644 .github/workflows/dotnet-build.yml delete mode 100644 .github/workflows/dotnet-release.yml delete mode 100644 .github/workflows/dotnet-validate.yml create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/validate.yml create mode 100755 Docker/liberate.sh create mode 100644 Dockerfile diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 00000000..8e857033 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,8 @@ +--- +version: 2 +updates: + # Maintain dependencies for GitHub Actions + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" diff --git a/.github/workflows/build-linux.yml b/.github/workflows/build-linux.yml new file mode 100644 index 00000000..3c0d1401 --- /dev/null +++ b/.github/workflows/build-linux.yml @@ -0,0 +1,79 @@ +# build-linux.yml +# Reusable workflow that builds the Linux and MacOS versions of Libation. +--- +name: build + +on: + workflow_call: + inputs: + version_override: + type: string + description: 'Version number override' + required: false + run_unit_tests: + type: boolean + description: 'Skip running unit tests' + required: false + default: true + +env: + DOTNET_CONFIGURATION: 'Release' + DOTNET_VERSION: '7.0.x' + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + os: [Linux, MacOS] + ui: [Avalonia] + release_name: [chardonnay] + steps: + - uses: actions/checkout@v3 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + env: + NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Get version + id: get_version + run: | + inputVersion="${{ inputs.version_override }}" + if [[ "${#inputVersion}" -gt 0 ]] + then + version="${inputVersion}" + else + version="$(grep -oP '(?<=).*(?=> "${GITHUB_OUTPUT}" + + - name: Unit test + if: ${{ inputs.run_unit_tests }} + working-directory: ./Source + run: dotnet test + + - name: Publish + working-directory: ./Source + run: | + dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin/Publish/${{ matrix.os }}-${{ matrix.release_name }} Libation${{ matrix.ui }}/Libation${{ matrix.ui }}.csproj -p:PublishProfile=Libation${{ matrix.ui }}/Properties/PublishProfiles/${{ matrix.os }}Profile.pubxml + dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin/Publish/${{ matrix.os }}-${{ matrix.release_name }} LoadByOS/${{ matrix.os }}ConfigApp/${{ matrix.os }}ConfigApp.csproj -p:PublishProfile=LoadByOS/Properties/${{ matrix.os }}ConfigApp/PublishProfiles/${{ matrix.os }}Profile.pubxml + dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin/Publish/${{ matrix.os }}-${{ matrix.release_name }} LibationCli/LibationCli.csproj -p:PublishProfile=LibationCli/Properties/PublishProfiles/${{ matrix.os }}Profile.pubxml + dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin/Publish/${{ matrix.os }}-${{ matrix.release_name }} Hangover${{ matrix.ui }}/Hangover${{ matrix.ui }}.csproj -p:PublishProfile=Hangover${{ matrix.ui }}/Properties/PublishProfiles/${{ matrix.os }}Profile.pubxml + + - name: Zip artifact + id: zip + working-directory: ./Source/bin/Publish/${{ matrix.os }}-${{ matrix.release_name }} + run: | + osbuild="$(echo '${{ matrix.os }}' | tr '[:upper:]' '[:lower:]')" + artifact="Libation.${{ steps.get_version.outputs.version }}-${osbuild}-${{ matrix.release_name }}" + echo "artifact=${artifact}" >> "${GITHUB_OUTPUT}" + tar -zcvf "../${artifact}.tar.gz" . + + - name: Publish artifact + uses: actions/upload-artifact@v3 + with: + name: ${{ steps.zip.outputs.artifact }}.tar.gz + path: ./Source/bin/Publish/${{ steps.zip.outputs.artifact }}.tar.gz + if-no-files-found: error diff --git a/.github/workflows/build-windows.yml b/.github/workflows/build-windows.yml new file mode 100644 index 00000000..8739bb6f --- /dev/null +++ b/.github/workflows/build-windows.yml @@ -0,0 +1,82 @@ +# build-windows.yml +# Reusable workflow that builds the Windows versions of Libation. +--- +name: build + +on: + workflow_call: + inputs: + version_override: + type: string + description: 'Version number override' + required: false + run_unit_tests: + type: boolean + description: 'Skip running unit tests' + required: false + default: true + +env: + DOTNET_CONFIGURATION: 'Release' + DOTNET_VERSION: '7.0.x' + +jobs: + build: + runs-on: windows-latest + strategy: + matrix: + os: [Windows] + ui: [Avalonia] + release_name: [chardonnay] + include: + - os: Windows + ui: WinForms + release_name: classic + prefix: Classic- + steps: + - uses: actions/checkout@v3 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + env: + NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Get version + id: get_version + run: | + if ("${{ inputs.version_override }}".length -gt 0) { + $version = "${{ inputs.version_override }}" + } else { + [xml]$appScaffolding = Get-Content -Path ./Source/AppScaffolding/AppScaffolding.csproj + $version = $appScaffolding.Project.PropertyGroup.Version + } + "version=$version" >> $env:GITHUB_OUTPUT + + - name: Unit test + if: ${{ inputs.run_unit_tests }} + working-directory: ./Source + run: dotnet test + + - name: Publish + working-directory: ./Source + run: | + dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin/Publish/${{ matrix.os }}-${{ matrix.release_name }} Libation${{ matrix.ui }}/Libation${{ matrix.ui }}.csproj -p:PublishProfile=Libation${{ matrix.ui }}/Properties/PublishProfiles/${{ matrix.os }}Profile.pubxml + dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin/Publish/${{ matrix.os }}-${{ matrix.release_name }} LoadByOS/${{ matrix.os }}ConfigApp/${{ matrix.os }}ConfigApp.csproj -p:PublishProfile=LoadByOS/Properties/${{ matrix.os }}ConfigApp/PublishProfiles/${{ matrix.os }}Profile.pubxml + dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin/Publish/${{ matrix.os }}-${{ matrix.release_name }} LibationCli/LibationCli.csproj -p:PublishProfile=LibationCli/Properties/PublishProfiles/${{ matrix.os }}Profile.pubxml + dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin/Publish/${{ matrix.os }}-${{ matrix.release_name }} Hangover${{ matrix.ui }}/Hangover${{ matrix.ui }}.csproj -p:PublishProfile=Hangover${{ matrix.ui }}/Properties/PublishProfiles/${{ matrix.os }}Profile.pubxml + + - name: Zip artifact + id: zip + working-directory: ./Source/bin/Publish + run: | + $artifact="${{ matrix.prefix }}Libation.${{ steps.get_version.outputs.version }}-" + "${{ matrix.os }}".ToLower() + "-${{ matrix.release_name }}" + "artifact=$artifact" >> $env:GITHUB_OUTPUT + Compress-Archive -Path "${{ matrix.os }}-${{ matrix.release_name }}\*" -DestinationPath "$artifact.zip" + + - name: Publish artifact + uses: actions/upload-artifact@v3 + with: + name: ${{ steps.zip.outputs.artifact }}.zip + path: ./Source/bin/Publish/${{ steps.zip.outputs.artifact }}.zip + if-no-files-found: error diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..7b62f1b1 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,30 @@ +# build.yml +# Reusable workflow that builds Libation for all platforms. +--- +name: build + +on: + workflow_call: + inputs: + version_override: + type: string + description: 'Version number override' + required: false + run_unit_tests: + type: boolean + description: 'Skip running unit tests' + required: false + default: true + +jobs: + windows: + uses: ./.github/workflows/build-windows.yml + with: + version_override: ${{ inputs.version_override }} + run_unit_tests: ${{ inputs.run_unit_tests }} + + linux: + uses: ./.github/workflows/build-linux.yml + with: + version_override: ${{ inputs.version_override }} + run_unit_tests: ${{ inputs.run_unit_tests }} diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 00000000..6ba62545 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,46 @@ +# docker.yml +# Reusable workflow that builds a docker image for Libation. +--- +name: docker + +on: + workflow_call: + inputs: + version: + type: string + description: 'Version number' + required: true + secrets: + docker_username: + required: true + docker_token: + required: true + +env: + DOCKER_IMAGE: ${{ secrets.docker_username }}/libation + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.docker_username }} + password: ${{ secrets.docker_token }} + + - name: Build and push + uses: docker/build-push-action@v3 + with: + push: true + build-args: 'FOLDER_NAME=Linux-chardonnay' + tags: ${{ env.DOCKER_IMAGE }}:latest,${{ env.DOCKER_IMAGE }}:${{ inputs.version }} diff --git a/.github/workflows/dotnet-build.yml b/.github/workflows/dotnet-build.yml deleted file mode 100644 index 6ae3c87a..00000000 --- a/.github/workflows/dotnet-build.yml +++ /dev/null @@ -1,44 +0,0 @@ -# This workflow will build a .NET project -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net - -name: build - -on: - workflow_call: - -env: - DOTNET_CONFIGURATION: 'Release' - -jobs: - build: - runs-on: windows-latest - strategy: - matrix: - os: [Linux, MacOS, Windows] - ui: [Avalonia] - release_name: [chardonnay] - include: - - os: Windows - ui: WinForms - release_name: classic - steps: - - uses: actions/checkout@v3 - - name: Setup .NET - uses: actions/setup-dotnet@v3 - with: - dotnet-version: '7.x' - env: - NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Build - working-directory: ./Source - run: | - dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin\Publish\${{ matrix.os }}-${{ matrix.release_name }} Libation${{ matrix.ui }}\Libation${{ matrix.ui }}.csproj -p:PublishProfile=Libation${{ matrix.ui }}\Properties\PublishProfiles\${{ matrix.os }}Profile.pubxml - dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin\Publish\${{ matrix.os }}-${{ matrix.release_name }} LoadByOS\${{ matrix.os }}ConfigApp\${{ matrix.os }}ConfigApp.csproj -p:PublishProfile=LoadByOS\Properties\${{ matrix.os }}ConfigApp\PublishProfiles\${{ matrix.os }}Profile.pubxml - dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin\Publish\${{ matrix.os }}-${{ matrix.release_name }} LibationCli\LibationCli.csproj -p:PublishProfile=LibationCli\Properties\PublishProfiles\${{ matrix.os }}Profile.pubxml - dotnet publish -c ${{ env.DOTNET_CONFIGURATION }} -o bin\Publish\${{ matrix.os }}-${{ matrix.release_name }} Hangover${{ matrix.ui }}\Hangover${{ matrix.ui }}.csproj -p:PublishProfile=Hangover${{ matrix.ui }}\Properties\PublishProfiles\${{ matrix.os }}Profile.pubxml - - name: Publish artifact - uses: actions/upload-artifact@v3 - with: - name: ${{ matrix.os }}-${{ matrix.release_name }} - path: ./Source/bin/Publish/${{ matrix.os }}-${{ matrix.release_name }}/* - if-no-files-found: error \ No newline at end of file diff --git a/.github/workflows/dotnet-release.yml b/.github/workflows/dotnet-release.yml deleted file mode 100644 index d2da4e4c..00000000 --- a/.github/workflows/dotnet-release.yml +++ /dev/null @@ -1,65 +0,0 @@ -# This workflow will build a .NET project -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net -name: release - -on: - push: - tags: - - 'v*' - -env: - DOTNET_VERSION: '7' # The .NET SDK version to use - DOTNET_SOURCE: './Source' - DOTNET_CONFIGURATION: 'Release' - -jobs: - build: - uses: ./.github/workflows/dotnet-build.yml - - release: - needs: build - runs-on: ubuntu-latest - steps: - - name: Calculate version - id: version - run: | - export TAG=${{ github.ref_name }} - echo "version=${TAG#v}" >> $GITHUB_OUTPUT - - - name: Download artifacts - uses: actions/download-artifact@v3 - with: - path: artifacts - - - name: Zip assets - working-directory: ./artifacts - run: | - for FILENAME in * - do - pushd "${FILENAME}" - zip -r "../Libation.${{ steps.version.outputs.version }}-${FILENAME,,}.zip" . - popd - done - mv Libation.${{ steps.version.outputs.version }}-windows-classic.zip Classic-Libation.${{ steps.version.outputs.version }}-windows-classic.zip - mkdir ./assets - mv *.zip ./assets - - - name: Create release - id: create_release - uses: actions/create-release@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token - with: - tag_name: ${{ github.ref }} - release_name: Libation ${{ steps.version.outputs.version }} - body: - draft: true - prerelease: false - - - name: Upload release assets - uses: dwenegar/upload-release-assets@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - release_id: ${{ steps.create_release.outputs.id }} - assets_path: ./artifacts/assets diff --git a/.github/workflows/dotnet-validate.yml b/.github/workflows/dotnet-validate.yml deleted file mode 100644 index 16313d20..00000000 --- a/.github/workflows/dotnet-validate.yml +++ /dev/null @@ -1,19 +0,0 @@ -# This workflow will build a .NET project -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net - -name: validate - -on: - push: - branches: [ "master" ] - pull_request: - branches: [ "master" ] - -env: - DOTNET_VERSION: '7' # The .NET SDK version to use - DOTNET_SLN: './Source/Libation.sln' - DOTNET_CONFIGURATION: 'Release' - -jobs: - build: - uses: ./.github/workflows/dotnet-build.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..243606ad --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,64 @@ +# release.yml +# Builds and creates the release on any tags starting with a `v` +--- +name: release +on: + push: + tags: + - 'v*' +jobs: + prerelease: + runs-on: ubuntu-latest + outputs: + version: ${{ steps.get_version.outputs.version }} + steps: + - name: Get tag version + id: get_version + run: | + export TAG='${{ github.ref_name }}' + echo "version=${TAG#v}" >> "${GITHUB_OUTPUT}" + + docker: + needs: [prerelease] + uses: ./.github/workflows/docker.yml + with: + version: ${{ needs.prerelease.outputs.version }} + secrets: + docker_username: ${{ secrets.DOCKERHUB_USERNAME }} + docker_token: ${{ secrets.DOCKERHUB_TOKEN }} + + build: + needs: [prerelease] + uses: ./.github/workflows/build.yml + with: + version_override: ${{ needs.prerelease.outputs.version }} + run_unit_tests: false + + release: + needs: [prerelease,build] + runs-on: ubuntu-latest + steps: + - name: Download artifacts + uses: actions/download-artifact@v3 + with: + path: artifacts + + - name: Create release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}' + with: + tag_name: '${{ github.ref }}' + release_name: 'Libation ${{ steps.version.outputs.version }}' + body: + draft: true + prerelease: false + + - name: Upload release assets + uses: dwenegar/upload-release-assets@v1 + env: + GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}' + with: + release_id: '${{ steps.create_release.outputs.id }}' + assets_path: ./artifacts diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml new file mode 100644 index 00000000..0ae4c712 --- /dev/null +++ b/.github/workflows/validate.yml @@ -0,0 +1,14 @@ +# validate.yml +# Validates that Libation will build on a pull request or push to master. +--- +name: validate + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + build: + uses: ./.github/workflows/build.yml diff --git a/Docker/liberate.sh b/Docker/liberate.sh new file mode 100755 index 00000000..83249b87 --- /dev/null +++ b/Docker/liberate.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +# Rewire echo to print date time +echo() { + if [[ -n $1 ]]; then + printf "$(date '+%F %T'): %s\n" "$1" + fi +} + +# ################################ +# Setup +# ################################ +echo "Starting" +if [[ -z "${SLEEP_TIME}" ]]; then + echo "No sleep time passed in. Will run once and exit." +else + echo "Sleep time is set to ${SLEEP_TIME}" +fi + +echo "" + +# Check if the config directory is passed in, and there is no link to it then create the link. +if [ -d "/config" ] && [ ! -d "/root/Libation" ]; then + echo "Linking config directory to the Libation config directory" + ln -s /config/ /root/Libation +fi + +# If no config error and exit +if [ ! -d "/config" ]; then + echo "ERROR: No /config directory. You must pass in a volume containing your config mapped to /config" + exit 1 +fi + +# If user passes in db from a /db/ folder and a db does not already exist / is not already linked +FILE=/db/LibationContext.db +if [ -f "${FILE}" ] && [ ! -f "/config/LibationContext.db" ]; then + echo "Linking passed in Libation database from /db/ to the Libation config directory" + ln -s $FILE /config/LibationContext.db +fi + +# Confirm we have a db in the config direcotry. +if [ ! -f "/config/LibationContext.db" ]; then + echo "ERROR: No Libation database detected, exiting." + exit 1 +fi + +# ################################ +# Loop and liberate +# ################################ +while true +do + echo "" + echo "Scanning accounts" + /libation/LibationCli scan + echo "Liberating books" + /libation/LibationCli liberate + echo "" + + # Liberate only once if SLEEP_TIME was set to -1 + if [ "${SLEEP_TIME}" = -1 ]; then + break + fi + + echo "Sleeping for ${SLEEP_TIME}" + sleep "${SLEEP_TIME}" +done + +echo "Exiting" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..bc7e0c7c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +# Dockerfile +FROM mcr.microsoft.com/dotnet/sdk:7.0 as build-env + +COPY Source /Source +RUN dotnet publish -c Release -o /Source/bin/Publish/Linux-chardonnay /Source/LibationCli/LibationCli.csproj -p:PublishProfile=/Source/LibationCli/Properties/PublishProfiles/LinuxProfile.pubxml +COPY Docker/liberate.sh /Source/bin/Publish/Linux-chardonnay + + +FROM mcr.microsoft.com/dotnet/runtime:7.0 + +ENV SLEEP_TIME "30m" + +# Sets the character set that will be used for folder and filenames when liberating +ENV LANG C.UTF-8 +ENV LC_ALL C.UTF-8 + +RUN mkdir /db /config /data + +COPY --from=build-env /Source/bin/Publish/Linux-chardonnay /libation + + +CMD ["./libation/liberate.sh"] diff --git a/Source/HangoverWinForms/HangoverWinForms.csproj b/Source/HangoverWinForms/HangoverWinForms.csproj index c08bed6b..95ad37a2 100644 --- a/Source/HangoverWinForms/HangoverWinForms.csproj +++ b/Source/HangoverWinForms/HangoverWinForms.csproj @@ -3,6 +3,7 @@ WinExe net7.0-windows + true true hangover.ico enable diff --git a/Source/LibationWinForms/LibationWinForms.csproj b/Source/LibationWinForms/LibationWinForms.csproj index 6e417f8e..a995910d 100644 --- a/Source/LibationWinForms/LibationWinForms.csproj +++ b/Source/LibationWinForms/LibationWinForms.csproj @@ -4,6 +4,7 @@ WinExe net7.0-windows + true true libation.ico Libation diff --git a/Source/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj b/Source/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj index 9dfa0c94..265b4186 100644 --- a/Source/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj +++ b/Source/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj @@ -3,6 +3,7 @@ WinExe net7.0-windows + true true enable true diff --git a/Source/_Demos/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj b/Source/_Demos/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj index 96fabc93..345291ea 100644 --- a/Source/_Demos/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj +++ b/Source/_Demos/LoadByOS/WindowsConfigApp/WindowsConfigApp.csproj @@ -3,6 +3,7 @@ WinExe net7.0-windows + true true enable false