I'm attempting to set an environment variable for my Node.js application using GitHub Actions. However, when I run the workflow, it appears that the application isn't reading the variable correctly, as it returns an 'invalid token' error. Additionally, I'd like to know how to use a .env
file to set this variable within GitHub Actions.
Here is the workflow:
name: Pay and Connect Voucher Request
on:
schedule:
- cron: '00 20 * * 1-5'
jobs:
request:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "18.15"
- name: Install Dependencies
run: npm install
- name: Setting the environment variables
run: |
echo "AUTHORIZATION_TOKEN=$AUTHORIZATION_TOKEN" >> $GITHUB_ENV
env:
AUTHORIZATION_TOKEN: ${{ secrets.AUTHORIZATION_TOKEN }}
- name: Run Program
run: npm start
This is the code I'm running from src/index.ts
:
import axios from "axios";
import dotenv from "dotenv";
dotenv.configDotenv();
const requestURL: string =
"https://uct.api.getslideapp.com/2/connect/vouchers/issue/";
const authorizationToken = process.env.AUTHORIZATION_TOKEN;
async function sendRequest() {
try {
const { data, status } = await axios.post(
requestURL,
{},
{
headers: {
"Content-Type": "application/json",
Origin: "https://app.payandconnect.co.za",
Referer: "https://app.payandconnect.co.za/u/vouchers",
Authorization: authorizationToken,
},
}
);
} catch (error) {
if (axios.isAxiosError(error)) {
console.log("message: ", error.response?.data);
} else {
console.log("unexpected error: ", error);
}
}
}
sendRequest();
Here is the error I'm getting:
> [email protected] start
> ts-node src/index.ts
TOKEN***
message: { status: 'error', message: 'Token is invalid.' }
Based on the information provided, it seems that you are facing two issues with setting the environment variable for your Node.js application using GitHub Actions. The first issue is that the application is not reading the variable correctly, resulting in an 'invalid token' error. The second issue is that you would like to know how to use a .env file to set this variable within GitHub Actions.
To address the first issue, it appears that the environment variable
AUTHORIZATION_TOKEN
is not being set correctly in the GitHub Actions workflow. This could be causing the 'invalid token' error when the application tries to read the variable. Have you confirmed that theAUTHORIZATION_TOKEN
secret is set properly in your GitHub repository secrets?Now, regarding the second issue, to use a .env file to set the variable within GitHub Actions, you can use the
dotenv
package to load the variable from a .env file. However, I noticed a small typo in your code where you useddotenv.configDotenv()
instead ofdotenv.config()
. Please update this line todotenv.config()
to correctly load the variable from the .env file.Have you tried making these changes and see if they resolve the issues you are facing? Let me know if you need further assistance.