Skip to content

Commit

Permalink
Merge pull request #3 from enriikke/migrate-to-new-syntax
Browse files Browse the repository at this point in the history
Migrate to JavaScript action
  • Loading branch information
enriikke authored Sep 12, 2019
2 parents 6f11d33 + 0a1e833 commit c9d9bca
Show file tree
Hide file tree
Showing 395 changed files with 78,727 additions and 155 deletions.
18 changes: 0 additions & 18 deletions Dockerfile

This file was deleted.

128 changes: 57 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,58 @@ GitHub Action to build and deploy your Gatsby site to GitHub Pages ❤️🎩
This GitHub Action will run `gatsby build` at the root of your repository and
deploy it to GitHub Pages for you! Here's a basic workflow example:

```workflow
workflow "Gatsby to GitHub Pages" {
on = "push"
resolves = ["Publish"]
}
action "Publish" {
uses = "enriikke/gatsby-gh-pages-action@master"
secrets = ["ACCESS_TOKEN"]
}
```

It's recommended to use this Action combined with the [Filters Action](https://github.com/actions/bin/tree/c6471707d308175c57dfe91963406ef205837dbd/filter)
to specify only the branch(es) you want to trigger a build.

```workflow
workflow "Gatsby to GitHub Pages" {
on = "push"
resolves = ["Publish"]
}
action "On Dev" {
uses = "actions/bin/filter@master"
args = "branch dev"
}
action "Publish" {
uses = "enriikke/gatsby-gh-pages-action@@1.0.1"
needs = ["On Dev"]
secrets = ["ACCESS_TOKEN"]
}
```yml
name: Gatsby Publish

on:
push:
branches:
- dev

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: enriikke/gatsby-gh-pages-action@v2
with:
access-token: ${{ secrets.ACCESS_TOKEN }}
```
### Knobs & Handles
This Action is fairly simple but it does provide you with a couple of
configuration options:
- **DEPLOY_BRANCH**: The branch expected by GitHub to have the static files
- **access-token**: A [GitHub Personal Access Token][github-access-token] with
the `repo` scope. This is **required** to push the site to your repo after
Gatsby finish building it. You should store this as a [secret][github-repo-secret]
in your repository. Provided as an [input][github-action-input].

- **deploy-branch**: The branch expected by GitHub to have the static files
needed for your site. For org and user pages it should always be `master`.
This is where the output of `gatsby build` will be pushed to. Defined as an
[environment variable](https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/#environment-variables).
This is where the output of `gatsby build` will be pushed to. Provided as an
[input][github-action-input].
Defaults to `master`.

- **ARGS**: Additional arguments that get passed to `gatsby build`. See the
[Gatsby documentation](https://www.gatsbyjs.org/docs/gatsby-cli/#build) for a
list of allowed options. Given as [workflow args](https://developer.github.com/actions/creating-github-actions/creating-a-docker-container/#cmd).
- **gatsby-args**: Additional arguments that get passed to `gatsby build`. See the
[Gatsby documentation][gatsby-build-docs] for a list of allowed options.
Provided as an [input][github-action-input].
Defaults to nothing.

### Org or User Pages

Create a repository with the format `<YOUR/ORG USERNAME>.github.io`, push your
Gatsby source code to a branch different than your `DEPLOY_BRANCH` and add this
GitHub Action to your workflow! 🚀😃
Gatsby source code to a branch other than `master` and add this GitHub Action to
your workflow! 🚀😃

### Repository Pages

Repo pages work a little different because the URL includes a trailing path with
the repository name, like `https://username.github.io/reponame/`. You need to
tell Gatsby what the path prefix is via `gatsby-config.js`:
Repo pages give you the option to push your static site to either `master` or
`gh-pages` branches. They also work a little different because the URL includes
a trailing path with the repository name, like
`https://username.github.io/reponame/`. You need to tell Gatsby what the path
prefix is via `gatsby-config.js`:

```js
module.exports = {
Expand All @@ -77,44 +69,38 @@ module.exports = {
Additionally, you need to tell the `gatsby build` command to use it by passing
the `--prefix-paths` as an argument. Here's an example workflow for that:

```workflow
workflow "Gatsby to GitHub Pages" {
on = "push"
resolves = ["Publish"]
}
action "Publish" {
uses = "enriikke/gatsby-gh-pages-action@master"
args = "--prefix-paths"
secrets = ["ACCESS_TOKEN"]
}
```yml
name: Gatsby Publish
on:
push:
branches:
- dev
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: enriikke/gatsby-gh-pages-action@v2
with:
access-token: ${{ secrets.ACCESS_TOKEN }}
deploy-branch: gh-pages
gatsby-args: --prefix-paths
```

🤩 Note that **NON** of this is necessary if you are using custom domains.🤩

## Requirements

A [GitHub Personal Access Token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/)
with the `repo` scope is needed to run this Action. This is important! This is
**NOT** the same as the `GITHUB_TOKEN` that can be included as part of an Action.

Just as important is that this personal access token needs to be provided as a
[secret](https://developer.github.com/actions/creating-workflows/storing-secrets/)
with the name `ACCESS_TOKEN`, **NOT** as an environment variable. The reason
being that secrets get encrypted while environment variables do **NOT**.

Sorry for being so _negative_ just now ☝️. I just want to make sure nobody
accidentally exposes any sensitive information. Let's keep access tokens safe! 😉😇

### Assumptions

This Action assumes that your Gatsby code sits at the root of your repository
and `gatsby build` outputs to the `public` directory. As of this writing, Gatsby
doesn't provide a way to customize the build directory so this should be a safe
assumption.


## That's It

Have fun building!
Have fun building!

[gatsby-build-docs]: https://www.gatsbyjs.org/docs/gatsby-cli/#build
[github-access-token]: https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line
[github-action-input]: https://help.github.com/en/articles/workflow-syntax-for-github-actions#jobsjob_idstepswith
[github-repo-secret]: https://help.github.com/en/articles/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables
20 changes: 20 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Gatsby Publish"
description: "Build and deploy your Gatsby site to GitHub Pages."
branding:
icon: "book-open"
color: "purple"
inputs:
access-token:
description: "A personal access token needed to push your site after it has been built."
required: true
deploy-branch:
description: "The branch expected by GitHub to have the static files needed for your site."
required: false
default: "master"
gatsby-args:
description: "Additional arguments that get passed to `gatsby build`."
required: false
default: ""
runs:
using: "node12"
main: "index.js"
66 changes: 0 additions & 66 deletions entrypoint

This file was deleted.

71 changes: 71 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const core = require("@actions/core")
const github = require("@actions/github")
const ioUtil = require("@actions/io/lib/io-util")
const exec = require("@actions/exec")

async function run() {
try {
const accessToken = core.getInput("access-token")
if (!accessToken) {
core.setFailed(
"No personal access token found. Please provide one by setting the `access-token` input for this action."
)
return
}

const deployBranch = core.getInput("deploy-branch")
if (!deployBranch) deployBranch = "master"

if (github.context.ref === `refs/heads/${deployBranch}`) {
console.log(`Triggered by branch used to deploy: ${github.context.ref}.`)
console.log("Nothing to deploy.")
return
}

const pkgManager = (await ioUtil.exists("./yarn.lock")) ? "yarn" : "npm"
console.log(`Installing your site's dependencies using ${pkgManager}.`)
await exec.exec(`${pkgManager} install`)
console.log("Finished installing dependencies.")

const gatsbyArgs = core.getInput("gatsby-args")
console.log("Ready to build your Gatsby site!")
console.log(`Building with: ${pkgManager} run gatsby build ${gatsbyArgs}`)
await exec.exec(`${pkgManager} run gatsby build`, [gatsbyArgs])
console.log("Finished buidling your site.")

// TODO: copy CNAME to ./public

const repo = `${github.context.repo.owner}/${github.context.repo.repo}`
const repoURL = `https://${accessToken}@github.com/${repo}.git`
console.log("Ready to deploy your new shiny site!")
console.log(`Deploying to repo: ${repo} and branch: ${deployBranch}`)
console.log(
"You can configure the deploy branch by setting the `deploy-branch` input for this action."
)
await exec.exec(`git init`, [], { cwd: "./public" })
await exec.exec(`git config user.name`, [github.context.actor], {
cwd: "./public",
})
await exec.exec(
`git config user.email`,
[`${github.context.actor}@users.noreply.github.com`],
{ cwd: "./public" }
)
await exec.exec(`git add`, ["."], { cwd: "./public" })
await exec.exec(
`git commit`,
["-m", `deployed via Gatsby Publish Action 🎩 for ${github.context.sha}`],
{ cwd: "./public" }
)
await exec.exec(`git push`, ["-f", repoURL, `master:${deployBranch}`], {
cwd: "./public",
})
console.log("Finished deploying your site.")

console.log("Enjoy! ✨")
} catch (error) {
core.setFailed(error.message)
}
}

run()
1 change: 1 addition & 0 deletions node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/which

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions node_modules/@actions/core/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c9d9bca

Please sign in to comment.