← BackAren't the standard actions going too far for you? Write your own one!u11g.com
11/25/2021

Aren't the standard actions going too far for you? Write your own one!

Motivation

Why do you build something like that? The reason is quite simple: everything that I have to do regularly and is essentially always the same, I automate. Tests run automated, linter checks run automated, the CI pipeline runs automated. So why not automate the screenshots as well? ;)

My Workflow

My Github Action is essentially a small NodeJS app that ships as a Dockerfile and can be found in the Marketplace. It uses Github’s @actions/core package, which makes interacting with the infrastructure a breeze.

NodeJS
@actions/core

Those who have already implemented NodeJS applications will have no problems building their own Github Action. I would like to highlight a few things to make it even easier for others to get started.

You need an action.yaml which describes the action:

name: "Websiteshot" 
description: "Github Action to schedule a new Screenshot Job with Websiteshot." 
branding: 
  icon: "camera" 
  color: "blue" 
runs: 
  using: "docker" 
  image: "Dockerfile"

The associated Dockerfile contains a few labels that are necessary for the Marketplace:

FROM node:slim 
# A bunch of `LABEL` fields for GitHub to index 
LABEL "com.github.actions.name"="Websiteshot" 
LABEL "com.github.actions.description"="Github Action to schedule a new Screenshot Job with Websiteshot." 
LABEL "com.github.actions.icon"="gear" 
LABEL "com.github.actions.color"="blue" 
LABEL "repository"="https://github.com/websiteshot/github-action" 
LABEL "homepage"="https://websiteshot.app" 
LABEL "maintainer"="Adam Urban <[email protected]>" 
# Copy over project files 
COPY . . 
# Install dependencies 
RUN npm install 
# Build Project 
RUN npm run build 
# This is what GitHub will run 
ENTRYPOINT ["node", "/dist/index.js"]

The app itself is quite manageable, because it uses the also existing NodeJS package of websiteshot and creates new jobs with the service:

import { Runner } from "./controller/runner.controller"; 
import { Validation } from "./controller/validation.controller"; 
const core = require("@actions/core"); 

async function run() { 
  try { 
    Validation.checkEnvVars(); 
    const jobId: string = await Runner.run(); 
    core.info(`Created Job with Id: ${jobId}`); 
  } catch (error) { 
    core.setFailed(error.message); 
  } 
} 
run();

In this code snippet you can see how the @actions/core package makes it very easy to end an action with an error or to write a log output.

But now to the workflow itself, which is also used by Websiteshot itself to generate new screenshots. After the CI pipeline has run, the last step is to start the Websiteshot action. You have to set a some environment variables that are used by the action.

name: Publish 
on: [push] 
# ... 
jobs: 
  create-screenshot: 
    runs-on: ubuntu-latest 
    name: "Create Screenshot via Template" 
    steps: 
      - uses: websiteshot/github-action@main 
        env: 
          PROJECT_ID: ${{ secrets.PROJECT_ID }} 
          API_KEY: ${{ secrets.API_KEY }} 
          TEMPLATE_ID: "abcdef-ghi..."

My Workflow

Additional Resources / Info

Disclaimer

While writing this post I noticed that it could be interpreted as an ad for Websiteshot. It’s not meant to be, it’s one of my side projects and I think the description of the action can help others or serve as inspiration to build your own action for your own use case. Of course, it’s also possible to create all the screenshots within a Github action (without using an external service). All you need is a headless browser and you’re ready to go. Have fun!

Home
Projects
Newsletter
Blog
Me
Philosophy
Gen Art
Work
Setup
CV
Machine
Dynamic Pong: Territory Conquest Meets Classic Physics
Introducing Camunda Directory
Getting Started with AIME Directory Collections in 5 Minutes
How to use a proxy in a nodejs environment
Introducing weeklyfoo
Create your own epaper calendar with Canvas
Codespaces can become a game changer
Cloudflare pages direct upload with stable preview urls
Building your own Twitter Thread Generator
300 APIs integrated in minutes, not days
Be an orchestration hero
Cut a few braids - new NPM package
ETHme - your chic web3 identity
Automatically update data and commit
Aren't the standard actions going too far for you? Write your own one!
Screenshots - a perfect task to automate!
Send messages to Slack from Camunda Cloud
5 Steps how to track your Team's Mood
Automate your manual tasks with Camunda and Trello!
Go beyond the basics
Is there an alternative to spaghetti?
Play with node-canvas and build something useful
How do I implement a command line tool?
Aime Directory
Boring Dev Tools
Camunda Directory
Data Democracy
Dev Pulse
Dynamic Pong
Lifosy
Art u11g
dapphuntr
DiyPunks
flethy
Maker Oracles
SwagTime
EthME
fcheat
Find the next victim
Generative Arts
MakerDAO Delegates
SkunkWorks NFT
Trees of Life NFT
zukuNFT
RestZeebe
Websiteshot
Zeebetron: How to Manage Multiple Zeebe Profiles with Electron
dot calendar
Sharu
Switch Interface (theme)
LINKEDIN
X
GITHUB