Skip to content

artifact.uploadArtifact Fails with "Unable to get the ACTIONS_RUNTIME_TOKEN env variable" #2009

Open
@jason89521

Description

@jason89521

Describe the Problem

Calling artifactClient.uploadArtifact results in the following error:

Error: Unable to get the ACTIONS_RUNTIME_TOKEN env variable

The Workflow File

name: FooBar

run-name: 'FooBar'

on:
  pull_request:
    branches:
      - master
    types: [opened, synchronize, reopened, ready_for_review]

jobs:
  exec-script:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '16'

      - name: Install dependencies
        run: npm ci

      - name: Execute the script
        run: node ./scripts/my-script.mjs
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          BASE_STATS_PATH: ./base/stats.json
          CURRENT_STATS_PATH: ./current/stats.json

Script File

import { DefaultArtifactClient } from '@actions/artifact'
import * as core from '@actions/core'
import { context, getOctokit } from '@actions/github'
import { createHTMLArtifact } from '@bundle-stats/cli-utils'
import { createJobs, createReport } from '@bundle-stats/utils'
import { readFile, writeFile } from 'fs/promises'
import process from 'node:process'

const {
	repo: { owner, repo: repoName },
	issue: { number: prNumber },
} = context
const githubToken = getEnv('GITHUB_TOKEN')
const baseStatsPath = getEnv('BASE_STATS_PATH')
const currentStatsPath = getEnv('CURRENT_STATS_PATH')
const octokit = getOctokit(githubToken)
const artifactClient = new DefaultArtifactClient()

;(async () => {
        // ...
        // Generate the report
	const bundleStatsHTML = createHTMLArtifact(jobs, report)
	await writeFile('bundle-stats.html', bundleStatsHTML)
	core.info('Uploading artifact')
	try {
		const { id: artifactId } = await artifactClient.uploadArtifact('bundle-stats-html', ['./bundle-stats.html'], './')
		if (!artifactId) {
			core.setFailed('Failed to upload artifact')
			return
		}
	} catch (error) {
		console.error(error)
	}

	// Create comment and so on...
        // ...
})()

Question

I'm unsure why calling artifactClient.uploadArtifact results in the error:

Error: Unable to get the ACTIONS_RUNTIME_TOKEN env variable

Do I need to manually set this environment variable? Or is there something missing in my workflow setup?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions