actions / actions/toolkit

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

Open
#2,009 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
5.9k
Forks
1.8k
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the workflow setup and the script's artifactClient.uploadArtifact call, then inspect how @actions/artifact obtains its runtime credentials in this execution context. Reproduce the workflow and verify that the artifact uploads successfully without an unavailable environment variable; the issue provides no named repository test or source file.

Written by the indexing model from the issue text.

Assessment

Tech stack
github-actions, node.js, typescript
Domain
ci-cd
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.