From e97f68a044993d2a1e0692cb6841beec18e082ff Mon Sep 17 00:00:00 2001 From: Miguel Di Ciurcio Filho Date: Fri, 17 Sep 2021 10:44:41 -0300 Subject: [PATCH 1/2] chore: make the build flexible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes the behavior of `build.ts` by using two new environment variables: BUILD_TARGET and RELEASE_VERSION. BUILD_TARGET must be one of the targets supported by deno. RELEASE_VERSION string used for naming the resulting binaries. Example: BUILD_TARGET=x86_64-unknown-linux-gnu \ RELEASE_VERSION=v0.1.0 \ deno run ... build.ts If the variables don’t exist, `build.ts` builds a binary based on runtime in use. --- cli/build.ts | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/cli/build.ts b/cli/build.ts index 777844f..2428aaf 100644 --- a/cli/build.ts +++ b/cli/build.ts @@ -2,7 +2,6 @@ import { bundle } from "https://deno.land/x/buckets@0.1.0/mod.ts"; import * as esbuild from "https://deno.land/x/esbuild@v0.12.19/mod.js"; import conf from "./buckets.ts"; -import { PIPELINIT_VERSION } from "./deps.ts"; const TARGETS = [ "x86_64-unknown-linux-gnu", @@ -11,6 +10,24 @@ const TARGETS = [ "aarch64-apple-darwin", ]; +const target: string | undefined = Deno.env.get("BUILD_TARGET"); +const version: string | undefined = Deno.env.get("RELEASE_VERSION"); + +if (target && !TARGETS.includes(target)) { + console.error( + `Invalid build target '${target}'. Must be one of:`, + ); + console.error(TARGETS.join("\n")); + Deno.exit(1); +} + +if (target && !version) { + console.error( + "The environment variable RELEASE_VERSION must be defined if BUILD_TARGET is also defined", + ); + Deno.exit(1); +} + // Bundle and minify await bundle(conf); const beforeMinify = await Deno.readTextFile(conf.output); @@ -20,7 +37,7 @@ const result = await esbuild.transform(beforeMinify, { await Deno.writeTextFile(conf.output, result.code); esbuild.stop(); -const compile = async function (target?: string) { +const compile = async function (target?: string, version?: string) { const cmd = [ "deno", "compile", @@ -28,12 +45,12 @@ const compile = async function (target?: string) { "--allow-read=.", "--allow-write", ]; - if (target) { + if (target && version) { cmd.push( "--target", target, "--output", - `bin/pipelinit-${PIPELINIT_VERSION}-${target}`, + `bin/pipelinit-${version}-${target}`, ); } else { cmd.push("--output", "bin/pipelinit"); @@ -47,14 +64,14 @@ const compile = async function (target?: string) { } }; -const tar = async function (target: string) { +const tar = async function (target: string, version: string) { const p = Deno.run({ cwd: "bin", cmd: [ "tar", "-czf", - `pipelinit-${PIPELINIT_VERSION}-${target}.tar.gz`, - `pipelinit-${PIPELINIT_VERSION}-${target}`, + `pipelinit-${version}-${target}.tar.gz`, + `pipelinit-${version}-${target}`, ], }); @@ -64,14 +81,14 @@ const tar = async function (target: string) { } }; -const zip = async function (target: string) { +const zip = async function (target: string, version: string) { const p = Deno.run({ cwd: "bin", cmd: [ "zip", "-9", - `pipelinit-${PIPELINIT_VERSION}-${target}.zip`, - `pipelinit-${PIPELINIT_VERSION}-${target}.exe`, + `pipelinit-${version}-${target}.zip`, + `pipelinit-${version}-${target}.exe`, ], }); @@ -81,18 +98,18 @@ const zip = async function (target: string) { } }; -const compress = async function (target: string) { +const compress = async function (target: string, version: string) { if (target === "x86_64-pc-windows-msvc") { - await zip(target); + await zip(target, version); } else { - await tar(target); + await tar(target, version); } }; -await compile(); -for (const target of TARGETS) { - await compile(target); - await compress(target); +await compile(target, version); + +if (target && version) { + await compress(target, version); } await Deno.remove(conf.output); From b67f7f5d68c72a724ffea0247841764077bd9604 Mon Sep 17 00:00:00 2001 From: Miguel Di Ciurcio Filho Date: Fri, 17 Sep 2021 11:00:59 -0300 Subject: [PATCH 2/2] feat: creates a new release workflow The new workflow does the following: - Builds binaries for all supported platforms - Builds and publish a Docker image - Creates and publishes the artifacts to a GitHub release To trigger the workflow: - Create a tag for the intended release: `git tag v0.1.0` - Push the tag: `git push --tags` The release is published as a prerelease and needs further information at the moment, like the changelog. --- .github/workflows/release.yaml | 90 ++++++++++++++++++++++++++++++++++ cli/Dockerfile | 10 ++++ 2 files changed, 100 insertions(+) create mode 100644 .github/workflows/release.yaml create mode 100644 cli/Dockerfile diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..bd6738c --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,90 @@ +name: Release new version + +on: + push: + tags: + - 'v*.*.*' + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + PRERELEASE: true + +jobs: + release: + name: Build and publish new release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set current version + run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV + - uses: denoland/setup-deno@v1 + with: + deno-version: v1.14.0 + + - name: Build Linux x86_64 binary + env: + BUILD_TARGET: "x86_64-unknown-linux-gnu" + RELEASE_VERSION: "${{ env.RELEASE_VERSION }}" + run: | + cd cli; deno run --unstable --allow-read --allow-write --allow-net --allow-env --allow-run build.ts + + - name: Log in to the Container registry + uses: docker/login-action@v1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v3 + with: + tags: | + type=semver,pattern={{version}},enable=true + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + uses: docker/build-push-action@v2 + with: + build-args: "version=${{ env.RELEASE_VERSION }}" + context: cli/ + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + - name: Build Windows x86_64 binary + env: + BUILD_TARGET: "x86_64-pc-windows-msvc" + RELEASE_VERSION: "${{ env.RELEASE_VERSION }}" + run: | + cd cli + deno run --unstable --allow-read --allow-write --allow-net --allow-env --allow-run build.ts + + - name: Build Mac x86_64 binary + env: + BUILD_TARGET: "x86_64-apple-darwin" + RELEASE_VERSION: "${{ env.RELEASE_VERSION }}" + run: | + cd cli + deno run --unstable --allow-read --allow-write --allow-net --allow-env --allow-run build.ts + + - name: Build Mac aarch64 binary + env: + BUILD_TARGET: "aarch64-apple-darwin" + RELEASE_VERSION: "${{ env.RELEASE_VERSION }}" + run: | + cd cli + deno run --unstable --allow-read --allow-write --allow-net --allow-env --allow-run build.ts + + - name: Publish the release + uses: softprops/action-gh-release@v1 + with: + name: "Pipelinit ${{ env.RELEASE_VERSION }}" + prerelease: ${{ env.PRELEASE }} + tag_name: ${{ env.RELEASE_VERSION }} + files: | + cli/bin/pipelinit-${{ env.RELEASE_VERSION }}-x86_64-unknown-linux-gnu.tar.gz + cli/bin/pipelinit-${{ env.RELEASE_VERSION }}-x86_64-pc-windows-msvc.zip + cli/bin/pipelinit-${{ env.RELEASE_VERSION }}-x86_64-apple-darwin.tar.gz + cli/bin/pipelinit-${{ env.RELEASE_VERSION }}-aarch64-apple-darwin.tar.gz diff --git a/cli/Dockerfile b/cli/Dockerfile new file mode 100644 index 0000000..0cb9953 --- /dev/null +++ b/cli/Dockerfile @@ -0,0 +1,10 @@ +FROM debian:11-slim + +ARG version +COPY bin/pipelinit-${version}-x86_64-unknown-linux-gnu /pipelinit + +VOLUME ["/app"] + +WORKDIR /app + +ENTRYPOINT ["/pipelinit"]