Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Henson, Garth authored and Henson, Garth committed Feb 4, 2019
0 parents commit 09ff3f9
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM node:10-alpine

LABEL version="1.0.0"
LABEL repository="http://github.com/guahanweb/actions-test"
LABEL homepage="http://github.com/actions"
LABEL maintainer="Garth Henson <garth@guahanweb.com>"

LABEL com.github.actions.name="Node App Helper Actions"
LABEL com.github.actions.description="Provides some helper scripts to aid in basic Node.js app delivery"
LABEL com.github.actions.icon="settings"
LABEL com.github.actions.color="green"

# install additional dependencies
RUN apk --update add git openssh bash && \
rm -rf /var/lib/apt/lists/* && \
rm /var/cache/apk/*

# COPY LICENSE README.md /

COPY "helpers.sh" "/helpers.sh"
COPY "entrypoint.sh" "/entrypoint.sh"

ENTRYPOINT ["/entrypoint.sh"]
CMD ["help"]
136 changes: 136 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Node.js Actions

This action is designed to help easily proxy any Node.js scripts into GH Actions to create workflows easily. By design, there are only a few predefined paths in this action, but the `run` path is flexible enough to build any workflow you may design.

The `entrypoint.sh` script takes in arguments to define what path to follow. There are currently two primary predefined tasks.

## Tasks

Each task is essentially a sub-task to the main entrypoint and will execute a very specific operation.

### `run`

The primary task is `run`. This simply executes the provided script within your NPM or yarn context. By configuring your action definition, you can use the `run` task to chain any number of operations within your system.

For example, if you had an NPM script titled `build` that would create your artifacts for deployment, you could set up your action like this:

```
action "Build" {
uses = "guahanweb/actions/node-app@master"
args = "run build"
}
```

**NOTE:** if you are using Yarn, provide an `env` definition specifying the `PKG_MANAGER` to be Yarn:

```
env = {
PKG_MANAGER = "yarn"
}
```

Under the covers, this is essentially going to run `npm run build` or `yarn build` on the worker machine. Obviously, this can be expanded to run any actions you may need.

**Linting example:**

```
action "Lint" {
uses = "guahanweb/actions/node-app@master"
env = {
PKG_MANAGER = "yarn"
}
args = "run lint"
}
```

#### Environment Variables

Several environment variables are availabe to customize the behavior of your actions.

<dl>
<dt><code>NODE_ENV</code></dt>
<dd>The environment in which to execute your node application.<br>
<i>defaults to <code>production</code></i></dd>
<dt><code>PKG_MANAGER</code></dt>
<dd>Specifies which package manager you are using. Valid values are <code>npm</code> and <code>yarn</code>.<br>
<i>defaults to <code>npm</code></i></dd>
</dl>

### `gh-pages`

This action also offers the ability to easily publish to an orphan branch on your repository. This is generally used for publishing static content to a `gh-pages` branch to be hosted by GitHub, but you can customize it to any branching strategy you desire.

This task requires the `GITHUB_TOKEN` secret from your action, or it will not have the appropriate permissions to push to your repo.

#### Example

```
action "GH Pages" {
uses = "guahanweb/actions/node-app@master"
secrets = ["GITHUB_TOKEN"]
args = "gh-pages"
}
```

#### Environment Variables

Since every application is different, there are a few overrides you can provide within your `env` block to customize behavior.

<dl>
<dt><code>GITHUB_TOKEN</code></dt>
<dd>This token is provided by GitHub when you specify it in your secrets array. It is required for the operation to have permission to push to your repository.</dd>
<dt><code>BUILD_DIR</code></dt>
<dd>Specifies the directory that contains the static assets to be pushed.<br>
<i>defaults to <code>build</code></i></dd>
<dt><code>REMOTE_BRANCH</code></dt>
<dd>Spcifies the branch name to which the contents of <code>$BUILD_DIR</code> should be pushed.<br>
<i>defaults to <code>gh-pages</code></i></dd>
</dl>

### `test`

The `test` operation is provided as an alias to `run test`.

## Putting it all together

Once you have a handle on the options, you can create multiple actions easily and chain them together into a workflow. Here is an example of how you might specify a workflow to lint, test, build, and deploy a GH Pages website using this repository.

#### Example Workflow

```
workflow "Build and Deploy Pages" {
on = "push"
resolves = ["Lint", "Build", "Test", "Deploy Pages"]
}
action "Lint" {
uses = "guahanweb/actions/node-app@master"
env = {
PKG_MANAGER = "yarn"
}
args = "run lint"
}
action "Build" {
uses = "guahanweb/actions/node-app@master"
env = {
PKG_MANAGER = "yarn"
}
args = "run build"
}
action "Test" {
uses = "guahanweb/actions/node-app@master"
env = {
PKG_MANAGER = "yarn"
}
args = "test"
}
action "Deploy Pages" {
uses = "guahanweb/actions/node-app@master"
secrets = ["GITHUB_TOKEN"]
args = "gh-pages"
needs = ["Lint", "Build", "Test"]
}
```
39 changes: 39 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

# set up environment
export NODE_ENV=${NODE_ENV:-"production"}
export PKG_MANAGER=${PKG_MANAGER:-"npm"}

# include some helper methods
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source "$DIR/helpers.sh"


function help {
echo "The following commands can be run from this script."
echo ""
echo " - run <script>"
echo " execute the specified <script> on the package"
echo ""
}

case $1 in
# execute specified script using the specified mode
run)
runScript "$2"
;;

# shorthand for entrypoint.sh run test
test)
runScript "test"
;;

# deploy a build to a specific git branch
gh-pages)
ghActionsSetup
ghPages
;;

*)
help;;
esac
93 changes: 93 additions & 0 deletions helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# helper functions for our script handler

# This function will validate the mode of build and return
# a normalized mode
function parseMode {
if [[ ! $# -eq 1 ]]; then
exit 1
fi

if [[ "$1" =~ ^[Yy]arn$ ]]; then
echo "yarn"
exit 0
elif [[ "$1" =~ ^(npm|NPM)$ ]]; then
echo "npm"
exit 0
fi
exit 1
}

# This function will process the provided script with either
# NPM or Yarn
# usage: runScript [npm|yarn] <script>
function runScript {
script="$1"
mode=$PKG_MANAGER

if [[ "$PKG_MANAGER" =~ ^[Yy]arn$ ]]; then
mode="yarn"
fi

if [[ -z "$script" ]]; then
echo "[ERROR] No script provided to execute"
exit 1
fi

case $mode in
yarn)
echo "running: yarn $script"
npm install yarn -g > /dev/null 2>&1
yarn install > /dev/null 2>&1
yarn $script
;;

*)
# default to NPM build
echo "running: npm run $script"
npm install > /dev/null 2>&1
npm run $script
;;
esac
}

# This function checks for some of the necessary environment variables
# that are provided by GH for interaction with the repo
function ghActionsSetup {
# required variables
if [[ -z "$GITHUB_TOKEN" || -z "$GITHUB_REPOSITORY" || -z "$GITHUB_ACTOR" ]]; then
echo "[ERROR] Environment is not set up appropriately: missing GH variables"
exit 1
fi

export REMOTE_REPO="https://${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
export SCRIPT_USER="${GITHUB_ACTOR}"
export SCRIPT_EMAIL="${GITHUB_ACTOR}@users.noreply.github.com"
}

# This function will force push a $build directory to a specified
# branch in the specified repository using the actions environment
# variables
#
# Required environment variables:
# GITHUB_ACTOR
#
function ghPages {
export BUILD_DIR=${BUILD_DIR:-"build"}
export REMOTE_BRANCH=${REMOTE_BRANCH:-"gh-pages"}

# navigate into build directory
cd $BUILD_DIR

# initialize the repo, and be sure we identify as the triggering user
git init && \
git config user.name "${SCRIPT_USER}" && \
git config user.email "${SCRIPT_EMAIL}" && \
git add . && \
echo -n 'Files to Commit:' && ls -l | wc -l && \
git commit -m 'action build' > /dev/null 2>&1 && \
git push --force ${REMOTE_REPO} master:${REMOTE_BRANCH} > /dev/null 2>&1 && \
rm -rf .git

# navigate back to the previous directory
cd -
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


5 comments on commit 09ff3f9

@coalminersdaughter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great

@coalminersdaughter
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@

@5hristina5
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q

@5hristina5
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q

@5hristina5
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q

Please sign in to comment.