From 674ce61ec182d4ab416cdee3f492bb6e0b0d2877 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:24:12 +0900 Subject: [PATCH] ci: consolidate CI workflows and package with bazel --- .bazelrc | 4 +- .github/workflows/create_archive_and_notes.sh | 44 -------------- .github/workflows/examples.yml | 22 ------- .github/workflows/release.yml | 27 --------- .github/workflows/workflow.yml | 57 ++++++++++++++++++- .gitignore | 1 + BUILD.bazel | 20 +++++++ CHANGELOG.md | 4 +- MODULE.bazel | 2 + internal/BUILD.bazel | 19 ++----- internal/pkg/BUILD.bazel | 56 ++++++++++++++++++ internal/pkg/release.sh | 19 +++++++ internal/pkg/release_notes.tmpl.md | 27 +++++++++ tests/BUILD.bazel | 16 ++++++ {internal => tests}/testdata/bad.sh | 0 {internal => tests}/testdata/good.sh | 0 16 files changed, 205 insertions(+), 113 deletions(-) delete mode 100755 .github/workflows/create_archive_and_notes.sh delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/release.yml create mode 100644 internal/pkg/BUILD.bazel create mode 100755 internal/pkg/release.sh create mode 100644 internal/pkg/release_notes.tmpl.md create mode 100644 tests/BUILD.bazel rename {internal => tests}/testdata/bad.sh (100%) rename {internal => tests}/testdata/good.sh (100%) diff --git a/.bazelrc b/.bazelrc index 32f4b07..9bdffa1 100644 --- a/.bazelrc +++ b/.bazelrc @@ -2,7 +2,7 @@ test --test_output=errors # Fix the excessive rebuilding when using anything that depends on protobuf rules # See https://github.com/bazelbuild/buildtools/issues/744 -build --incompatible_strict_action_env -run --incompatible_strict_action_env +common --incompatible_strict_action_env +common --enable_bzlmod try-import user.bazelrc diff --git a/.github/workflows/create_archive_and_notes.sh b/.github/workflows/create_archive_and_notes.sh deleted file mode 100755 index 4988660..0000000 --- a/.github/workflows/create_archive_and_notes.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash -set -o errexit -o nounset -o pipefail - -# Set by GH actions, see -# https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables -TAG=${GITHUB_REF_NAME} -if [[ "$TAG" == "master" ]]; then - TAG="${GITHUB_SHA}" -fi - -# A prefix is added to better match the GitHub generated archives. -ARCHIVE="rules_shellcheck-$TAG.tar.gz" -git archive --format=tar "${TAG}" | gzip > "$ARCHIVE" -SHA=$(shasum -a 256 "$ARCHIVE" | awk '{print $1}') - -cat > release_notes.txt << EOF -## Using Bzlmod with Bazel 6 - -**NOTE: bzlmod support is still beta. APIs subject to change.** - -Add to your \`MODULE.bazel\` file: - -\`\`\`starlark -bazel_dep(name = "rules_shellcheck", version = "${TAG}") -\`\`\` - -## Legacy: using WORKSPACE - -Paste this snippet into your \`WORKSPACE\` file: - -\`\`\`starlark -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "rules_shellcheck", - sha256 = "${SHA}", - url = "https://github.com/aignas/rules_shellcheck/releases/download/${TAG}/rules_shellcheck-${TAG}.tar.gz", -) - -load("@rules_shellcheck//:deps.bzl", "shellcheck_dependencies") - -shellcheck_dependencies() -\`\`\` -EOF diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 9ef1209..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: Examples - -on: - pull_request: {} - -jobs: - build: - runs-on: ubuntu-latest - - strategy: - matrix: - example: - - bzlmod - - check_glob - - released_example - - steps: - - uses: actions/checkout@v4 - - - name: Test - run: bazel test //... - working-directory: examples/${{ matrix.example }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index c6301e0..0000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,27 +0,0 @@ -# Cut a release whenever a new tag is pushed to the repo. ---- -name: Release - -on: # yamllint disable rule:truthy - push: - tags: - - "*.*.*" - -jobs: - build: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Create release archive and notes - run: .github/workflows/create_archive_and_notes.sh - - - name: Release - uses: softprops/action-gh-release@v1 - with: - # Use GH feature to populate the changelog automatically - generate_release_notes: true - body_path: release_notes.txt - fail_on_unmatched_files: true - files: rules_shellcheck-*.tar.gz diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index e416448..9432946 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -1,7 +1,15 @@ +--- name: CI -on: - push: {} +on: # yamllint disable rule:truthy + push: + branches: + - master + tags: + - "*.*.*" + pull_request: + branches: + - master jobs: build: @@ -24,3 +32,48 @@ jobs: - name: Test run: bazel test //... + + examples: + runs-on: ubuntu-latest + + strategy: + matrix: + example: + - bzlmod + - check_glob + - released_example + + steps: + - uses: actions/checkout@v4 + + - name: Test + run: bazel test //... + working-directory: examples/${{ matrix.example }} + + release: + runs-on: ubuntu-latest + needs: + - build + - test-mac + - examples + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Create release archive and notes + run: | + # Set by GH actions, see + # https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables + bazel run \ + --stamp --embed_label "${{ github.ref_name }}" \ + //:release -- release + + - name: Release + if: ${{ github.event != 'pull_request' }} + uses: softprops/action-gh-release@v1 + with: + # Use GH feature to populate the changelog automatically + generate_release_notes: true + body_path: release/release_notes.md + fail_on_unmatched_files: true + files: release/rules_shellcheck-*.tar.gz diff --git a/.gitignore b/.gitignore index 23a1334..86b1f0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ bazel-* user.bazelrc +/release/ diff --git a/BUILD.bazel b/BUILD.bazel index 90e6ef8..5cbf540 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,3 +1,23 @@ +filegroup( + name = "distribution", + srcs = [ + "BUILD.bazel", + "CHANGELOG.md", + "LICENSE", + "MODULE.bazel", + "README.md", + "WORKSPACE", + "//internal:distribution", + ], + visibility = ["//internal/pkg:__pkg__"], +) + +alias( + name = "release", + actual = "//internal/pkg:release", + visibility = ["//:__subpackages__"], +) + config_setting( name = "darwin_x86_64", constraint_values = [ diff --git a/CHANGELOG.md b/CHANGELOG.md index 3db840c..b87b0aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,9 @@ This document is maintaining changes since the last released version (0.1.1) ## Unreleased -Nothing yet. +### Changed + +* Packaging is now partially done with `bazel`. ## v0.2.4 diff --git a/MODULE.bazel b/MODULE.bazel index ed206f5..31c50ce 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -13,3 +13,5 @@ use_repo( "shellcheck_linux_amd64", "shellcheck_linux_arm64", ) + +bazel_dep(name = "rules_pkg", version = "0.9.1", dev_dependency = True) diff --git a/internal/BUILD.bazel b/internal/BUILD.bazel index 49a89cc..a011dca 100644 --- a/internal/BUILD.bazel +++ b/internal/BUILD.bazel @@ -1,16 +1,5 @@ -load(":rules.bzl", "shellcheck_test") - -shellcheck_test( - name = "fail_test", - data = [ - "testdata/bad.sh", - ], - expect_fail = True, -) - -shellcheck_test( - name = "success_test", - data = [ - "testdata/good.sh", - ], +filegroup( + name = "distribution", + srcs = glob(["*"]), + visibility = ["//:__pkg__"], ) diff --git a/internal/pkg/BUILD.bazel b/internal/pkg/BUILD.bazel new file mode 100644 index 0000000..95a93af --- /dev/null +++ b/internal/pkg/BUILD.bazel @@ -0,0 +1,56 @@ +load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix") +load("@rules_pkg//pkg:tar.bzl", "pkg_tar") +load("//:def.bzl", "shellcheck_test") + +pkg_files( + name = "files", + srcs = [ + "//:distribution", + ], + strip_prefix = strip_prefix.from_root(), +) + +pkg_tar( + name = "tar", + srcs = [ + ":files", + ], + out = "rules_shellcheck.tar", +) + +genrule( + name = "archive", + srcs = [":tar"], + outs = ["rules_shellcheck.tar.gz"], + cmd = "gzip -c $< > $@", +) + +genrule( + name = "release_stamped", + srcs = ["release.sh"], + outs = ["release_stamped.sh"], + cmd = "; ".join([ + "BUILD_EMBED_LABEL=$$(grep ^BUILD_EMBED_LABEL bazel-out/stable-status.txt | cut -d' ' -f2)", + "sed \"s/\\$${BUILD_EMBED_LABEL}/$$BUILD_EMBED_LABEL/g\" $< >$@", + ]), + stamp = 1, +) + +sh_binary( + name = "release", + srcs = ["release_stamped"], + data = [ + ":archive", + ":release_notes.tmpl.md", + ], + env = { + "ARCHIVE": "$(location :archive)", + "RELEASE_NOTES_TEMPLATE": "$(location :release_notes.tmpl.md)", + }, + visibility = ["//:__pkg__"], +) + +shellcheck_test( + name = "release.shellcheck", + data = [":release"], +) diff --git a/internal/pkg/release.sh b/internal/pkg/release.sh new file mode 100755 index 0000000..72a6e52 --- /dev/null +++ b/internal/pkg/release.sh @@ -0,0 +1,19 @@ +#!/bin/bash +set -euo pipefail + +DST="${BUILD_WORKSPACE_DIRECTORY}/$1" +TAG="${2:-${BUILD_EMBED_LABEL}}" + +mkdir -p "$DST" + +RELEASE_ARCHIVE="$DST/rules_shellcheck-$TAG.tar.gz" +RELEASE_NOTES="$DST/release_notes.md" + +cp -v "$ARCHIVE" "$RELEASE_ARCHIVE" +SHA=$(sha256sum "$RELEASE_ARCHIVE" | awk '{print $1}') + +sed \ + -e "s/%%TAG%%/$1/g" \ + -e "s/%%SHA256%%/$SHA/g" \ + "${RELEASE_NOTES_TEMPLATE}" \ + > "$RELEASE_NOTES" diff --git a/internal/pkg/release_notes.tmpl.md b/internal/pkg/release_notes.tmpl.md new file mode 100644 index 0000000..71863fa --- /dev/null +++ b/internal/pkg/release_notes.tmpl.md @@ -0,0 +1,27 @@ +## Using Bzlmod with Bazel 6 + +**NOTE: bzlmod support is still beta. APIs subject to change.** + +Add to your `MODULE.bazel` file: + +```starlark +bazel_dep(name = "rules_shellcheck", version = "%%TAG%%") +``` + +## Legacy: using WORKSPACE + +Paste this snippet into your `WORKSPACE` file: + +```starlark +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +http_archive( + name = "rules_shellcheck", + sha256 = "%%SHA256%%", + url = "https://github.com/aignas/rules_shellcheck/releases/download/%%TAG%%/rules_shellcheck-%%TAG%%.tar.gz", +) + +load("@rules_shellcheck//:deps.bzl", "shellcheck_dependencies") + +shellcheck_dependencies() +``` diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel new file mode 100644 index 0000000..b12392a --- /dev/null +++ b/tests/BUILD.bazel @@ -0,0 +1,16 @@ +load("//internal:rules.bzl", "shellcheck_test") + +shellcheck_test( + name = "fail_test", + data = [ + "testdata/bad.sh", + ], + expect_fail = True, +) + +shellcheck_test( + name = "success_test", + data = [ + "testdata/good.sh", + ], +) diff --git a/internal/testdata/bad.sh b/tests/testdata/bad.sh similarity index 100% rename from internal/testdata/bad.sh rename to tests/testdata/bad.sh diff --git a/internal/testdata/good.sh b/tests/testdata/good.sh similarity index 100% rename from internal/testdata/good.sh rename to tests/testdata/good.sh