Skip to content

Commit

Permalink
Change PR deployment to build artifact + deploy (#1576)
Browse files Browse the repository at this point in the history
* Add build step; check github context

* Fix build script

* Fix build script

* Add deployment script

* Remove naming check

* Update token

* Update pathing

* Update comments

* Fix comment

* Add title

* Fix comment

* Update clean script

* Include ref in artifact

* Update name

* Move envfile to vars

Co-authored-by: eeeqeee <103794572+eeeqeee@users.noreply.github.com>

* Update new-release.yml to use var envfile

---------

Co-authored-by: eeeqeee <103794572+eeeqeee@users.noreply.github.com>
  • Loading branch information
nguyentvan7 and eeeqeee authored Feb 29, 2024
1 parent 7ca0025 commit af59815
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 125 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/beta-go-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ concurrency:
cancel-in-progress: true

jobs:
call-deploy-frontend:
uses: ./.github/workflows/deploy-frontend.yml
call-build-frontend:
uses: ./.github/workflows/build-frontend.yml
with:
frontend_name: 'frontend'
repo_full_name: ${{ github.repository }}
ref: ${{ github.ref }}
deployment_name: 'beta'
pr_repo: ${{ vars.PR_REPO }}
show_dev_components: true
secrets: inherit
5 changes: 2 additions & 3 deletions .github/workflows/beta-sro-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ concurrency:
cancel-in-progress: true

jobs:
call-deploy-frontend:
uses: ./.github/workflows/deploy-frontend.yml
call-build-frontend:
uses: ./.github/workflows/build-frontend.yml
with:
frontend_name: 'sr-frontend'
repo_full_name: ${{ github.repository }}
ref: ${{ github.ref }}
deployment_name: 'beta'
pr_repo: ${{ vars.PR_REPO }}
show_dev_components: true
secrets: inherit
67 changes: 67 additions & 0 deletions .github/workflows/build-frontend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Build Frontend
run-name: Build frontend of ${{ inputs.frontend_name }} for ${{ inputs.deployment_name }} - ${{ github.event.inputs.ref }}

on:
workflow_call:
inputs:
frontend_name:
description: 'Frontend type to build. Can be `frontend`, or `sr-frontend`'
type: string
repo_full_name:
description: 'Full repository name to build from.'
type: string
default: 'frzyc/genshin-optimizer'
ref:
description: 'Ref to build from. Can be a commit hash.'
type: string
default: 'master'
deployment_name:
description: 'Name for the deployment. This will determine the URL. If you choose a name that is already deployed, it will overwrite that deployment.'
type: string
default: 'master'
show_dev_components:
description: 'Flag to show components typically only shown in development mode, for experimental or in-progress features.'
type: boolean
default: false

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout
uses: actions/checkout@v4
with:
repository: ${{ inputs.repo_full_name }}
ref: ${{ inputs.ref }}
submodules: true
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'yarn'
- name: Yarn install
run: |
yarn install --immutable --immutable-cache
- name: Setup .env
run: |
printf '%s' "$ENVFILE" > apps/frontend/.env.local
env:
ENVFILE: ${{ vars.ENVFILE }}
- name: Build genshin-optimizer
run: |
NX_URL_GITHUB_GO_CURRENT_VERSION="https://github.com/${{ inputs.repo_full_name }}/commit/$(git rev-parse HEAD)" \
NX_DAEMON="false" \
NX_SHOW_DEV_COMPONENTS="${{inputs.show_dev_components}}" \
yarn run nx run ${{inputs.frontend_name}}:build-vite:production --base-href=""
echo ${{inputs.frontend_name}} > frontend_name
echo ${{inputs.deployment_name}} > deployment_name
echo $(git rev-parse HEAD) > ref
- name: Archive build
uses: actions/upload-artifact@v4
with:
name: frontend_build
path: |
dist/apps/${{inputs.frontend_name}}
frontend_name
deployment_name
ref
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
name: Deploy GO (WR) PR
run-name: Deploy GO (WR) for ${{ github.event.number }} - ${{ github.event.pull_request.title }}
name: Build GO (WR) PR
run-name: Build GO (WR) for ${{ github.event.number }} - ${{ github.event.pull_request.title }}

permissions:
actions: read
contents: read
issues: write
pull-requests: write

on:
pull_request:
Expand All @@ -26,15 +24,12 @@ on:
types: [opened, reopened, synchronize, labeled]

jobs:
call-deploy-frontend:
uses: ./.github/workflows/deploy-frontend.yml
# Forks don't have permission to deploy
if: ${{ github.event.pull_request.head.repo.full_name == github.repository }}
call-build-frontend:
uses: ./.github/workflows/build-frontend.yml
with:
frontend_name: 'frontend'
repo_full_name: ${{ github.event.pull_request.head.repo.full_name }}
ref: ''
deployment_name: ${{ github.event.number }}
pr_repo: ${{ vars.PR_REPO }}
show_dev_components: ${{ contains(github.event.pull_request.labels.*.name, 'showDevComponents') }}
secrets: inherit
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
name: Deploy SRO PR
run-name: Deploy SRO for ${{ github.event.number }} - ${{ github.event.pull_request.title }}
name: Build SRO PR
run-name: Build SRO for ${{ github.event.number }} - ${{ github.event.pull_request.title }}

permissions:
actions: read
contents: read
issues: write
pull-requests: write

on:
pull_request:
Expand All @@ -18,15 +16,12 @@ on:
types: [opened, reopened, synchronize, labeled]

jobs:
call-deploy-frontend:
uses: ./.github/workflows/deploy-frontend.yml
# Forks don't have permission to deploy
if: ${{ github.event.pull_request.head.repo.full_name == github.repository }}
call-build-frontend:
uses: ./.github/workflows/build-frontend.yml
with:
frontend_name: 'sr-frontend'
repo_full_name: ${{ github.event.pull_request.head.repo.full_name }}
ref: ''
deployment_name: ${{ github.event.number }}
pr_repo: ${{ vars.PR_REPO }}
show_dev_components: ${{ contains(github.event.pull_request.labels.*.name, 'showDevComponents') }}
secrets: inherit
7 changes: 2 additions & 5 deletions .github/workflows/clean-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ name: Clean PR Deployment
run-name: Clean deployment ${{ github.event.number }}${{ github.event.inputs.deployment_name }} - ${{ github.event.pull_request.title }}

on:
pull_request:
pull_request_target:
types: [closed]
workflow_dispatch:
inputs:
deployment_name:
description: 'Name for the deployment. This will determine the URL. If you choose a name that is already deployed, it will overwrite that deployment.'
description: 'Name for the deployment'
type: string
default: 'master'

jobs:
deploy:
runs-on: ubuntu-latest
timeout-minutes: 15
if: |
(github.event_name == 'workflow_dispatch')
|| (github.event.pull_request.head.repo.full_name == github.repository)
steps:
- name: Determine build information
id: build-info
Expand Down
123 changes: 28 additions & 95 deletions .github/workflows/deploy-frontend.yml
Original file line number Diff line number Diff line change
@@ -1,117 +1,49 @@
name: Deploy Frontend
run-name: Deploy frontend of ${{ inputs.frontend_name }} for ${{ inputs.deployment_name }} - ${{ github.event.inputs.ref }}
name: Deploy frontend from artifact
run-name: Deploy frontend from artifact created by "${{ github.event.workflow_run.display_title }}"

on:
workflow_dispatch:
inputs:
frontend_name:
description: 'Frontend type to build. Can be `frontend`, or `sr-frontend`'
type: string
repo_full_name:
description: 'Full repository name to build from.'
type: string
default: 'frzyc/genshin-optimizer'
ref:
description: 'Ref to build from. Can be a commit hash.'
type: string
default: 'master'
deployment_name:
description: 'Name for the deployment. This will determine the URL. If you choose a name that is already deployed, it will overwrite that deployment.'
type: string
default: 'master'
pr_repo:
description: 'Name of the repo to create the deployment on'
type: string
show_dev_components:
description: 'Flag to show components typically only shown in development mode, for experimental or in-progress features.'
type: boolean
default: false
workflow_call:
inputs:
frontend_name:
description: 'Frontend type to build. Can be `frontend`, or `sr-frontend`'
type: string
repo_full_name:
description: 'Full repository name to build from.'
type: string
default: 'frzyc/genshin-optimizer'
ref:
description: 'Ref to build from. Can be a commit hash.'
type: string
default: 'master'
deployment_name:
description: 'Name for the deployment. This will determine the URL. If you choose a name that is already deployed, it will overwrite that deployment.'
type: string
default: 'master'
pr_repo:
description: 'Name of the repo to create the deployment on'
type: string
show_dev_components:
description: 'Flag to show components typically only shown in development mode, for experimental or in-progress features.'
type: boolean
default: false
workflow_run:
workflows:
- Build GO (WR) PR
- Build SRO PR
types:
- completed

jobs:
deploy:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout
uses: actions/checkout@v4
with:
repository: ${{ inputs.repo_full_name }}
ref: ${{ inputs.ref }}
submodules: true
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'yarn'
- name: Yarn install
run: |
yarn install --immutable --immutable-cache
- name: Setup .env
run: |
printf '%s' "$ENVFILE" > apps/frontend/.env.local
env:
ENVFILE: ${{ secrets.ENVFILE }}
- name: Output date
id: output-date
run: echo "date=$(date -u)" >> $GITHUB_OUTPUT
- name: Output ref
id: output-ref
run: echo "ref=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Maintain comment
uses: actions-cool/maintain-one-comment@v3
- name: Download build artifact
uses: actions/download-artifact@v4
with:
body: |
[${{ inputs.frontend_name }}] [${{ steps.output-date.outputs.date }}] - Building version ${{ steps.output-ref.outputs.ref }}
body-include: '<!-- Created by actions-cool/maintain-one-comment -->'
update-mode: 'append'
- name: Build genshin-optimizer
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup variables
id: vars
run: |
NX_URL_GITHUB_GO_CURRENT_VERSION="https://github.com/${{ inputs.repo_full_name }}/commit/$(git rev-parse HEAD)" \
NX_DAEMON="false" \
NX_SHOW_DEV_COMPONENTS="${{inputs.show_dev_components}}" \
yarn run nx run ${{inputs.frontend_name}}:build-vite:production --base-href=""
echo "deployment_name=$(cat frontend_build/deployment_name)" >> $GITHUB_OUTPUT
echo "frontend_name=$(cat frontend_build/frontend_name)" >> $GITHUB_OUTPUT
echo "ref=$(cat frontend_build/ref)" >> $GITHUB_OUTPUT
- name: Checkout external gh-pages
uses: actions/checkout@v4
with:
repository: ${{ inputs.pr_repo }}
repository: ${{ vars.pr_repo }}
ssh-key: ${{ secrets.PR_REPO_SSH_KEY }}
ref: gh-pages
path: gh-pages
- name: Move to external gh-pages
run: |
mkdir --parents "gh-pages/${{ inputs.deployment_name }}/${{ inputs.frontend_name }}"
rm -rfv "gh-pages/${{ inputs.deployment_name }}/${{ inputs.frontend_name }}" || true
mv "dist/apps/${{ inputs.frontend_name }}" "gh-pages/${{ inputs.deployment_name }}/${{ inputs.frontend_name }}"
mkdir --parents "gh-pages/${{ steps.vars.outputs.deployment_name }}/${{ steps.vars.outputs.frontend_name }}"
rm -rfv "gh-pages/${{ steps.vars.outputs.deployment_name }}/${{ steps.vars.outputs.frontend_name }}" || true
mv "frontend_build/dist/apps/${{ steps.vars.outputs.frontend_name }}" "gh-pages/${{ steps.vars.outputs.deployment_name }}/${{ steps.vars.outputs.frontend_name }}"
- name: Make commit to deploy
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config pull.rebase false
git add ./${{ inputs.deployment_name }}/${{ inputs.frontend_name }}
git commit --allow-empty -m "build ${{ inputs.deployment_name }}/${{ inputs.frontend_name }} ${{ inputs.repo_full_name }}@$(cd .. && git rev-parse HEAD)"
git add ./${{ steps.vars.outputs.deployment_name }}/${{ steps.vars.outputs.frontend_name }}
git commit --allow-empty -m "build ${{ steps.vars.outputs.deployment_name }}/${{ steps.vars.outputs.frontend_name }} ${{ steps.vars.outputs.ref }}"
working-directory: gh-pages
- name: Push commit
uses: nick-fields/retry@v2
Expand All @@ -126,16 +58,17 @@ jobs:
id: export-url
# First line splits the full repo name (frzyc/genshin-optimizer) into $account (frzyc) and $repo (genshin-optimizer)
run: |
IFS=/ read -r account repo <<< ${{ inputs.pr_repo }}
echo "url=Deployed $(git rev-parse HEAD) to https://$account.github.io/$repo/${{ inputs.deployment_name }}/${{ inputs.frontend_name }} (Takes 3-5 minutes after this completes to be available)" >> $GITHUB_OUTPUT
echo "Deployed $(git rev-parse HEAD) to https://$account.github.io/$repo/${{ inputs.deployment_name }}/${{ inputs.frontend_name }} (Takes 3-5 minutes after this completes to be available)"
IFS=/ read -r account repo <<< ${{ vars.pr_repo }}
echo "url=Deployed ${{ steps.vars.outputs.ref }} to https://$account.github.io/$repo/${{ steps.vars.outputs.deployment_name }}/${{ steps.vars.outputs.frontend_name }} (Takes 3-5 minutes after this completes to be available)" >> $GITHUB_OUTPUT
- name: Output date
id: output-date2
run: echo "date=$(date -u)" >> $GITHUB_OUTPUT
- name: Maintain comment
uses: actions-cool/maintain-one-comment@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
number: ${{ steps.vars.outputs.deployment_name }}
body: |
[${{ inputs.frontend_name }}] [${{ steps.output-date2.outputs.date }}] - ${{ steps.export-url.outputs.url }}
[${{ steps.vars.outputs.frontend_name }}] [${{ steps.output-date2.outputs.date }}] - ${{ steps.export-url.outputs.url }}
body-include: '<!-- Created by actions-cool/maintain-one-comment -->'
update-mode: 'append'
2 changes: 1 addition & 1 deletion .github/workflows/new-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
run: |
printf '%s' "$ENVFILE" > apps/frontend/.env.local
env:
ENVFILE: ${{ secrets.ENVFILE }}
ENVFILE: ${{ vars.ENVFILE }}
- name: Build genshin-optimizer
run: |
NX_URL_GITHUB_GO_CURRENT_VERSION="https://github.com/${{ github.repository }}/commit/$(git rev-parse HEAD)" \
Expand Down

0 comments on commit af59815

Please sign in to comment.