Skip to content

Commit 0468473

Browse files
committed
replace duplicate work with action
1 parent a81e41d commit 0468473

File tree

3 files changed

+114
-86
lines changed

3 files changed

+114
-86
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: 'Deploy Website to Amplify'
2+
description: 'Build Hugo site and deploy to AWS Amplify'
3+
inputs:
4+
aws-role-arn:
5+
description: 'AWS IAM role ARN to assume'
6+
required: true
7+
aws-region:
8+
description: 'AWS region for Amplify'
9+
required: true
10+
amplify-app-id:
11+
description: 'AWS Amplify App ID'
12+
required: true
13+
amplify-branch-name:
14+
description: 'AWS Amplify branch name'
15+
required: true
16+
s3-bucket:
17+
description: 'S3 bucket for website files'
18+
required: true
19+
s3-prefix:
20+
description: 'S3 prefix/path for website files'
21+
required: false
22+
default: ''
23+
hugo-base-url:
24+
description: 'Base URL for Hugo build'
25+
required: false
26+
default: ''
27+
preview:
28+
description: 'Whether this is a preview deployment'
29+
required: false
30+
default: 'false'
31+
runs:
32+
using: 'composite'
33+
steps:
34+
- name: Install Go
35+
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
36+
with:
37+
go-version-file: test/hack/resource/parse-redirects/go.mod
38+
check-latest: true
39+
cache-dependency-path: "test/hack/resource/go.sum"
40+
- name: Install Hugo
41+
uses: peaceiris/actions-hugo@16361eb4acea8698b220b76c0d4e84e1fd22c61d # v2.6.0
42+
with:
43+
hugo-version: '0.120.3'
44+
extended: true
45+
- name: Build Hugo site
46+
working-directory: website
47+
shell: bash
48+
env:
49+
HUGO_ENV: production
50+
HUGO_ENABLEGITINFO: true
51+
TZ: America/Los_Angeles
52+
HUGO_CACHEDIR: ${{ github.workspace }}/.hugo
53+
NPM_CONFIG_CACHE: ${{ github.workspace }}/.npm
54+
run: |
55+
npm ci --prefer-offline
56+
if [ "${{ inputs.preview }}" == "true" ]; then
57+
hugo --gc --minify --buildFuture -b "${{ inputs.hugo-base-url }}"
58+
else
59+
hugo --gc --minify
60+
fi
61+
- name: Configure AWS credentials
62+
uses: aws-actions/configure-aws-credentials@b47578312673ae6fa5b5096b330d9fbac3d116df # v4.2.1
63+
with:
64+
role-to-assume: ${{ inputs.aws-role-arn }}
65+
aws-region: ${{ inputs.aws-region }}
66+
- name: Upload to S3
67+
shell: bash
68+
run: |
69+
S3_PATH="s3://${{ inputs.s3-bucket }}/${{ inputs.s3-prefix }}"
70+
aws s3 sync website/public/ "$S3_PATH" --delete
71+
- name: Create Amplify branch (if preview and doesn't exist)
72+
shell: bash
73+
run: |
74+
if [ "${{ inputs.preview }}" == "true" ]; then
75+
if ! aws amplify get-branch --app-id ${{ inputs.amplify-app-id }} --branch-name "${{ inputs.amplify-branch-name }}" 2>/dev/null; then
76+
aws amplify create-branch \
77+
--app-id ${{ inputs.amplify-app-id }} \
78+
--branch-name "${{ inputs.amplify-branch-name }}"
79+
fi
80+
fi
81+
- name: Configure redirects
82+
shell: bash
83+
run: |
84+
REDIRECT_RULES=$(go run test/hack/resource/parse-redirects/main.go)
85+
aws amplify update-app \
86+
--app-id ${{ inputs.amplify-app-id }} \
87+
--custom-rules "$REDIRECT_RULES"
88+
- name: Deploy to Amplify
89+
shell: bash
90+
run: |
91+
SOURCE_URL="s3://${{ inputs.s3-bucket }}/${{ inputs.s3-prefix }}"
92+
aws amplify start-deployment \
93+
--app-id ${{ inputs.amplify-app-id }} \
94+
--branch-name "${{ inputs.amplify-branch-name }}" \
95+
--source-url "$SOURCE_URL" \
96+
--source-url-type BUCKET_PREFIX

.github/workflows/website-deploy.yaml

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,11 @@ jobs:
1111
contents: read
1212
steps:
1313
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
14-
- name: Install Go
15-
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
14+
- name: Deploy website
15+
uses: ./.github/actions/deploy-website
1616
with:
17-
go-version-file: test/hack/resource/parse-redirects/go.mod
18-
check-latest: true
19-
cache-dependency-path: "test/hack/resource/go.sum"
20-
- name: Install Hugo
21-
uses: peaceiris/actions-hugo@16361eb4acea8698b220b76c0d4e84e1fd22c61d # v2.6.0
22-
with:
23-
hugo-version: '0.120.3'
24-
extended: true
25-
- name: Build Hugo site
26-
working-directory: website
27-
env:
28-
HUGO_ENV: production
29-
HUGO_ENABLEGITINFO: true
30-
TZ: America/Los_Angeles
31-
HUGO_CACHEDIR: ${{ github.workspace }}/.hugo
32-
NPM_CONFIG_CACHE: ${{ github.workspace }}/.npm
33-
run: |
34-
npm ci --prefer-offline
35-
hugo --gc --minify
36-
- name: Configure AWS credentials
37-
uses: aws-actions/configure-aws-credentials@b47578312673ae6fa5b5096b330d9fbac3d116df # v4.2.1
38-
with:
39-
role-to-assume: arn:aws:iam::${{ vars.RELEASE_ACCOUNT_ID }}:role/${{ vars.WEBSITE_ROLE_NAME }}
17+
aws-role-arn: arn:aws:iam::${{ vars.RELEASE_ACCOUNT_ID }}:role/${{ vars.WEBSITE_ROLE_NAME }}
4018
aws-region: ${{ vars.AMPLIFY_REGION }}
41-
- name: Sync Hugo build to S3
42-
run: aws s3 sync website/public/ s3://${{ vars.AMPLIFY_S3_BUCKET }} --delete
43-
- name: Configure Amplify redirect rules
44-
run: |
45-
REDIRECT_RULES=$(go run test/hack/resource/parse-redirects/main.go)
46-
aws amplify update-app \
47-
--app-id ${{ vars.AMPLIFY_APP_ID }} \
48-
--custom-rules "$REDIRECT_RULES"
49-
- name: Trigger Amplify deployment from S3
50-
run: |
51-
aws amplify start-deployment \
52-
--app-id ${{ vars.AMPLIFY_APP_ID }} \
53-
--branch-name ${{ vars.BRANCH_NAME }} \
54-
--source-url s3://${{ vars.AMPLIFY_S3_BUCKET }} \
55-
--source-url-type BUCKET_PREFIX
19+
amplify-app-id: ${{ vars.AMPLIFY_APP_ID }}
20+
amplify-branch-name: ${{ vars.BRANCH_NAME }}
21+
s3-bucket: ${{ vars.AMPLIFY_S3_BUCKET }}

.github/workflows/website-preview.yaml

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Deploy Website Preview to Amplify
22
on:
33
pull_request:
4+
types: [ opened, synchronize, reopened ]
45
branches: [ main ]
56
paths: [ website/** ]
67
jobs:
@@ -21,17 +22,6 @@ jobs:
2122
echo PR_NUMBER="$pr_number"
2223
echo BRANCH_NAME="pr-$pr_number"
2324
} >> "$GITHUB_ENV"
24-
- name: Install Go
25-
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
26-
with:
27-
go-version-file: test/hack/resource/parse-redirects/go.mod
28-
check-latest: true
29-
cache-dependency-path: "test/hack/resource/go.sum"
30-
- name: Install Hugo
31-
uses: peaceiris/actions-hugo@16361eb4acea8698b220b76c0d4e84e1fd22c61d # v2.6.0
32-
with:
33-
hugo-version: '0.120.3'
34-
extended: true
3525
- name: Configure AWS credentials
3626
uses: aws-actions/configure-aws-credentials@b47578312673ae6fa5b5096b330d9fbac3d116df # v4.2.1
3727
with:
@@ -42,41 +32,17 @@ jobs:
4232
APP_DOMAIN=$(aws amplify get-app --app-id ${{ vars.AMPLIFY_APP_ID_BETA }} --query 'app.defaultDomain' --output text)
4333
PREVIEW_URL="https://${{ env.BRANCH_NAME }}.$APP_DOMAIN"
4434
echo PREVIEW_URL="$PREVIEW_URL" >> "$GITHUB_ENV"
45-
- name: Build Hugo site (deploy-preview context)
46-
working-directory: website
47-
env:
48-
HUGO_ENV: production
49-
HUGO_ENABLEGITINFO: true
50-
TZ: America/Los_Angeles
51-
HUGO_CACHEDIR: ${{ github.workspace }}/.hugo
52-
NPM_CONFIG_CACHE: ${{ github.workspace }}/.npm
53-
run: |
54-
npm ci --prefer-offline
55-
hugo --gc --minify --buildFuture -b "${{ env.PREVIEW_URL }}"
56-
- name: Upload to S3
57-
run: |
58-
aws s3 sync website/public/ s3://${{ vars.AMPLIFY_S3_BUCKET_BETA }}/pr-${{ env.PR_NUMBER }}/ --delete
59-
- name: Create Amplify branch (if doesn't exist)
60-
run: |
61-
if ! aws amplify get-branch --app-id ${{ vars.AMPLIFY_APP_ID_BETA }} --branch-name "${{ env.BRANCH_NAME }}" 2>/dev/null; then
62-
aws amplify create-branch \
63-
--app-id ${{ vars.AMPLIFY_APP_ID_BETA }} \
64-
--branch-name "${{ env.BRANCH_NAME }}" \
65-
--description "Preview for PR #${{ env.PR_NUMBER }}"
66-
fi
67-
- name: Configure redirects
68-
run: |
69-
REDIRECT_RULES=$(go run test/hack/resource/parse-redirects/main.go)
70-
aws amplify update-app \
71-
--app-id ${{ vars.AMPLIFY_APP_ID_BETA }} \
72-
--custom-rules "$REDIRECT_RULES"
73-
- name: Deploy to Amplify
74-
run: |
75-
aws amplify start-deployment \
76-
--app-id ${{ vars.AMPLIFY_APP_ID_BETA }} \
77-
--branch-name "${{ env.BRANCH_NAME }}" \
78-
--source-url s3://${{ vars.AMPLIFY_S3_BUCKET_BETA }}/pr-${{ env.PR_NUMBER }}/ \
79-
--source-url-type BUCKET_PREFIX
35+
- name: Deploy website
36+
uses: ./.github/actions/deploy-website
37+
with:
38+
aws-role-arn: arn:aws:iam::${{ vars.RELEASE_PREVIEW_ACCOUNT_ID }}:role/${{ vars.WEBSITE_ROLE_NAME }}
39+
aws-region: ${{ vars.AMPLIFY_REGION }}
40+
amplify-app-id: ${{ vars.AMPLIFY_APP_ID_BETA }}
41+
amplify-branch-name: ${{ env.BRANCH_NAME }}
42+
s3-bucket: ${{ vars.AMPLIFY_S3_BUCKET_BETA }}/
43+
s3-prefix: pr-${{ env.PR_NUMBER }}
44+
hugo-base-url: ${{ env.PREVIEW_URL }}
45+
preview: 'true'
8046
- uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
8147
env:
8248
PREVIEW_URL: ${{ env.PREVIEW_URL }}

0 commit comments

Comments
 (0)