This was my submission for the DEV.to GitHub Actions 2 Hackathon.
In a world where people are willing to destroy our (only) planet for profit without providing something in return (looking at you NFTs 👀), we, as developers, are responsible for the sustainability of what we create or contribute to.
“If the Internet was a country, it would be the 7th largest polluter” ~ Sustainable Web Manifesto
There are at least three parts on reducing your website’s carbon emissions: Host, Code & Content.
Host: Hosting your website on a host that uses renewable energy can decrease its CO2 emissions significantly. The Green Web Foundation has collected a huge list of them. Additionally, it has created many tools utilizing them, including the Green Web Check tool.
Content: Images are the biggest offenders to the amount of data clients need to download when visiting a website. Compressing them, reducing the amount used and replacing them with SVGs (if possible of course) will all contribute against it. After that, ADs, tracking, other media (eg. sounds & videos) and fonts increase the total data transferred by a lot.
On the action side of things, the action uses lighthouse to get the amount of transferred data which then passes through some calculations to get the amount of CO2 it produces per visit. The Green Web Foundation’s API is also used (if set to true) to determine whether the host is using renewable energy. Lastly, some additional calculations are being done to generate some fun facts about it before logging it in the action output & create comments on the commit or PR that caused it to run. This all happens inside a docker container for two reasons: 1) GitHub uses at max node12 (Current LTS is 16) plus dependencies are being bundled in the repo (which increases its size and makes development difficult). 2) lighthouse requires a browser to run which is far easier and less time consuming to just install in the docker image.
name: Calculate CO2 🌱 on: [push, pull_request] jobs: audit: runs-on: ubuntu-latest steps: - name: Calculate CO2 🌱 uses: GeopJr/[email protected] with: url: "https://geopjr.dev/" renewable: true
Reasoning: Reducing your website’s carbon emissions is just as important as running tests. Accepting a PR or commiting code that increases them by eg. 20% is irresponsible and should be prevented as soon as possible.
Yaml File or Link to Code
Additional Resources / Info
The repo README includes workflows for connecting Vercel & Netlify previews with the action as well as some interesting action conditions.
The renewable energy check is being done by The Green Web Foundation.