All Projects → paulphys → nextjs-cron

paulphys / nextjs-cron

Licence: other
Cron jobs with Github Actions for Next.js apps on Vercel▲

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to nextjs-cron

deploy-to-vercel-action
🎬▲ Deploy your project to Vercel using GitHub Actions. Supports PR previews and GitHub deployments.
Stars: ✭ 84 (-41.67%)
Mutual labels:  github-actions, vercel
nextjs-github-pages
🚀 Deploy a Next.js app to Github Pages via Github Actions.
Stars: ✭ 89 (-38.19%)
Mutual labels:  github-actions, vercel
mojito-admin-starter
此项目主要为了演示如何自动化 Fullstack project 的 Infrastructure。
Stars: ✭ 17 (-88.19%)
Mutual labels:  github-actions, vercel
request-on-steroids
An HTTP client ✨ with retry, circuit-breaker and tor support 📦 out-of-the-box
Stars: ✭ 19 (-86.81%)
Mutual labels:  github-actions
ghaction-virustotal
GitHub Action to upload and scan files with VirusTotal
Stars: ✭ 105 (-27.08%)
Mutual labels:  github-actions
website
The Algorithms website providing GitHub's largest open-source algorithm library.
Stars: ✭ 616 (+327.78%)
Mutual labels:  vercel
jahir.dev
My personal website 💎 – Built using Next.js, TypeScript, MDX, contentlayer, Notion and Stitches styled components
Stars: ✭ 119 (-17.36%)
Mutual labels:  vercel
point-vue-cron
vue component: cron expression generator
Stars: ✭ 21 (-85.42%)
Mutual labels:  cron
ts-scaffold
🏗 ts-scaffold - Scaffold project for Typescript projects, with Unit Tests and basic dependencies set up
Stars: ✭ 13 (-90.97%)
Mutual labels:  github-actions
linkin
Linkin is a customizable self hosted link tree platform.
Stars: ✭ 62 (-56.94%)
Mutual labels:  vercel
nx-extend
Nx Workspaces builders and tools
Stars: ✭ 67 (-53.47%)
Mutual labels:  vercel
setup-bats
GitHub Action to setup BATS testing framework
Stars: ✭ 25 (-82.64%)
Mutual labels:  github-actions
netlify-cms-oauth-provider-node
A stateless external OAuth provider for netlify-cms with built-in support for Vercel serverless functions
Stars: ✭ 30 (-79.17%)
Mutual labels:  vercel
actions-deploy-gist
📌 Deploy file to Github Gist
Stars: ✭ 26 (-81.94%)
Mutual labels:  github-actions
jr.mitou.org
未踏ジュニアの公式Webサイトです! YAML ファイルで更新できます 🛠💨
Stars: ✭ 17 (-88.19%)
Mutual labels:  github-actions
grammer-blog
My personal blog about programming or random stuff. Made using Vue JS 2 and Gridsome as a jamstack framework for Vue JS. Hosted on vercel
Stars: ✭ 14 (-90.28%)
Mutual labels:  vercel
templates
Collection of Conan recipe + CI templates
Stars: ✭ 71 (-50.69%)
Mutual labels:  github-actions
netlify-build-github-actions
An example of triggering a Netlify build using Github Actions Scheduled Events
Stars: ✭ 31 (-78.47%)
Mutual labels:  github-actions
action-eslint
🐋🐬 TypeScript/JavaScript ESLint action
Stars: ✭ 24 (-83.33%)
Mutual labels:  github-actions
chicio.github.io
👻 Fabrizio Duroni (me 😄) personal website. Created using GatsbyJS, Styled Components, Storybook, Typescript, tsParticles, GitHub pages, Github Actions, Upptime.
Stars: ✭ 20 (-86.11%)
Mutual labels:  github-actions

Next.js Cron

Cron jobs with Github Actions for Next.js applications on Vercel▲

Motivation

Since the Vercel platform is event-driven, therefore not maintaining a running server, you can't really schedule calls on your API routes or Serverless functions in your Next.js application. Although there are many pre-existing services that provide scheduled cron jobs, I ultimately decided that Github Actions suits my needs the best, since it integrates nicely with any project that already lives on Github, plus it's completely free.

Get started

All Github Actions reside in the directory .github/workflows/ of your repository and are written in YAML.

.github/workflows/starter.yaml is the most basic workflow to help you get started with Actions.

Scheduled tasks

With Scheduled events you can execute tasks at specified intervals. For instance, the provided workflow .github/workflows/scheduled.yaml executes a HTTP request with curl every 60 minutes.

name: Hourly cron job
on:
  schedule:
    - cron: '*/60 * * * *'
jobs:
  cron:
    runs-on: ubuntu-latest
    steps:
      - name: Hourly cron job
        run: |
          curl --request POST \
          --url 'https://example.com/api/task' \
          --header 'Authorization: Bearer ${{ secrets.ACTION_KEY }}'

If you are having trouble writing cron schedule expressions, take a look at crontab guru.

Next.js API routes

API routes and Serverless functions provide a straightforward solution to building your API with Next.js on Vercel. Any file inside the folder pages/api is mapped to /api/* and will be treated as an API endpoint instead of a page.

If you are using serverless functions, regardless of the Runtime, you would need to put the files into the /api/ directory at your project's root.

Authorization flow

To securely trigger API routes and serverless functions with Github Actions, you need to provide an authorization key in the header of your API call, which, when executed, gets compared to a corresponding key in your Next.js application.

You can achieve this by adding Encrypted Secrets to your Github repository and passing them with the header of your HTTP request, like shown in the previous code snippet. Along with adding the key to your Github repository, you also need to access it within your Next.js application, preferably through Environment Variables.

The example pages/api/example.js implements this authorization flow.

export default function handler(req, res) {

  const { APP_KEY } = process.env;
  const { ACTION_KEY } = req.headers.authorization.split(" ")[1];

  try {
    if (ACTION_KEY === APP_KEY) {
      // Process the POST request
      res.status(200).json({ success: 'true' })
    } else {
      res.status(401)
    }
  } catch(err) {
    res.status(500)
  }
}

Use pages/api/example.ts for Typescript.

import type { NextApiRequest, NextApiResponse } from 'next'

export default function handler(req:NextApiRequest, res:NextApiResponse) {

  const { APP_KEY } = process.env;
  const { ACTION_KEY } = req.headers.authorization.split(" ")[1];

  try {
    if (ACTION_KEY === APP_KEY) {
      // Process the POST request
      res.status(200).json({ success: 'true' })
    } else {
      res.status(401)
    }
  } catch(err) {
    res.status(500)
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].