From e23075c542b658085f09dd10e712dd3c72773c4e Mon Sep 17 00:00:00 2001 From: bodand Date: Wed, 28 Dec 2022 22:00:54 +0100 Subject: [PATCH] v0.2.2 feature complete - automating everything... at least trying to... --- .cirrus.star | 84 ++++++++++++++++ .cirrus.yml | 182 ++++++++++++++++++++++++++++------ CMakeLists.txt | 7 +- changelog/ChangeLog-v0.2.2.md | 12 +++ ci/make_release.bat | 32 +++++- ci/make_release.sh | 49 ++++++--- ci/package_env.bat | 4 + ci/sha256.bat | 6 ++ ci/upload-artefact.bat | 8 ++ ci/upload-artefact.sh | 5 +- ci/version_env.bat | 2 +- vcpkg.json | 2 +- 12 files changed, 340 insertions(+), 53 deletions(-) create mode 100644 .cirrus.star create mode 100644 changelog/ChangeLog-v0.2.2.md create mode 100644 ci/package_env.bat create mode 100644 ci/sha256.bat create mode 100644 ci/upload-artefact.bat diff --git a/.cirrus.star b/.cirrus.star new file mode 100644 index 0000000..774a8f6 --- /dev/null +++ b/.cirrus.star @@ -0,0 +1,84 @@ +load('cirrus', 'env', 'http') + + +def on_build_completed(_ctx): + completed_release(_ctx) + + +def on_build_failed(_ctx): + failed_release(_ctx) + + +def failed_release(_ctx): + if env.get("CIRRUS_TAG") != None: + delete_current_release() + + +def completed_release(_ctx): + if env.get("CIRRUS_TAG") != None: + finish_current_release() + + +def repo_path(): + return "/repos/bodand/cg3" + + +def delete_current_release(): + """Deletes the current release.""" + rel_id = get_current_release() + print("deleting release with id {}".format(rel_id)) + delete_release(rel_id) + + +def finish_current_release(): + """Updates the current release from draft to complete release.""" + rel_id = get_current_release() + print("finishing release with id {}".format(rel_id)) + return finish_release(rel_id) + + +def get_current_release(): + releases = github_get(repo_path() + "/releases").json() + for rel in releases: + if rel["draft"]: + print("found draft for tag: '{}', id: '{}'".format(rel["tag_name"], rel["id"])) + return "{}".format(rel["id"]) + return None + +def delete_release(id): + github_delete(repo_path() + "/releases/" + id) + + +def finish_release(id): + github_patch(repo_path() + "/releases/" + id, {"draft": False}) + + +def get_github_token(): + return env.get("GITHUB_TOKEN") + + +def github_delete(url): + hdrs = { + "Accept": "application/vnd.github+json", + "Authorization": "Bearer " + get_github_token(), + "X-GitHub-Api-Version": "2022-11-28" + } + http.delete("https://api.github.com" + url, headers=hdrs) + + +def github_get(url): + hdrs = { + "Accept": "application/vnd.github+json", + "Authorization": "Bearer " + get_github_token(), + "X-GitHub-Api-Version": "2022-11-28" + } + return http.get("https://api.github.com" + url, headers=hdrs) + + +def github_patch(url, body): + hdrs = { + "Accept": "application/vnd.github+json", + "Authorization": "Bearer " + get_github_token(), + "X-GitHub-Api-Version": "2022-11-28" + } + return http.patch("https://api.github.com" + url, headers=hdrs, json_body=body) diff --git a/.cirrus.yml b/.cirrus.yml index 0505e8b..e5035c1 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -1,3 +1,15 @@ +lnx_failed_remove_tag: &FAIL_TAG_LNX + clean_working_dir_script: + - git clean -f -x -q -d + - git tag -d ${CIRRUS_TAG} + - git push --tags --force + +win_failed_remove_tag: &FAIL_TAG_WIN + clean_working_dir_script: + - git clean -f -x -q -d + - git tag -d %CIRRUS_TAG% + - git push --tags --force + lnx_build_template: &BUILD_TEMPLATE_TASK submodules_script: - git submodule update --init --recursive @@ -74,6 +86,7 @@ windows_test_template: &WINDOWS_TEST_TEMPLATE_TASK - choco install jq -y - choco install pandoc -y - choco install ruby -y + - choco install aria2 -y - choco install cmake -y - choco install ninja -y - gem install asciidoctor @@ -141,28 +154,80 @@ linux_deploy_template: &LINUX_DEPLOY_TEMPLATE_TASK - signify-openbsd -S -es "${CG_PKG_NAME}.sec" -m "${CG_PKG_NAME}.sha256" gh_create_release_script: - - ci/make_release.sh + - /usr/bin/env sh ci/make_release.sh gh_release_upload_script: - cd ${BUILD_PATH} - - ../ci/upload-artefact.sh "${CG_PKG_NAME}.pub" - - ../ci/upload-artefact.sh "${CG_PKG_NAME}.tar.xz" "application/x-gtar" - - ../ci/upload-artefact.sh "${CG_PKG_NAME}.deb" "application/vnd.debian.binary-package" - - ../ci/upload-artefact.sh "${CG_PKG_NAME}.rpm" "application/x-rpm" - - ../ci/upload-artefact.sh "${CG_PKG_NAME}.sha256.sig" "text/plain" - - binary_artifacts: - paths: - - ${BUILD_PATH}/${CG_PKG_NAME}.pub - - ${BUILD_PATH}/${CG_PKG_NAME}.tar.xz - - ${BUILD_PATH}/${CG_PKG_NAME}.deb - - ${BUILD_PATH}/${CG_PKG_NAME}.rpm - - ${BUILD_PATH}/${CG_PKG_NAME}.sha256.sig + - /usr/bin/env sh ../ci/upload-artefact.sh "${CG_PKG_NAME}.pub" + - /usr/bin/env sh ../ci/upload-artefact.sh "${CG_PKG_NAME}.tar.xz" "application/x-gtar" + - /usr/bin/env sh ../ci/upload-artefact.sh "${CG_PKG_NAME}.deb" "application/vnd.debian.binary-package"/usr/bin/sh + - /usr/bin/env sh ../ci/upload-artefact.sh "${CG_PKG_NAME}.rpm" "application/x-rpm" + - /usr/bin/env sh ../ci/upload-artefact.sh "${CG_PKG_NAME}.sha256.sig" "text/plain" -linux_deployment_task: - only_if: $CIRRUS_BRANCH == 'trunk' && $CIRRUS_TAG != '' - depends_on: - - linux_test +windows_deploy_template: &WINDOWS_DEPLOY_TEMPLATE_TASK + env: + CG_CMAKE_PRESET: win-msvc-${CG_ARCH}-rel + CG_CMAKE_BUILD_PRESET: release-windows-${CG_ARCH} + PATH: C:\ProgramData\chocolatey\bin;C:\tools\ruby31\bin;C:\Program Files\CMake\bin;%CIRRUS_WORKING_DIR%\wix_toolset;%PATH% + + dependencies_script: + - choco install visualstudio2022buildtools -y --package-parameters "--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 --add Microsoft.VisualStudio.Component.VC.Redist.14.Latest --add Microsoft.Component.VC.Runtime.UCRTSDK --add Microsoft.VisualStudio.Component.Windows10SDK --add Microsoft.VisualStudio.Component.Windows11SDK.22621 --add Microsoft.VisualStudio.Component.VC.ATL" + - choco install vswhere -y + - choco install jq -y + - choco install pandoc -y + - choco install ruby -y + - choco install cmake -y + - choco install aria2 -y + - choco install ninja -y + - choco install curl -y + - gem install asciidoctor + - cmake -E make_directory wix_toolset + - cd wix_toolset + - aria2c https://github.com/wixtoolset/wix3/releases/download/wix3112rtm/wix311-binaries.zip -o wix311.zip + - cmake -E tar xzf wix311.zip + + <<: *WIN_BUILD_TEMPLATE_TASK + + get_signify_script: + - cd %BUILD_PATH% + - aria2c https://github.com/badboy/signify-rs/releases/download/v0.5.3/signify-v0.5.3-x86_64-pc-windows-msvc.zip -o signify.zip + # cmake -E tar is not actually tar; it can handle zip files + - cmake -E tar xzf signify.zip + + tarball_package_script: + - cd %BUILD_PATH% && cpack -G TXZ . + + package_msi_script: + - cd %BUILD_PATH% && cpack -G WIX . + + elevate_package_name_into_env_script: + - ci\package_env.bat + + generate_signing_keys_script: + - cd %BUILD_PATH% + - vars.cmd + - signify -G -nc "cg3 %CG_VER% package distribution" -s "%CG_PKG_NAME%.sec" -p "%CG_PKG_NAME%.pub" + + sign_packages_script: + - cd %BUILD_PATH% + - vars.cmd + - ..\ci\sha256.bat "%CG_PKG_NAME%.tar.xz" > "%CG_PKG_NAME%.sha256" + - ..\ci\sha256.bat "%CG_PKG_NAME%.msi" >> "%CG_PKG_NAME%.sha256" + - signify -S -es "%CG_PKG_NAME%.sec" -m "%CG_PKG_NAME%.sha256" + + gh_create_release_script: + - ci\make_release.bat + + gh_release_upload_script: + - cd %BUILD_PATH% + - vars.cmd + - ..\ci\upload-artefact.bat "%CG_PKG_NAME%.pub" "application/octet-stream" + - ..\ci\upload-artefact.bat "%CG_PKG_NAME%.tar.xz" "application/x-gtar" + - ..\ci\upload-artefact.bat "%CG_PKG_NAME%.msi" "application/msword" + - ..\ci\upload-artefact.bat "%CG_PKG_NAME%.sha256.sig" "text/plain" + +linux_test_task_tmpl: &LNX_TEST_TMPL + stateful: "false" container: image: gcc:latest cpu: 8 @@ -174,12 +239,20 @@ linux_deployment_task: CMAKE_TAR_URL: https://github.com/Kitware/CMake/releases/download/v3.25.1/cmake-3.25.1-linux-x86_64.tar.gz PATH: /tmp/cirrus-ci-build/cmake-3.25.1-linux-x86_64/bin:${PATH} - <<: *LINUX_DEPLOY_TEMPLATE_TASK + <<: *LINUX_TEST_TEMPLATE_TASK -linux_arm_deployment_task: - only_if: $CIRRUS_BRANCH == 'trunk' && $CIRRUS_TAG != '' - depends_on: - - linux_arm_test +linux_test_task: + only_if: $CIRRUS_TAG == '' + + <<: *LNX_TEST_TMPL + +linux_deploy_test_task: + only_if: $CIRRUS_TAG != '' + + <<: *LNX_TEST_TMPL + +linux_arm_test_task_tmpl: &LNX_ARM_TEST_TMPL + stateful: "false" arm_container: image: gcc:latest cpu: 8 @@ -193,9 +266,22 @@ linux_arm_deployment_task: CMAKE_TAR_URL: https://github.com/Kitware/CMake/releases/download/v3.25.1/cmake-3.25.1-linux-aarch64.tar.gz PATH: /tmp/cirrus-ci-build/cmake-3.25.1-linux-aarch64/bin:${PATH} - <<: *LINUX_DEPLOY_TEMPLATE_TASK + <<: *LINUX_TEST_TEMPLATE_TASK -linux_test_task: +linux_arm_test_task: + only_if: $CIRRUS_TAG == '' + + <<: *LNX_ARM_TEST_TMPL + +linux_arm_deploy_test_task: + only_if: $CIRRUS_TAG != '' + + <<: *LNX_ARM_TEST_TMPL + +linux_deployment_task: + only_if: $CIRRUS_TAG != '' + depends_on: + - linux_deploy_test container: image: gcc:latest cpu: 8 @@ -207,9 +293,15 @@ linux_test_task: CMAKE_TAR_URL: https://github.com/Kitware/CMake/releases/download/v3.25.1/cmake-3.25.1-linux-x86_64.tar.gz PATH: /tmp/cirrus-ci-build/cmake-3.25.1-linux-x86_64/bin:${PATH} - <<: *LINUX_TEST_TEMPLATE_TASK + <<: *LINUX_DEPLOY_TEMPLATE_TASK -linux_arm_test_task: + on_failure: + <<: *FAIL_TAG_LNX + +linux_arm_deployment_task: + only_if: $CIRRUS_TAG != '' + depends_on: + - linux_arm_deploy_test arm_container: image: gcc:latest cpu: 8 @@ -223,9 +315,13 @@ linux_arm_test_task: CMAKE_TAR_URL: https://github.com/Kitware/CMake/releases/download/v3.25.1/cmake-3.25.1-linux-aarch64.tar.gz PATH: /tmp/cirrus-ci-build/cmake-3.25.1-linux-aarch64/bin:${PATH} - <<: *LINUX_TEST_TEMPLATE_TASK + <<: *LINUX_DEPLOY_TEMPLATE_TASK -windows_test_task: + on_failure: + <<: *FAIL_TAG_LNX + +windows_test_task_tmpl: &WIN_TEST_TMPL + stateful: "false" windows_container: image: cirrusci/windowsservercore:2019 cpu: 8 @@ -236,3 +332,31 @@ windows_test_task: BUILD_PATH: _build-release-x64-msvc <<: *WINDOWS_TEST_TEMPLATE_TASK + +windows_test_task: + only_if: $CIRRUS_TAG == '' + + <<: *WIN_TEST_TMPL + +windows_deploy_test_task: + only_if: $CIRRUS_TAG != '' + + <<: *WIN_TEST_TMPL + +windows_deployment_task: + only_if: $CIRRUS_TAG != '' + depends_on: + - windows_deploy_test + windows_container: + image: cirrusci/windowsservercore:2019 + cpu: 8 + memory: 32G + + env: + CG_ARCH: x64 + BUILD_PATH: _build-release-x64-msvc + + <<: *WINDOWS_DEPLOY_TEMPLATE_TASK + + on_failure: + <<: *FAIL_TAG_WIN diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d77f01..904d8fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,7 +55,7 @@ cmake_policy(SET CMP0048 NEW) # project ######################################################################### project(cg3 - VERSION 0.2.1 + VERSION 0.2.2 HOMEPAGE_URL "https://github.com/bodand/cg3" DESCRIPTION "Prog1 homework static checker" LANGUAGES CXX) @@ -269,6 +269,11 @@ install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.adoc" DESTINATION "${CMAKE_INSTALL_DOCDIR}") # cpack +if (WIN32) + add_executable(winarch src/util/winarch/main.cxx) + target_compile_options(winarch PRIVATE /EHsc) + target_compile_features(winarch PRIVATE cxx_std_17) +endif () get_arch(PACKAGE_ARCH) if (NOT PACKAGE_ARCH) message(SEND_ERROR "Could not guess meaningfully about package ISA") diff --git a/changelog/ChangeLog-v0.2.2.md b/changelog/ChangeLog-v0.2.2.md new file mode 100644 index 0000000..a70ed07 --- /dev/null +++ b/changelog/ChangeLog-v0.2.2.md @@ -0,0 +1,12 @@ +# Cg3 v0.2.2 changelog + +## Internal changes + +- WIP releases should now be created only as Drafts +- Automate upgrading completed draft releases to complete releases using Starlark +- Automate deleting incomplete releases using Starlark +- Extract all logic from .cirrus.yml into external shell scripts +- Write Windows specific batch/powershell scripts for CI/CD +- Windows CI +- Windows CD +- Releases now contain Windows MSI installers diff --git a/ci/make_release.bat b/ci/make_release.bat index b0df87c..44e36c8 100644 --- a/ci/make_release.bat +++ b/ci/make_release.bat @@ -1,8 +1,32 @@ +@echo off + +cd %BUILD_PATH% + +curl ^ + -H "Accept: application/vnd.github+json" ^ + -H "Authorization: Bearer %GITHUB_TOKEN%" ^ + -H "X-GitHub-Api-Version: 2022-11-28" ^ + "https://api.github.com/repos/bodand/cg3/releases" -o releases.json + +for /f "usebackq tokens=*" %%r in (`jq ".[]|select(.draft).id" releases.json`) do set "draft_id=%%r" + +if NOT DEFINED draft_id goto CREAT_DRAFT + +echo:Draft release found; using existing release with id %draft_id% +:WRITE_ENV + +for /f "usebackq tokens=*" %%r in (`jq ".[]|select(.draft).id" releases.json`) do echo:set "GH_REL_ID=%%r" >> vars.cmd + +goto END + +:CREAT_DRAFT +echo:Draft release not found; creating... + jq --null-input ^ --arg tag_name "%CIRRUS_TAG%" ^ --arg name "cg3 release %CIRRUS_TAG%" ^ --arg body "$(cat %CIRRUS_WORKING_DIR%/changelog/ChangeLog-%CIRRUS_TAG%.md)" ^ - "{"tag_name": $tag_name, "name": $name, "body": $body, "generate_release_notes": false}" ^ + "{"tag_name": $tag_name, "name": $name, "body": $body, "generate_release_notes": false, "draft": true}" ^ >release-payload.json curl -X POST ^ @@ -16,6 +40,8 @@ curl ^ -H "Accept: application/vnd.github+json" ^ -H "Authorization: Bearer %GITHUB_TOKEN%" ^ -H "X-GitHub-Api-Version: 2022-11-28" ^ - "https://api.github.com/repos/bodand/cg3/releases/tags/${CIRRUS_TAG}" | tee response.json + "https://api.github.com/repos/bodand/cg3/releases" -o releases.json + +goto WRITE_ENV -for /f "usebackq tokens=*" %%r in (`jq .id response.json`) do echo "GH_REL_ID=%%r" >%CIRRUS_ENV% +:END diff --git a/ci/make_release.sh b/ci/make_release.sh index 0518e5d..72aa7e2 100644 --- a/ci/make_release.sh +++ b/ci/make_release.sh @@ -1,22 +1,39 @@ #!/usr/bin/env sh -jq --null-input \ - --arg tag_name "${CIRRUS_TAG}" \ - --arg name "cg3 release ${CIRRUS_TAG}" \ - --arg body "$(cat ${CIRRUS_WORKING_DIR}/changelog/ChangeLog-${CIRRUS_TAG}.md)" \ - '{"tag_name": $tag_name, "name": $name, "body": $body, "generate_release_notes": false}' \ - >release-payload.json - -curl -X POST \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer ${GITHUB_TOKEN}" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "https://api.github.com/repos/bodand/cg3/releases" \ - -d "@release-payload.json" - curl \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer ${GITHUB_TOKEN}" \ -H "X-GitHub-Api-Version: 2022-11-28" \ - "https://api.github.com/repos/bodand/cg3/releases/tags/${CIRRUS_TAG}" | tee response.json -echo "GH_REL_ID=$(jq .id response.json)" >${CIRRUS_ENV} + "https://api.github.com/repos/bodand/cg3/releases" -o releases.json +draft_id=$(jq '.[] | select(.draft).id' releases.json) + +if [ -z "$draft_id" ]; then + rm -f releases.json + echo "Draft release not found; creating..." + jq --null-input \ + --arg tag_name "${CIRRUS_TAG}" \ + --arg name "cg3 release ${CIRRUS_TAG}" \ + --arg body "$(cat ${CIRRUS_WORKING_DIR}/changelog/ChangeLog-${CIRRUS_TAG}.md)" \ + '{"tag_name": $tag_name, "name": $name, "body": $body, "generate_release_notes": false, "draft": true}' \ + >release-payload.json + + curl -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${GITHUB_TOKEN}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/bodand/cg3/releases" \ + -d "@release-payload.json" + + sleep 5 # idk what is happening anymore... + curl \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${GITHUB_TOKEN}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/bodand/cg3/releases" -o releases.json +else + echo "Draft release found; using existing release" +fi + +draft_id=$(jq '.[] | select(.draft).id' releases.json) +echo "Draft id used: $draft_id" +echo "GH_REL_ID=$draft_id" >${CIRRUS_ENV} diff --git a/ci/package_env.bat b/ci/package_env.bat new file mode 100644 index 0000000..e0acd5c --- /dev/null +++ b/ci/package_env.bat @@ -0,0 +1,4 @@ +@REM Windows is fix, we don't need to emulate the `uname` part +cd %BUILD_PATH% +call vars.cmd +for /f "usebackq" %%a in (`winarch`) do echo:set "CG_PKG_NAME=cg3-checker-%CG_VER%-Windows-%%a" >> vars.cmd diff --git a/ci/sha256.bat b/ci/sha256.bat new file mode 100644 index 0000000..45a22c2 --- /dev/null +++ b/ci/sha256.bat @@ -0,0 +1,6 @@ +@echo off +REM sha256.bat produces a file hash compatible with BSD +set "file=%~nx1" + +for /f "usebackq tokens=1" %%h in (`cmake -E sha256sum %file%`) do set "hash=%%h" +echo:SHA256 (%file%) = %hash% diff --git a/ci/upload-artefact.bat b/ci/upload-artefact.bat new file mode 100644 index 0000000..4df55bd --- /dev/null +++ b/ci/upload-artefact.bat @@ -0,0 +1,8 @@ +echo:Uploading to release id %GH_REL_ID%... +curl -X POST ^ + -H "Accept: application/vnd.github+json" ^ + -H "Authorization: Bearer %GITHUB_TOKEN%" ^ + -H "X-GitHub-Api-Version: 2022-11-28" ^ + -H "Content-Type: %~2" ^ + "https://uploads.github.com/repos/bodand/cg3/releases/%GH_REL_ID%/assets?name=%~1" ^ + --data-binary "@%~1" diff --git a/ci/upload-artefact.sh b/ci/upload-artefact.sh index d47c5de..57cdf05 100644 --- a/ci/upload-artefact.sh +++ b/ci/upload-artefact.sh @@ -1,11 +1,12 @@ #!/usr/bin/env sh -filename=$0 -type=$1 +filename=$1 +type=$2 if [ -z "$type" ]; then type="application/octet-stream" fi +echo "Uploading to release id ${GH_REL_ID}..." curl -X POST \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer ${GITHUB_TOKEN}" \ diff --git a/ci/version_env.bat b/ci/version_env.bat index 643163b..63809d9 100644 --- a/ci/version_env.bat +++ b/ci/version_env.bat @@ -1,2 +1,2 @@ cd %BUILD_PATH% -for /f "usebackq" %%v in (`type versioninfo.txt`) do echo "CG_VER=%%v" > %CIRRUS_ENV% +for /f "usebackq" %%v in (`type versioninfo.txt`) do echo:set "CG_VER=%%v" >> vars.cmd diff --git a/vcpkg.json b/vcpkg.json index 28b8df5..fd0406e 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,6 +1,6 @@ { "name": "cg3", - "version-semver": "0.2.1", + "version-semver": "0.2.2", "license": "BSD-3-Clause", "builtin-baseline": "d7c68eea3888e285e4e51b028c13efa1c5229029", "dependencies": [