splash image

March 14, 2021

Running a compiled Deno script in a GitHub Action

This is a quick followup to my recent post about Getting Started with Deno.

In this post I will enhance a GitHub Action to do the following:

scan.js

I cross-compiled scan.js using Deno v1.8.1, and uploaded the resulting binaries to a release on GitHub.

Using the releases feature of GitHub is a convenient way to publish compiled artifacts. In this case I used the manual upload feature in GitHub, but this step could be automated as well.

Releases with compiled artifacts in Repo deno-hello

Here is the deno compile command. The binary for Linux is called scan-linux-x86.

deno --unstable compile \
  --allow-net \
  --lite \
  --target x86_64-unknown-linux-gnu \
  --output scan-linux-x86 \
  scan.js

The resulting binaries are quite large (~50MB), even using --lite, but I expect that to improve.

.github/workflow/generate.yaml

In this case, I modified the workflow for the static site in jldec/cloudflare-pages-test. Here is the excerpt of the yaml for the relevant step.

- name: generate output
  run: |
    ...
    npm run generate
    npm run preview &
    curl -LO https://github.com/jldec/deno-hello/releases/download/v1.0.0/scan-linux-x86 && chmod +x scan-linux-x86
    ./scan-linux-x86 http://localhost:3001/
    ...

npm run generate invokes the static site generator.

npm run preview & starts the preview server on port 3001, running in the background.

Both commands are defined as scripts in package.json.

curl -LO downloads the Linux binary from the GitHub release. Running curl here gives the preview server time to start listening on port 3001, before commencing the scan.

Success

When the scan-linux-x86 command finds no broken links in the static site, it exits with 0, allowing the GitHub Action workflow to continue.

scan success in GitHub Action log output

If there are broken links the workflow will fail, and I will hear about it in my inbox :)

scan failure in GitHub Action log output

Deno logo

To leave a comment
please visit dev.to/jldec

debug

user: anonymous

{
  "path": "/blog/running-a-compiled-deno-script-in-a-github-action",
  "attrs": {
    "title": "Running a compiled Deno script in a GitHub Action",
    "splash": {
      "image": "/images/first-blossoms-2021.jpg"
    },
    "date": "2021-03-14",
    "layout": "BlogPostLayout",
    "excerpt": "In this post I enhance a GitHub Action to invoke the compiled scan.js Deno script which scans for broken links in generated HTML pages.\n"
  },
  "md": "# Running a compiled Deno script in a GitHub Action\n\nThis is a quick followup to my recent post about [Getting Started with Deno](getting-started-with-deno).\n\nIn this post I will enhance a GitHub Action to do the following:\n\n- Generate HTML using a static site generator (SSG).\n- Launch a preview web server which runs in the background.\n- Download a compiled Deno script.\n- Invoke the Deno script to scan for broken links in the generated HTML.\n- Only publish the HTML if there are no broken links.\n\n## scan.js\n\nI cross-compiled [scan.js](https://github.com/jldec/deno-hello/blob/main/scan.js) using Deno v1.8.1, and uploaded the resulting binaries to a [release](https://github.com/jldec/deno-hello/releases) on GitHub.\n\nUsing the releases feature of GitHub is a convenient way to publish compiled artifacts. In this case I used the manual upload feature in GitHub, but this step could be automated as well.\n\n![Releases with compiled artifacts in Repo deno-hello](/images/deno-scan-releases.png)\n\nHere is the deno compile [command](https://github.com/jldec/deno-hello/blob/main/compile.sh#L1). The binary for Linux is called `scan-linux-x86`.\n\n```\ndeno --unstable compile \\\n  --allow-net \\\n  --lite \\\n  --target x86_64-unknown-linux-gnu \\\n  --output scan-linux-x86 \\\n  scan.js\n```\n\nThe resulting binaries are quite large (~50MB), even using [--lite](https://deno.land/manual@v1.7.4/tools/compiler#generating-smaller-binaries), but I expect that to improve.\n\n## .github/workflow/generate.yaml\n\nIn this case, I modified the [workflow](https://github.com/jldec/cloudflare-pages-test/blob/main/.github/workflows/generate.yaml) for the static site in [jldec/cloudflare-pages-test](https://github.com/jldec/cloudflare-pages-test). Here is the excerpt of the yaml for the relevant step.\n\n```yaml\n- name: generate output\n  run: |\n    ...\n    npm run generate\n    npm run preview &\n    curl -LO https://github.com/jldec/deno-hello/releases/download/v1.0.0/scan-linux-x86 && chmod +x scan-linux-x86\n    ./scan-linux-x86 http://localhost:3001/\n    ...\n```\n\n`npm run generate` invokes the static site generator.  \n\n`npm run preview &` starts the preview server on port 3001, running in the background.\n\nBoth commands are defined as scripts in [package.json](https://github.com/jldec/cloudflare-pages-test/blob/main/package.json).\n\n`curl -LO` downloads the Linux binary from the GitHub release. Running curl here gives the preview server time to start listening on port 3001, before commencing the scan.\n\n## Success\n\nWhen the `scan-linux-x86` command finds no broken links in the static site, it exits with 0, allowing the GitHub Action [workflow](https://github.com/jldec/cloudflare-pages-test/runs/2112253519?check_suite_focus=true#step:4:72) to continue.\n\n![scan success in GitHub Action log output](/images/scan-success.png)\n\nIf there are broken links the workflow will [fail](https://github.com/jldec/cloudflare-pages-test/runs/2106962300?check_suite_focus=true), and I will hear about it in my inbox :)\n\n![scan failure in GitHub Action log output](/images/scan-failure.png)\n\n> [![Deno logo](/images/deno-logo.png \".no-border\")](https://deno.land/)\n\n_To leave a comment  \nplease visit [dev.to/jldec](https://dev.to/jldec/running-a-compiled-deno-script-in-a-github-action-4ljn)_\n\n\n",
  "html": "<h1>Running a compiled Deno script in a GitHub Action</h1>\n<p>This is a quick followup to my recent post about <a href=\"getting-started-with-deno\">Getting Started with Deno</a>.</p>\n<p>In this post I will enhance a GitHub Action to do the following:</p>\n<ul>\n<li>Generate HTML using a static site generator (SSG).</li>\n<li>Launch a preview web server which runs in the background.</li>\n<li>Download a compiled Deno script.</li>\n<li>Invoke the Deno script to scan for broken links in the generated HTML.</li>\n<li>Only publish the HTML if there are no broken links.</li>\n</ul>\n<h2>scan.js</h2>\n<p>I cross-compiled <a href=\"https://github.com/jldec/deno-hello/blob/main/scan.js\">scan.js</a> using Deno v1.8.1, and uploaded the resulting binaries to a <a href=\"https://github.com/jldec/deno-hello/releases\">release</a> on GitHub.</p>\n<p>Using the releases feature of GitHub is a convenient way to publish compiled artifacts. In this case I used the manual upload feature in GitHub, but this step could be automated as well.</p>\n<p><img src=\"/images/deno-scan-releases.png\" alt=\"Releases with compiled artifacts in Repo deno-hello\"></p>\n<p>Here is the deno compile <a href=\"https://github.com/jldec/deno-hello/blob/main/compile.sh#L1\">command</a>. The binary for Linux is called <code>scan-linux-x86</code>.</p>\n<pre><code>deno --unstable compile \\\n  --allow-net \\\n  --lite \\\n  --target x86_64-unknown-linux-gnu \\\n  --output scan-linux-x86 \\\n  scan.js\n</code></pre>\n<p>The resulting binaries are quite large (~50MB), even using <a href=\"https://deno.land/manual@v1.7.4/tools/compiler#generating-smaller-binaries\">--lite</a>, but I expect that to improve.</p>\n<h2>.github/workflow/generate.yaml</h2>\n<p>In this case, I modified the <a href=\"https://github.com/jldec/cloudflare-pages-test/blob/main/.github/workflows/generate.yaml\">workflow</a> for the static site in <a href=\"https://github.com/jldec/cloudflare-pages-test\">jldec/cloudflare-pages-test</a>. Here is the excerpt of the yaml for the relevant step.</p>\n<pre><code class=\"language-yaml\">- name: generate output\n  run: |\n    ...\n    npm run generate\n    npm run preview &amp;\n    curl -LO https://github.com/jldec/deno-hello/releases/download/v1.0.0/scan-linux-x86 &amp;&amp; chmod +x scan-linux-x86\n    ./scan-linux-x86 http://localhost:3001/\n    ...\n</code></pre>\n<p><code>npm run generate</code> invokes the static site generator.</p>\n<p><code>npm run preview &amp;</code> starts the preview server on port 3001, running in the background.</p>\n<p>Both commands are defined as scripts in <a href=\"https://github.com/jldec/cloudflare-pages-test/blob/main/package.json\">package.json</a>.</p>\n<p><code>curl -LO</code> downloads the Linux binary from the GitHub release. Running curl here gives the preview server time to start listening on port 3001, before commencing the scan.</p>\n<h2>Success</h2>\n<p>When the <code>scan-linux-x86</code> command finds no broken links in the static site, it exits with 0, allowing the GitHub Action <a href=\"https://github.com/jldec/cloudflare-pages-test/runs/2112253519?check_suite_focus=true#step:4:72\">workflow</a> to continue.</p>\n<p><img src=\"/images/scan-success.png\" alt=\"scan success in GitHub Action log output\"></p>\n<p>If there are broken links the workflow will <a href=\"https://github.com/jldec/cloudflare-pages-test/runs/2106962300?check_suite_focus=true\">fail</a>, and I will hear about it in my inbox :)</p>\n<p><img src=\"/images/scan-failure.png\" alt=\"scan failure in GitHub Action log output\"></p>\n<blockquote>\n<p><a href=\"https://deno.land/\"><img src=\"/images/deno-logo.png\" alt=\"Deno logo\" title=\".no-border\"></a></p>\n</blockquote>\n<p><em>To leave a comment<br>\nplease visit <a href=\"https://dev.to/jldec/running-a-compiled-deno-script-in-a-github-action-4ljn\">dev.to/jldec</a></em></p>\n"
}