February 22, 2024
O. Wolfson
In this article, we'll create a GitHub Action that runs once a day at midnight UTC to execute a script. This script interacts with an external API that requires environment variables for authentication. While the specifics of the API are not detailed, the principles apply broadly, including for services like Supabase, APIs requiring API keys, or similar authentication methods.
.github folder, then into the workflows folder. If these don't exist, create them.daily_api_interaction.yml. The .yml extension is necessary as GitHub Actions workflows are defined in YAML.Open daily_api_interaction.yml and start defining your workflow:
yamlname: Daily API Interaction
on:
schedule:
- cron: "0 0 * * *" # Runs at midnight UTC every day
workflow_dispatch: # Allows manual triggering
jobs:
interact_with_api:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "16" # Or any version you require
- name: Install dependencies
run: npm install
- name: Execute script
run: node path/to/your_script.js
env:
API_KEY: ${{ secrets.API_KEY }}
API_SECRET: ${{ secrets.API_SECRET }}
API_KEY and API_SECRET, for example. Fill in the "Name" and "Value" fields accordingly and save.cron: '0 0 * * *' expression specifies the job runs at 00:00 (midnight) UTC every day.workflow_dispatch event from the GitHub UI under the "Actions" tab of your repository.env block under the Execute script step makes the secrets available as environment variables within the script context.actions/setup-node matches the version required by your project.By following these steps, you've set up a GitHub Action to securely interact with an external API on a daily schedule, demonstrating how to automate tasks while keeping sensitive data protected.