Skip to content

Commit

Permalink
feat: set-env action
Browse files Browse the repository at this point in the history
BREAKING:
First major release
  • Loading branch information
allenevans committed Sep 22, 2019
1 parent d31df37 commit f69c6b5
Show file tree
Hide file tree
Showing 18 changed files with 435 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 140

[*.md]
trim_trailing_whitespace = false
41 changes: 41 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: "build"

on: [pull_request]

jobs:
validation:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1

- name: "Config"
run: |
git config --global user.email "git-ci@techinity.com"
git config --global user.name "gitci"
- name: "Install dependencies"
run: npm install

- name: "Build"
run: npm run build

- name: "Test"
run: npm run test

- name: "Lint"
run: npm run lint

- name: "Commit build output"
run: |
git checkout -B "dist/artifacts-${{github.event.number}}"
git reset ${{github.event.after}}
git add -f dist/
git commit -m "ci: build distributables"
git push -f https://${{secrets.GH_ACTION_TOKEN}}@github.com/allenevans/set-env.git HEAD:dist/artifacts-${{github.event.number}}
- name: "Check for uncommitted changes"
run: |
git diff --exit-code --stat -- . ':!node_modules' \
|| (echo "##[error] found changed filces after build." \
&& exit 1)
46 changes: 46 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: "release"
on:
push:
branches:
- 'master'

jobs:
validation:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1

- name: "Config"
run: |
echo "Release triggered by ${{github.actor}}"
echo "Commit ${{github.event.commits[0].message}}"
git config --global user.email "git-ci@techinity.com"
git config --global user.name "gitci"
- name: "Install dependencies"
run: npm install

- name: "Build"
run: npm run build

- name: "Test"
run: npm run test

- name: "Commit build artifacts"
if: |
contains(github.event.commits[0].message, '[skip ci]') == false &&
contains(github.event.commits[0].message, 'release artifacts') == false
run: |
git add --ignore-errors -f dist/
git diff-index --quiet HEAD -- || git commit -m 'release artifacts'
- name: "Semantic version"
if: |
contains(github.event.commits[0].message, '[skip ci]') == false &&
contains(github.event.commits[0].message, 'release artifacts') == false
env:
GH_TOKEN: ${{secrets.GITHUB_ACTION_TOKEN}}
run: |
npm run release
git status
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package-lock.json

# Logs
logs
*.log
npm-debug.log*

# Coverage directory used by tools like istanbul
coverage
*.lcov

# Dependency directories
node_modules/

# Optional REPL history
.node_repl_history

# Output
dist/
*.tgz
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock=false
save-exact
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"arrowParens": "always",
"jsxBracketSameLine": false,
"parser": "typescript",
"printWidth": 140,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"useTabs": false
}
10 changes: 10 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"branch": "master",
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/git"
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Allen Evans

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
# set-env
Github action to set environment variables

Example usage:-

```
name: example-pipline
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: changed packages
id: changed_packages
uses: allenevans/set-env@master
- name: Dump steps context
env:
STEPS_CONTEXT: ${{ toJson(steps) }}
run: |
echo "$STEPS_CONTEXT"
echo "scope=${{steps.changed_packages.outputs.scope}}"
```
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: 'set-env'
description: 'Set environment variables that can be used in other steps'
author: 'Allen Evans'
runs:
using: 'node12'
main: 'dist/set-env.js'
1 change: 1 addition & 0 deletions docs/contributors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Contributors
17 changes: 17 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
collectCoverage: true,
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
resetMocks: true,
testMatch: [
'**/?(*.)spec.ts',
],
testPathIgnorePatterns: [
'dist/',
'node_modules/',
],
testURL: 'http://localhost/',
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
verbose: true,
};
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "set-env",
"version": "0.0.1",
"private": true,
"description": "Set environment variables that can be used in other steps",
"main": "dist/set-env.js",
"scripts": {
"build": "webpack --config ./webpack.config.js",
"clean": "rimraf dist/",
"lint": "tslint -c tslint.json --project tsconfig.json",
"prebuild": "npm run clean",
"release": "semantic-release",
"test": "jest --config=jest.config.js --silent"
},
"repository": {
"type": "git",
"url": "/~https://github.com/allenevans/set-env.git"
},
"keywords": [
"actions",
"node",
"setup"
],
"author": "Allen Evans",
"license": "MIT",
"devDependencies": {
"@actions/core": "1.1.1",
"@semantic-release/changelog": "3.0.4",
"@semantic-release/commit-analyzer": "6.3.0",
"@semantic-release/git": "7.0.16",
"@semantic-release/npm": "5.1.15",
"@semantic-release/release-notes-generator": "7.3.0",
"@types/jest": "24.0.18",
"@types/node": "12.7.5",
"jest": "24.9.0",
"prettier": "1.18.2",
"rimraf": "3.0.0",
"semantic-release": "15.13.24",
"ts-jest": "24.1.0",
"ts-loader": "6.1.2",
"ts-node": "8.4.1",
"tslint": "5.20.0",
"tslint-config-airbnb": "5.11.2",
"typescript": "3.6.3",
"webpack": "4.40.2",
"webpack-cli": "3.3.9"
}
}
54 changes: 54 additions & 0 deletions src/main.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as core from '@actions/core';

jest.mock('@actions/core', () => ({
exportVariable: jest.fn(),
setFailed: jest.fn(),
}));

describe('set-env', () => {
afterEach(() => {
Object.keys(process.env)
.filter((key) => key.match(/^INPUT_/))
.forEach((key) => {
delete process.env[key];
});
});

it('should export all environment variables beginning with INPUT_', () => {
process.env.INPUT_VERSION = '1.2.3';

jest.isolateModules(() => require('./main'));

expect(core.exportVariable).toHaveBeenCalledTimes(1);
expect(core.exportVariable).toHaveBeenCalledWith('VERSION', '1.2.3');
});

it('should export multiple environment variables beginning with INPUT_', () => {
process.env.INPUT_A = '1';
process.env.INPUT_B = '2';
process.env.INPUT_C = '3';

jest.isolateModules(() => require('./main'));

expect(core.exportVariable).toHaveBeenCalledTimes(3);
expect(core.exportVariable).toHaveBeenCalledWith('A', '1');
expect(core.exportVariable).toHaveBeenCalledWith('B', '2');
expect(core.exportVariable).toHaveBeenCalledWith('C', '3');
});

it('should report exceptions', () => {
(<jest.Mock>core.exportVariable).mockImplementation(() => {
throw new Error('FAIL');
});
jest.spyOn(process, 'exit').mockImplementation((code?: number): never => {
throw new Error(`${code}`);
});

process.env.INPUT_TEST = 'test';

jest.isolateModules(() => expect(() => require('./main')).toThrowError('1'));

expect(core.setFailed).toHaveBeenCalledTimes(1);
expect(core.setFailed).toHaveBeenCalledWith('FAIL');
});
});
14 changes: 14 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as core from '@actions/core';

(function run() {
try {
Object.keys(process.env)
.filter((key) => /^INPUT_/.test(key))
.forEach((key) => {
core.exportVariable(key.replace(/^INPUT_/, ''), `${process.env[key]}`);
});
} catch (error) {
core.setFailed(error.message);
process.exit(1);
}
})();
Loading

0 comments on commit f69c6b5

Please sign in to comment.