Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
chvmvd committed Mar 31, 2023
1 parent 0861c82 commit f84d155
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![license](https://img.shields.io/badge/license-MIT-informational.svg)](LICENSE)
![PRs](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)

GitHub Action that automatically builds your project, deploys to GitHub Pages and creates a PR preview.
GitHub Action that automatically builds your project, deploys it to GitHub Pages and creates a PR preview.

## Table of Contents

Expand All @@ -19,7 +19,7 @@ GitHub Action that automatically builds your project, deploys to GitHub Pages an

## About

This action will automatically build your project, deploy to GitHub Pages and create a PR preview.
This action will automatically build your project, deploy it to GitHub Pages and also create a PR preview.

## Usage

Expand Down Expand Up @@ -63,17 +63,17 @@ Second, you need to ensure that your GitHub repository is configured to use GitH

The following configuration options are available:

| Name | Description | Required | Default |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------ | -------- | ------------------------------------ |
| `type` | The type of project you want to build(currently only `vite` is supported). | `true` | |
| `rootDir` | The root directory of your project. | `false` | `.` |
| `folder` | The folder that contains the built files(If set to `auto`, it will use the default folder for the type of the project.). | `false` | `auto` |
| `pr-preview` | Whether to create a PR preview or not. | `false` | `true` |
| `production-branch` | The branch that contains the production code. | `false` | the default branch of the repository |
| `development-branch` | The branch that contains the development code.(This should contain each pattern as a separate line.) | `false` | |
| `deployment-branch` | The branch that the project will be deployed to. | `false` | `gh-pages` |
| `umbrella-dir` | The directory that will contain all PR previews. | `false` | `pr-preview` |
| `custom-url` | The custom URL to use. | `false` | `""` |
| Name | Description | Required | Default |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------------------ |
| `type` | The type of your project. (Other settings will be automatically set for the type of the project. Currently only `vite` is supported.) | `true` | |
| `rootDir` | The root directory of your project. (Default is the current directory.) | `false` | `.` |
| `folder` | The folder that contains the built files. (If set to `auto`, it will use the default folder for the type of the project.) | `false` | `auto` |
| `pr-preview` | Whether to create a PR preview or not. | `false` | `true` |
| `production-branch` | The branch that contains the production code. (Default is `main` or `master`) | `false` | `main` or `master` |
| `development-branch` | The branch that contains the development code. It will be deployed to `https://<owner>.github.io/<repo>/<branch>`. (This can be multiple branches. If you want to specify multiple branches, separate them with a newline.) | `false` | |
| `deployment-branch` | The branch that the project will be deployed to. | `false` | `gh-pages` |
| `umbrella-dir` | The directory that will contain all PR previews. | `false` | `pr-preview` |
| `custom-url` | The custom URL to deploy. | `false` | |

## License

Expand Down
43 changes: 18 additions & 25 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
---
name: Build, Deploy to GitHub Pages and Deploy PR Preview
author: WATAHIKI Yuto
description: Build, Deploy to GitHub Pages and Deploy PR Preview
description: This action builds a project and deploys it to GitHub Pages and also deploys PR preview automatically.
inputs:
type:
description: Type of the project(`vite` is the only supported type for now)
description: Type of the project(Other settings will be automatically set for the type of the project. Currently only `vite` is supported.)
required: true
rootDir:
description: Root directory of the project
description: Root directory of the project(Default is the current directory)
required: false
default: .
folder:
description: Folder to deploy(If set to `auto`, it will use the default folder for the type of the project.)
description: Folder that contains the built files(If set to `auto`, it will use the default folder for the type of the project.)
required: false
default: auto
pr-preview:
Expand All @@ -22,21 +22,19 @@ inputs:
description: Branch that contains the production code(Default is `main` or `master`)
required: false
development-branch:
description: Branch that contains the development code
description: Branch that contains the development code(This can be multiple branches. If you want to specify multiple branches, separate them with a newline.)
required: false
default: ""
deployment-branch:
description: Branch that the project will be deployed to
required: false
default: gh-pages
umbrella-dir:
description: Directory containing all previews
description: Directory that will contain all previews
required: false
default: pr-preview
custom-url:
description: Custom URL to deploy
required: false
default: ""

runs:
using: composite
Expand Down Expand Up @@ -74,39 +72,34 @@ runs:
}
result-encoding: string

- name: Make BASE_URL
- name: Determine the base url
id: base-url
uses: actions/github-script@v6
env:
DEVELOPMENT_BRANCHES: ${{ inputs.development-branch }}
with:
script: |
const githubEventName = "${{ github.event_name }}";
const githubRepository = "${{ github.repository }}"; // owner/repo
const githubRepositoryOwner = "${{ github.repository_owner }}"; // owner
const githubRepositoryName = githubRepository.slice(githubRepositoryOwner.length + 1); // repo
let productionBaseUrl = "";
if (githubRepositoryName !== `${githubRepositoryOwner}.github.io` && "${{ inputs.custom-url }}" === "") {
productionBaseUrl = `/${githubRepositoryName}`; // /repo
}
const githubRepositoryName = "${{ github.event.repository.name }}"; // repo
const repositoryBaseUrl = githubRepositoryName === `${githubRepositoryOwner}.github.io` || "${{ inputs.custom-url }}" !== "" ? "" : `/${githubRepositoryName}`; // "" or /<repo>
if (githubEventName === "push") {
const githubBranchName = "${{ github.ref_name }}"; // branch
const developmentBranches = process.env.DEVELOPMENT_BRANCHES.split("\n");
// base URL of the production site
if (githubEventName === "push" && githubBranchName === "${{ steps.production-branch.outputs.result }}") {
const baseUrl = productionBaseUrl;
return baseUrl; // /repo
if (githubBranchName === "${{ steps.production-branch.outputs.result }}") {
const baseUrl = repositoryBaseUrl;
return baseUrl; // /<repo>
}
// base URL of the development site
else if (githubEventName === "push" && developmentBranches.includes(githubBranchName)) {
const baseUrl = `${productionBaseUrl}/${githubBranchName}`;
return baseUrl; // /repo/branch
else if (developmentBranches.includes(githubBranchName)) {
const baseUrl = `${repositoryBaseUrl}/${githubBranchName}`;
return baseUrl; // /<repo>/<branch>
}
} else if (githubEventName === "pull_request") {
const refName = "${{ github.ref_name }}"; // PR_NUMBER/merge
const prNumber = refName.slice(0, refName.length - 6); // PR_NUMBER
const baseUrl = `${productionBaseUrl}/pr-preview/pr-${prNumber}`;
return baseUrl; // /repo/pr-preview/pr-PR_NUMBER
const prNumber = "${{ github.event.number }}"; // pr-number
const baseUrl = `${repositoryBaseUrl}/pr-preview/pr-${prNumber}`;
return baseUrl; // /<repo>/pr-preview/<pr-number>
}
result-encoding: string

Expand Down

0 comments on commit f84d155

Please sign in to comment.