Skip to content

Commit

Permalink
Add setGitHubEnvVar utility
Browse files Browse the repository at this point in the history
  • Loading branch information
firefoxic committed May 11, 2024
1 parent 825feba commit cee9272
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.

## [Unreleased]

### Added

- The `setGitHubEnvVar` utility that adds the specified environment variable with the specified value if the `CI` environment variable is set.

## [0.3.0] — 2024–05–11

### Changed
Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,45 @@ export default defineConfig({
})
```

### `setGitHubEnvVar`

Your action must accept the environment variable you pass into it from the JS. For example, you can pass the path to the distribution directory from the `vite.config.js`:

```js
import { defineConfig } from "vite"

import { setGitHubEnvVar } from "@firefoxic/utils"

const PATH_TO_SRC = `./a_very_strange_path_to_source`
const PATH_TO_DIST = `./a_very_strange_path_to_dist`

setGitHubEnvVar(`PATH_TO_DIST`, PATH_TO_DIST)

export default defineConfig({
root: PATH_TO_SRC,
build: {
outDir: `.${PATH_TO_DIST}`,
},
})
```

Then you can use this variable in the deploy step of your action:

```yaml
jobs:
job_name:
runs-on: ubuntu-latest
steps:

# Any steps before deploying a project

- name: Deploy
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: ${{ env.PATH_TO_DIST }}
branch: gh-pages
```
[license-url]: https://github.com/firefoxic/utils/blob/main/LICENSE.md
[license-image]: https://img.shields.io/badge/License-MIT-limegreen.svg
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { getProjectRoot } from "./getProjectRoot.js"

export { setGitHubEnvVar } from "./setGitHubEnvVar.js"
16 changes: 16 additions & 0 deletions lib/setGitHubEnvVar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { appendFile } from "node:fs/promises"
import { env } from "node:process"

/**
* Sets an environment variable in the GitHub Actions environment file.
*
* @param {string} varName The name of the environment variable to set.
* @param {string} varValue The value of the environment variable to set.
*
* @returns {Promise<void>}
*/
export async function setGitHubEnvVar (varName, varValue) {
if (!env.CI) return

await appendFile(env.GITHUB_ENV, `\n${varName}=${varValue}`)
}

0 comments on commit cee9272

Please sign in to comment.