Skip to content

Commit

Permalink
feat(v1): v1 features (#3)
Browse files Browse the repository at this point in the history
Implementing those options:

|  Parameter  | Required | Description                                                                                                                            | Example                                                                                                 |
|:-----------:|:--------:|----------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------|
|  repository |   true   | The repository ref <owner>/<repo>                                                                                                      | `repository: "Steph0/dotenv-configserver"`                                                              |
|    token    |   true   | This should be a token with access to your repository scoped in as a secret                                                            | `token: ${{ secrets.GITHUB_TOKEN }}`                                                                    |
|    branch   |   false  | The remote branch to checkout (default: main)                                                                                          | `branch: "staging"`                                                                                     |
| destination |   false  | The working folder to write configuration to (default 'RUNNER_TEMP')                                                                   | `destination: "/my/dest/folder"`                                                                        |
|  directory  |   false  | Look for file in configserver subdirectory (default '.').<br>Useful if your configserver hosts several config directories in it        | `directory: "my-app-dir"`                                                                               |
|   filename  |   false  | The config filename (default to '.env')                                                                                                | `filename: "my-application.env"`                                                                        |
|   profile   |   false  | Profile for file (ex: 'prod' will make tool <br>look for <filename_part>-<profile>.<filename_extension>)<br><br>If empty, won't apply. | `profile: "prod"`<br>Depending on filename will make action look for file:<br>`my-application-prod.env` |
|   cleanup   |   false  | If false, won't delete configuration files downloaded after loading to GITHUB_ENV (default: true)                                      | `cleanup: false`                                                                                        |
  • Loading branch information
Steph0 authored and smt-ofa committed Apr 19, 2021
1 parent 925ff5b commit f04de18
Show file tree
Hide file tree
Showing 16 changed files with 11,824 additions and 4,257 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/act-test.event
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"action": "workflow_dispatch",
"inputs": {
"repository": "Steph0/test-configserver",
"token": "",
"branch": "test-profile",
"directory": "test",
"filename": "application.env",
"profile": "prod"
}
}
52 changes: 52 additions & 0 deletions .github/workflows/act-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: "Test action"
on:
workflow_dispatch:
inputs:
repository:
description: "Repository to fetch (<owner>/<repo>)"
required: true
token:
description: "Access token - PAT"
required: true
branch:
description: "Remote branch to checkout (default: main)"
required: false
directory:
description: "Look for file in subdirectory (default '.')"
required: false
filename:
description: "Config filename, with or without extension (default to '.env')"
required: false
profile:
description: "Profile for file (ex: 'prod' ~= prod.env)"
required: false
destination:
description: "Working folder to write configuration to (default '.')"
required: false
cleanup:
description: "If false, won't delete configuration files (default true)"
required: false

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: "Checkout"
id: checkout
uses: actions/checkout@v2

- name: "Launch action"
uses: ./
with:
repository: "${{ github.event.inputs.repository }}"
token: "${{ github.event.inputs.token }}"
branch: "${{ github.event.inputs.branch }}"
directory: "${{ github.event.inputs.directory }}"
filename: "${{ github.event.inputs.filename }}"
profile: "${{ github.event.inputs.profile }}"
destination: "${{ github.event.inputs.destination }}"
cleanup: "${{ github.event.inputs.cleanup }}"

# You should see your .env config in 'env'
- name: "See exported values"
run: env
25 changes: 0 additions & 25 deletions .github/workflows/test.yml

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ typings/

# next.js build output
.next

# Custom
*.secrets
*.secret
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* @Steph0
* @actions/actions-runtime
237 changes: 166 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,116 +1,211 @@
# Create a JavaScript Action
# Environment from remote dotenv config server

<p align="center">
<a href="https://github.com/actions/javascript-action/actions"><img alt="javscript-action status" src="https://github.com/actions/javascript-action/workflows/units-test/badge.svg"></a>
</p>
Load dotenv (.env) files from a remote repository and loads it to `GITHUB_ENV`.

Use this template to bootstrap the creation of a JavaScript action.:rocket:
Env variables will be then available using `${{ env.<KEY> }}` in your later jobs/actions.

This template includes tests, linting, a validation workflow, publishing, and versioning guidance.
**This action can work as a workaround from missing Github Environments tab in Github Teams plan**

If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action)
## Configuration

## Create an action from this template
| Parameter | Required | Description | Example |
|:-----------:|:--------:|----------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| repository | true | The remote repository (configserver). Format: `<owner>/<repo>` | `repository: "Steph0/dotenv-configserver"` |
| token | false | This should be a token with access to your repository scoped in as a secret (default to GITHUB_TOKEN) | `token: ${{ secrets.GITHUB_TOKEN }}` |
| branch | false | The remote branch to checkout (default: main) | `branch: "staging"` |
| destination | false | The working folder to write configuration to (default 'RUNNER_TEMP') | `destination: "/my/dest/folder"` |
| directory | false | Look for file in configserver subdirectory (default '.').<br>Useful if your configserver hosts several config directories in it | `directory: "my-app-dir"` |
| filename | false | The config filename (default to '.env') | `filename: "my-application.env"` |
| profile | false | Profile for file (ex: 'prod' will make tool <br>look for <filename_part>-<profile>.<filename_extension>)<br><br>If empty, won't apply. | `profile: "prod"`<br>Depending on filename will make action look for file:<br>`my-application-prod.env` |
| cleanup | false | If false, won't delete configuration files downloaded after loading to GITHUB_ENV (default: true) | `cleanup: false` |

Click the `Use this Template` and provide the new repo details for your action

## Code in Main
## Usage

Install the dependencies
This action allows many directory structure in your configserver.
This section illustrates dotenv-configserver configurations according to common examples of configserver directory structures:

```bash
npm install
```
### Basic

Run the tests :heavy_check_mark:
Configserver:
<pre>
(main branch)
|_ .env
</pre>

```bash
$ npm test
Github Action:

PASS ./index.test.js
✓ throws invalid number (3ms)
wait 500 ms (504ms)
test runs (95ms)
...
```yaml
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: "Checkout"
id: checkout
uses: actions/checkout@v2

- name: "Launch action"
uses: Steph0/dotenv-configserver
with:
repository: "Steph0/test-configserver"
token: "${{ secrets.ACTION_TOKEN }}"

# You should see your .env config in 'env'
- name: "See exported values"
run: env
```
## Change action.yml
### Flat
The action.yml defines the inputs and output for your action.
Configserver:
Update the action.yml with your name, description, inputs and outputs for your action.
<pre>
(main branch)
|_ dev.env
|_ staging.env
|_ prod.env
</pre>
See the [documentation](https://help.github.com/en/articles/metadata-syntax-for-github-actions)
Github Action:
## Change the Code
```yaml
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: "Checkout"
id: checkout
uses: actions/checkout@v2

- name: "Launch action"
uses: Steph0/dotenv-configserver
with:
repository: "Steph0/test-configserver"
token: "${{ secrets.ACTION_TOKEN }}"
# Will look for 'prod.env'
profile: "prod"

# You should see your .env config in 'env'
- name: "See exported values"
run: env
```
Most toolkit and CI/CD operations involve async operations so the action is run in an async function.
### Env per branch
```javascript
const core = require('@actions/core');
...
Configserver:
<pre>
(main branch)
|_ .env
async function run() {
try {
...
}
catch (error) {
core.setFailed(error.message);
}
}
(dev branch)
|_ .env
</pre>
run()
Github Action:
```yaml
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: "Checkout"
id: checkout
uses: actions/checkout@v2

- name: "Launch action"
uses: Steph0/dotenv-configserver
with:
repository: "Steph0/test-configserver"
token: "${{ secrets.ACTION_TOKEN }}"
# Will checkout 'dev' branch
branch: "dev"

# You should see your .env config in 'env'
- name: "See exported values"
run: env
```
See the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages) for the various packages.
## Package for distribution
### Nested directories
GitHub Actions will run the entry point from the action.yml. Packaging assembles the code into one file that can be checked in to Git, enabling fast and reliable execution and preventing the need to check in node_modules.
Configserver:
<pre>
(main branch)
|_ front
|_ application-prod.env
|_ backend
|_ application-prod.env
Actions are run from GitHub repos. Packaging the action will create a packaged action in the dist folder.
(dev branch)
|_ front
|_ application-dev.env
|_ backend
|_ application-dev.env
</pre>
Run prepare
Github Action:
```bash
npm run prepare
```yaml
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: "Checkout"
id: checkout
uses: actions/checkout@v2

- name: "Launch action"
uses: Steph0/dotenv-configserver
with:
repository: "Steph0/test-configserver"
token: "${{ secrets.ACTION_TOKEN }}"
# Will checkout 'dev' branch
branch: "dev"
# Look for backend conf
directory: backend
# Override default filename
filename: "application" # or `application.env`
# Insert profile in filename (eg: application-dev.env)
profile: "dev"

# You should see your .env config in 'env'
- name: "See exported values"
run: env
```
Since the packaged index.js is run from the dist folder.
## Development
Install
```bash
git add dist
# Install dependencies
npm install
# Build action
npm run all
```

## Create a release branch

Users shouldn't consume the action from master since that would be latest code and actions can break compatibility between major versions.

Checkin to the v1 release branch
You can test actions locally using [ACT](https://github.com/nektos/act).
Example:

```bash
git checkout -b v1
git commit -a -m "v1 release"
# 'test.secrets' is a local file (automatically ignored) containing secrets like your Github PAT
npm run prepare && \
act workflow_dispatch -e ./.github/workflows/act-test.event -b --secret-file ./.github/workflows/test.secrets
```

```bash
git push origin v1
```
## Inspiration

Note: We recommend using the `--license` option for ncc, which will create a license file for all of the production node modules used in your project.
This project took great inspiration from:

Your action is now published! :rocket:
* [xom9ikk/dotenv](https://github.com/xom9ikk/dotenv)
* [actions/checkout](https://github.com/actions/checkout)
* [spring-cloud-config](https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.2.RELEASE/#_locating_remote_configuration_resources)

See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)
Heavily based also on the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages).

## Usage
## Contribute

You can now consume the action by referencing the v1 branch
Feel free to ask for support or features requests in the Issues tab of the project repository.

```yaml
uses: actions/javascript-action@v1
with:
milliseconds: 1000
```
Contributions (bug fixes, new features) are welcomed!

See the [actions tab](https://github.com/actions/javascript-action/actions) for runs of this action! :rocket:
License: MIT
Loading

0 comments on commit f04de18

Please sign in to comment.