From 3bc427f416da83ffec9face07adf2cbc7c2aaecb Mon Sep 17 00:00:00 2001 From: Thomas Buckley-Houston Date: Mon, 18 Jul 2022 12:58:27 -0400 Subject: devops: Update release process for Github --- scripts/bundling.bash | 87 +++++++++++++++++++++++++++++++++++++------------- scripts/common.bash | 18 ++++++++++- scripts/docker.bash | 1 + scripts/misc.bash | 25 ++++++--------- scripts/releasing.bash | 68 ++++++++++++++++++++++++++++----------- 5 files changed, 141 insertions(+), 58 deletions(-) (limited to 'scripts') diff --git a/scripts/bundling.bash b/scripts/bundling.bash index eb3c538..a46bfcf 100644 --- a/scripts/bundling.bash +++ b/scripts/bundling.bash @@ -1,7 +1,23 @@ #!/usr/bin/env bash -function build_webextension() { - local NODE_BIN=$PROJECT_ROOT/webext/node_modules/.bin +export XPI_PATH="$PROJECT_ROOT"/interfacer/src/browsh/browsh.xpi +export XPI_SOURCE_DIR=$PROJECT_ROOT/webext/dist/web-ext-artifacts +export NODE_BIN=$PROJECT_ROOT/webext/node_modules/.bin +MDN_USER="user:13243312:78" + +function versioned_xpi_file() { + echo "$XPI_SOURCE_DIR/browsh-$(browsh_version).xpi" +} + +# You'll want to use this with `go run ./cmd/browsh --debug --firefox.use-existing` +function build_webextension_watch() { + "$NODE_BIN"/web-ext run \ + --firefox contrib/firefoxheadless.sh \ + --verbose +} + +function build_webextension_production() { + local version && version=$(browsh_version) cd "$PROJECT_ROOT"/webext && "$NODE_BIN"/webpack cd "$PROJECT_ROOT"/webext/dist && rm ./*.map @@ -13,36 +29,63 @@ function build_webextension() { "$NODE_BIN"/web-ext build --overwrite-dest ls -alh web-ext-artifacts - version=$(browsh_version) + webextension_sign + local source_file && source_file=$(versioned_xpi_file) + + echo "Bundling $source_file to $XPI_PATH" + cp -f "$source_file" "$XPI_PATH" - local source_file - local source_dir=$PROJECT_ROOT/webext/dist/web-ext-artifacts - local bundle_file=$PROJECT_ROOT/interfacer/src/browsh/browsh.xpi + echo "Making extra copy for Goreleaser to put in Github release:" + local goreleaser_pwd="$PROJECT_ROOT"/interfacer/ + cp -a "$source_file" "$goreleaser_pwd" + ls -alh "$goreleaser_pwd" +} - if [ "$BROWSH_ENV" == "RELEASE" ]; then - # The signed version. There can only be one canonical XPI for each semantic - # version. - source_file="$source_dir/browsh-$version-an+fx.xpi" +# It is possible to use unsigned webextensions in Firefox but it requires that Firefox +# uses problematically insecure config. I know it's a hassle having to jump through all +# these signing hoops, but I think it's better to use a standard Firefox configuration. +# Moving away from the webextension alltogether is another story, but something I'm still +# thinking about. +# +# NB: There can only be one canonical XPI for each semantic version. +# +# shellcheck disable=2120 +function webextension_sign() { + local use_existing=$1 + if [ "$use_existing" == "" ]; then "$NODE_BIN"/web-ext sign --api-key "$MDN_USER" --api-secret "$MDN_KEY" + _rename_built_xpi else - # TODO: This doesn't currently work with the Marionettte `tempAddon` - # installation method. Just use `web-ext run` and Browsh's `use-existing-ff` - # flag - which is better anyway as it auto-reloads the extension when files - # change. NB: If you fix this, don't forget to change the filename loaded - # by `Asset()` in `main.go`. - # In development/testing, we want to be able to bundle the webextension - # frequently without having to resort to version bumps. - source_file="$source_dir/browsh-$version.zip" + echo "Skipping signing, downloading existing webextension" + local base="https://github.com/browsh-org/browsh/releases/download" + curl -L \ + -o "$(versioned_xpi_file)" \ + "$base/v$LATEST_TAGGED_VERSION/browsh-$LATEST_TAGGED_VERSION.xpi" fi +} - echo "Bundling $source_file to $bundle_file" - cp -f "$source_file" "$bundle_file" +function _rename_built_xpi() { + pushd "$XPI_SOURCE_DIR" || _panic + local xpi_file + xpi_file="$( + find ./*.xpi \ + -printf "%T@ %f\n" | + sort | + cut -d' ' -f2 | + tail -n1 + )" + cp -a "$xpi_file" "$(versioned_xpi_file)" + popd || _panic } function bundle_production_webextension() { local version && version=$(browsh_version) local base='https://github.com/browsh-org/browsh/releases/download' local release_url="$base/v$version/browsh-$version-an.fx.xpi" - local xpi_file=$PROJECT_ROOT/interfacer/src/browsh/browsh.xpi - curl -L -o "$xpi_file" "$release_url" + echo "Downloading webextension from: $release_url" + local size && size=$(wc -c <"$XPI_PATH") + curl -L -o "$XPI_PATH" "$release_url" + if [ "$size" -lt 500 ]; then + _panic "Problem downloading latest webextension XPI" + fi } diff --git a/scripts/common.bash b/scripts/common.bash index 20bcb04..cfa04c9 100644 --- a/scripts/common.bash +++ b/scripts/common.bash @@ -1,7 +1,23 @@ -#!/bin/env bash +#!/usr/bin/env bash +# shellcheck disable=2120 function _panic() { local message=$1 echo >&2 "$message" exit 1 } + +function _md5() { + local path=$1 + md5sum "$path" | cut -d' ' -f1 +} + +function pushd() { + # shellcheck disable=2119 + command pushd "$@" >/dev/null || _panic +} + +function popd() { + # shellcheck disable=2119 + command popd "$@" >/dev/null || _panic +} diff --git a/scripts/docker.bash b/scripts/docker.bash index 8d758a1..e87086f 100644 --- a/scripts/docker.bash +++ b/scripts/docker.bash @@ -1,6 +1,7 @@ #!/usr/bin/env bash function docker_image_name() { + _export_versions echo browsh/browsh:v"$BROWSH_VERSION" } diff --git a/scripts/misc.bash b/scripts/misc.bash index 7932329..e3bd51a 100644 --- a/scripts/misc.bash +++ b/scripts/misc.bash @@ -36,15 +36,19 @@ function install_firefox() { popd || _panic } -function parse_golang_version_from_ci_config() { - local line && line=$(grep 'go-version:' <"$PROJECT_ROOT"/.github/workflows/main.yml) - local version && version=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 3) - [ "$version" = "" ] && _panic "Couldn't parse Golang version" +function parse_golang_version_from_go_mod() { + local path=$1 + [ "$path" = "" ] && _panic "Path to Golang interfacer code not passed" + local line && line=$(grep '^go ' <"$path"/go.mod) + local version && version=$(echo "$line" | tr -s ' ' | cut -d ' ' -f 2) + [ "$(echo "$version" | tr -s ' ')" == "" ] && _panic "Couldn't parse Golang version" echo -n "$version" } function install_golang() { - local version && version=$(parse_golang_version_from_ci_config) + local path=$1 + [ "$path" = "" ] && _panic "Path to Golang interfacer code not passed" + local version && version=$(parse_golang_version_from_go_mod "$path") [ "$GOPATH" = "" ] && _panic "GOPATH not set" [ "$GOROOT" = "" ] && _panic "GOROOT not set" echo "Installing Golang v$version... to $GOROOT" @@ -56,14 +60,3 @@ function install_golang() { tar -C "$GOROOT/.." -xzf go.tar.gz go version } - -function build_browsh_binary() { - local path=$1 - pushd "$path" || _panic - local webextension="src/browsh/browsh.xpi" - [ ! -f "$webextension" ] && _panic "browsh.xpi not present" - md5sum "$webextension" - go build ./cmd/browsh - ./browsh --version - popd || _panic -} diff --git a/scripts/releasing.bash b/scripts/releasing.bash index b474a98..f162fcf 100644 --- a/scripts/releasing.bash +++ b/scripts/releasing.bash @@ -1,4 +1,4 @@ -#!/bin/env bash +#!/usr/bin/env bash export BROWSH_VERSION export LATEST_TAGGED_VERSION @@ -8,8 +8,12 @@ function _goreleaser_production() { echo "Installing \`goreleaser'..." go install github.com/goreleaser/goreleaser@v"$GORELEASER_VERSION" fi - pushd "$PROJECT_ROOT"/interfacer/src || _panic - goreleaser release + pushd "$PROJECT_ROOT"/interfacer || _panic + _export_versions + [ "$BROWSH_VERSION" = "" ] && _panic "BROWSH_VERSION unset (goreleaser needs it)" + goreleaser release \ + --config "$PROJECT_ROOT"/goreleaser.yml \ + --rm-dist popd || _panic } @@ -21,9 +25,9 @@ function _export_versions() { } function _parse_browsh_version() { - version_file=$PROJECT_ROOT/interfacer/src/browsh/version.go - line=$(grep 'browshVersion' <"$version_file") - version=$(echo "$line" | grep -o '".*"' | sed 's/"//g') + local version_file=$PROJECT_ROOT/interfacer/src/browsh/version.go + local line && line=$(grep 'browshVersion' <"$version_file") + local version && version=$(echo "$line" | grep -o '".*"' | sed 's/"//g') echo -n "$version" } @@ -39,7 +43,7 @@ function _tag_on_version_change() { echo_versions if ! _is_new_version; then - echo "Not running release as there's no new version." + echo "Not tagging as there's no new version." exit 0 fi @@ -47,15 +51,13 @@ function _tag_on_version_change() { git show v"$BROWSH_VERSION" --quiet git config --global user.email "ci@github.com" git config --global user.name "Github Actions" - # `/dev/null` needed to prevent Github token appearing in logs - git push --tags --quiet https://"$GITHUB_TOKEN"@github.com/browsh-org/browsh >/dev/null 2>&1 - + git add --all git reset --hard v"$BROWSH_VERSION" } function echo_versions() { _export_versions - echo "Browsh Golang version: $BROWSH_VERSION" + echo "Browsh binary version: $BROWSH_VERSION" echo "Git latest tag: $LATEST_TAGGED_VERSION" } @@ -72,21 +74,35 @@ function github_actions_output_version_status() { echo "::set-output name=is_new_version::$status" } -function npm_build_release() { +function webext_build_release() { pushd "$PROJECT_ROOT"/webext || _panic - BROWSH_ENV=RELEASE npm run build_webextension + build_webextension_production popd || _panic } function update_browsh_website_with_new_version() { + _export_versions + local remote="git@github.com:browsh-org/www.brow.sh.git" pushd /tmp || _panic - git clone https://github.com/browsh-org/www.brow.sh.git - cd www.brow.sh || exit 1 + git clone "$remote" + cd www.brow.sh || _panic echo "latest_version: $BROWSH_VERSION" >_data/browsh.yml git add _data/browsh.yml git commit -m "Github Actions: updated Browsh version to $BROWSH_VERSION" - # `/dev/null` needed to prevent Github token appearing in logs - git push --quiet https://"$GITHUB_TOKEN"@github.com/browsh-org/www.brow.sh >/dev/null 2>&1 + git push "$remote" + popd || _panic +} + +function update_homebrew_tap_with_new_version() { + _export_versions + local remote="git@github.com:browsh-org/homebrew-browsh.git" + pushd /tmp || _panic + git clone "$remote" + cd homebrew-browsh || _panic + cp -f "$PROJECT_ROOT"/interfacer/dist/browsh.rb browsh.rb + git add browsh.rb + git commit -m "Github Actions: updated to $BROWSH_VERSION" + git push "$remote" popd || _panic } @@ -99,9 +115,23 @@ function goreleaser_local_only() { popd || _panic } +function build_browsh_binary() { + # Requires $path argument because it's used in the Dockerfile where the GOROOT is + # outside .git/ + local path=$1 + pushd "$path" || _panic + local webextension="src/browsh/browsh.xpi" + [ ! -f "$webextension" ] && _panic "browsh.xpi not present" + md5sum "$webextension" + go build ./cmd/browsh + echo "Freshly built \`browsh' version: $(./browsh --version 2>&1)" + popd || _panic +} + function release() { - npm_build_release + [ "$(git rev-parse --abbrev-ref HEAD)" != "master" ] && _panic "Not releasing unless on the master branch" + webext_build_release + build_browsh_binary "$PROJECT_ROOT"/interfacer _tag_on_version_change _goreleaser_production - update_browsh_website_with_new_version } -- cgit v1.2.3