Skip to content

Commit 94f502a

Browse files
authored
Merge pull request #1 from electricbookworks/s3-book-content
S3 book content
2 parents c39b691 + 7242527 commit 94f502a

File tree

3 files changed

+108
-78
lines changed

3 files changed

+108
-78
lines changed

.github/workflows/deploy.config.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"book-server": {
3-
"repo": "electricbookworks/electric-book-server-template",
4-
"branches": [
5-
"staging",
6-
"live"
7-
],
3+
"bucket-comment": "The bucket name is suffixed with -live or -staging depending on the branch.",
4+
"bucket": "ebt-books",
85
"configs-comment": "All configs are relative to the _configs folder.",
96
"configs": ["_config.live.yml"],
107
"builds": [
8+
{
9+
"configs": [],
10+
"dir": "electric-book"
11+
},
1112
{
1213
"configs": ["_config.student.yml"],
1314
"dir": "audience/student"
@@ -30,5 +31,11 @@
3031
"options": ""
3132
}
3233
]
34+
},
35+
"vercel": {
36+
"trigger": {
37+
"live": "https://api.vercel.com/v1/integrations/deploy/prj_3JQKhLSRlptDAqiKPWyuKMGhbaGG/LbOKWUxQpc",
38+
"staging": "https://api.vercel.com/v1/integrations/deploy/prj_3JQKhLSRlptDAqiKPWyuKMGhbaGG/gKusn6nGUb"
39+
}
3340
}
3441
}

.github/workflows/deploy.yml

Lines changed: 82 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ jobs:
3030
skip_book_server: ${{ steps.check_config.outputs.skip_book_server }}
3131
should_build: ${{ steps.check_builds.outputs.should_build }}
3232
should_deploy: ${{ steps.check_branch.outputs.should_deploy }}
33-
deploy_repo: ${{ steps.load_config.outputs.deploy_repo }}
34-
deploy_branches: ${{ steps.load_config.outputs.deploy_branches }}
33+
deploy_bucket: ${{ steps.load_config.outputs.deploy_bucket }}
3534
deploy_configs: ${{ steps.load_config.outputs.deploy_configs }}
3635
build_dirs: ${{ steps.load_config.outputs.build_dirs }}
3736
build_configs: ${{ steps.load_config.outputs.build_configs }}
@@ -60,14 +59,12 @@ jobs:
6059
exit 0
6160
fi
6261
63-
DEPLOY_REPO=$(jq -r '.["book-server"].repo' "$CONFIG_FILE")
64-
DEPLOY_BRANCHES=$(jq -r '.["book-server"].branches | join(",")' "$CONFIG_FILE")
62+
DEPLOY_BUCKET=$(jq -r '.["book-server"].bucket' "$CONFIG_FILE")
6563
DEPLOY_CONFIGS=$(jq -r '.["book-server"].configs | join(",")' "$CONFIG_FILE")
6664
BUILD_DIRS=$(jq -r '.["book-server"].builds | map(.dir) | join(",")' "$CONFIG_FILE")
6765
BUILD_CONFIGS=$(jq -r '.["book-server"].builds | map(.configs | if length > 0 then join(",") else " " end) | join(";")' "$CONFIG_FILE")
6866
69-
echo "deploy_repo=${DEPLOY_REPO}" >> $GITHUB_OUTPUT
70-
echo "deploy_branches=${DEPLOY_BRANCHES}" >> $GITHUB_OUTPUT
67+
echo "deploy_bucket=${DEPLOY_BUCKET}" >> $GITHUB_OUTPUT
7168
echo "deploy_configs=${DEPLOY_CONFIGS}" >> $GITHUB_OUTPUT
7269
echo "build_dirs=${BUILD_DIRS}" >> $GITHUB_OUTPUT
7370
echo "build_configs=${BUILD_CONFIGS}" >> $GITHUB_OUTPUT
@@ -90,27 +87,22 @@ jobs:
9087
if: steps.check_builds.outputs.should_build == 'true'
9188
run: |
9289
CURRENT_BRANCH="${{ github.ref_name }}"
93-
IFS=',' read -ra ALLOWED <<< "${{ steps.load_config.outputs.deploy_branches }}"
94-
BRANCH_ALLOWED=false
95-
for branch in "${ALLOWED[@]}"; do
96-
if [[ "${CURRENT_BRANCH}" == "${branch// /}" ]]; then
97-
BRANCH_ALLOWED=true
98-
break
99-
fi
100-
done
101-
102-
if [[ "${BRANCH_ALLOWED}" == "false" ]]; then
103-
echo "❌ Branch '${CURRENT_BRANCH}' is not allowed, skipping deployment."
90+
if [ "$CURRENT_BRANCH" != "live" ] && [ "$CURRENT_BRANCH" != "staging" ]; then
91+
echo "❌ Branch '${CURRENT_BRANCH}' is not 'live' or 'staging', skipping deployment."
10492
echo "should_deploy=false" >> $GITHUB_OUTPUT
10593
else
10694
echo "✅ Branch '${CURRENT_BRANCH}' is allowed for deployment."
10795
echo "should_deploy=true" >> $GITHUB_OUTPUT
96+
echo "target_branch=${CURRENT_BRANCH}" >> $GITHUB_OUTPUT
10897
fi
10998
11099
deploy-to-book-server:
111100
needs: pre-check
112101
if: needs.pre-check.outputs.should_deploy == 'true'
113102
runs-on: ubuntu-latest
103+
permissions:
104+
id-token: write
105+
contents: read
114106

115107
steps:
116108
- name: Checkout source repository
@@ -158,72 +150,47 @@ jobs:
158150
FULL_CONFIGS="${{ needs.pre-check.outputs.deploy_configs }}"
159151
fi
160152
161-
npm run eb -- output --baseurl="${BUILD_DIR}" --configs="${FULL_CONFIGS}" --dontserve=true --deploy=true
153+
npm run eb -- output --baseurl="/${DIR_NAME}" --configs="${FULL_CONFIGS}" --dontserve=true --deploy=true
162154
163-
mkdir -p "_build_staging${BUILD_DIR}"
164-
cp -r _site${BUILD_DIR}/* "_build_staging${BUILD_DIR}/"
155+
mkdir -p "_build_staging/${DIR_NAME}"
156+
cp -r "_site/${DIR_NAME}"/* "_build_staging/${DIR_NAME}/"
165157
done
166158
167-
- name: Clone target repository and determine target branch
168-
env:
169-
ORG_DEPLOY_TOKEN: ${{ secrets.ORG_DEPLOY_TOKEN }}
159+
- name: Configure AWS credentials
160+
uses: aws-actions/configure-aws-credentials@v4
161+
with:
162+
role-to-assume: arn:aws:iam::381492306053:role/GitHubActionsS3Access
163+
aws-region: eu-west-2
164+
165+
- name: Deploy to S3
170166
run: |
171-
git clone "https://${ORG_DEPLOY_TOKEN}@github.com/${{ needs.pre-check.outputs.deploy_repo }}.git" target-repo
172-
cd target-repo
173-
174-
SOURCE_BRANCH="${{ github.ref_name }}"
175-
TARGET_BRANCH="${SOURCE_BRANCH}"
167+
TARGET_BRANCH="${{ needs.pre-check.outputs.target_branch }}"
168+
BUCKET="${{ needs.pre-check.outputs.deploy_bucket }}"
176169
177-
if git show-ref --verify --quiet refs/remotes/origin/${SOURCE_BRANCH}; then
178-
git checkout ${SOURCE_BRANCH}
170+
if [ "$TARGET_BRANCH" = "live" ]; then
171+
BUCKET_SUFFIX="-live"
179172
else
180-
if [[ "${SOURCE_BRANCH}" == "master" ]] && git show-ref --verify --quiet refs/remotes/origin/main; then
181-
TARGET_BRANCH="main"
182-
git checkout main
183-
elif [[ "${SOURCE_BRANCH}" == "main" ]] && git show-ref --verify --quiet refs/remotes/origin/master; then
184-
TARGET_BRANCH="master"
185-
git checkout master
186-
else
187-
git checkout -b ${SOURCE_BRANCH}
188-
fi
173+
BUCKET_SUFFIX="-staging"
189174
fi
190175
191-
echo "TARGET_BRANCH=${TARGET_BRANCH}" >> $GITHUB_ENV
192-
193-
- name: Deploy to target repository
194-
env:
195-
ORG_DEPLOY_TOKEN: ${{ secrets.ORG_DEPLOY_TOKEN }}
196-
run: |
197-
cd target-repo
198-
git config url."https://${ORG_DEPLOY_TOKEN}@github.com/".insteadOf "https://github.com/"
176+
S3_BUCKET="${BUCKET}${BUCKET_SUFFIX}"
199177
200178
IFS=',' read -ra DIRS <<< "${{ needs.pre-check.outputs.build_dirs }}"
201179
202-
for DIR_NAME in "${DIRS[@]}"; do
203-
DEPLOY_DIR="/${DIR_NAME#*/}"
204-
rm -rf public${DEPLOY_DIR}
205-
mkdir -p public${DEPLOY_DIR}
206-
cp -r ../_build_staging${DEPLOY_DIR}/* public${DEPLOY_DIR}/
207-
find public${DEPLOY_DIR} -name ".gitignore" -delete
208-
done
209-
210-
git config user.name "GitHub Actions"
211-
git config user.email "actions@github.com"
212-
git add . --force
180+
# Get the unique root directories
181+
ROOT_DIRS=$(for dir in "${DIRS[@]}"; do echo "${dir%%/*}"; done | sort -u)
213182
214-
if git diff --staged --quiet; then
215-
echo "No changes to deploy"
216-
exit 0
217-
fi
183+
for ROOT_DIR in $ROOT_DIRS; do
184+
echo "Syncing directory '$ROOT_DIR' to s3://${S3_BUCKET}/${ROOT_DIR}/"
185+
aws s3 sync "_build_staging/${ROOT_DIR}/" "s3://${S3_BUCKET}/${ROOT_DIR}/" --delete
186+
done
218187
219-
git commit -m "Deploy '${{ github.event.head_commit.message }}' from ${{ github.repository }}"
220-
git push origin ${TARGET_BRANCH}
188+
echo "S3_BUCKET=${S3_BUCKET}" >> $GITHUB_ENV
221189
222190
- name: Deployment Summary
223191
run: |
224-
echo "✅ Successfully deployed to ${{ needs.pre-check.outputs.deploy_repo }}"
192+
echo "✅ Successfully deployed to ${S3_BUCKET}"
225193
echo "📁 Source branch: ${{ github.ref_name }}"
226-
echo "📁 Target branch: ${TARGET_BRANCH}"
227194
echo "📂 Deployed directories: ${{ needs.pre-check.outputs.build_dirs }}"
228195
echo "🔧 Build configs used: ${{ needs.pre-check.outputs.build_configs }}"
229196
echo "🔗 Called from: ${{ github.repository }}"
@@ -297,4 +264,52 @@ jobs:
297264
298265
# Sync to S3, expanding the options array
299266
aws s3 sync "$source/" "s3://$bucket/$dest/" "${options_array[@]}"
300-
done
267+
done
268+
269+
pre-check-vercel:
270+
runs-on: ubuntu-latest
271+
outputs:
272+
should_trigger: ${{ steps.check_vercel.outputs.should_trigger }}
273+
trigger_url: ${{ steps.check_vercel.outputs.trigger_url }}
274+
steps:
275+
- name: Checkout config file
276+
uses: actions/checkout@v4
277+
with:
278+
sparse-checkout: ${{ inputs.config-file }}
279+
fetch-depth: 1
280+
281+
- name: Check for vercel trigger
282+
id: check_vercel
283+
run: |
284+
CONFIG_FILE="${{ inputs.config-file }}"
285+
286+
if [ ! -f "$CONFIG_FILE" ] || ! jq -e 'has("vercel")' "$CONFIG_FILE" > /dev/null || ! jq -e '.vercel | has("trigger")' "$CONFIG_FILE" > /dev/null; then
287+
echo "No vercel trigger configuration found. Skipping."
288+
echo "should_trigger=false" >> $GITHUB_OUTPUT
289+
exit 0
290+
fi
291+
292+
CURRENT_BRANCH="${{ github.ref_name }}"
293+
if [ "$CURRENT_BRANCH" != "live" ] && [ "$CURRENT_BRANCH" != "staging" ]; then
294+
echo "Branch '$CURRENT_BRANCH' is not 'live' or 'staging'. Skipping Vercel trigger."
295+
echo "should_trigger=false" >> $GITHUB_OUTPUT
296+
exit 0
297+
fi
298+
299+
TRIGGER_URL=$(jq -r ".vercel.trigger.${CURRENT_BRANCH}" "$CONFIG_FILE")
300+
if [ -z "$TRIGGER_URL" ] || [ "$TRIGGER_URL" == "null" ]; then
301+
echo "No Vercel trigger URL found for branch '$CURRENT_BRANCH'. Skipping."
302+
echo "should_trigger=false" >> $GITHUB_OUTPUT
303+
exit 0
304+
fi
305+
306+
echo "should_trigger=true" >> $GITHUB_OUTPUT
307+
echo "trigger_url=${TRIGGER_URL}" >> $GITHUB_OUTPUT
308+
309+
trigger-vercel-deploy:
310+
needs: [deploy-to-book-server, sync-media-to-s3, pre-check-vercel]
311+
if: always() && needs.pre-check-vercel.outputs.should_trigger == 'true'
312+
runs-on: ubuntu-latest
313+
steps:
314+
- name: Trigger Vercel Deploy
315+
run: curl -X POST "${{ needs.pre-check-vercel.outputs.trigger_url }}"

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ Create `.github/workflows/deploy.config.json`:
4949
```json
5050
{
5151
"book-server": {
52-
"repo": "electricbookworks/electric-book-server-template",
53-
"branches": [
54-
"staging",
55-
"live"
56-
],
52+
"bucket-comment": "The bucket name is suffixed with -live or -staging depending on the branch.",
53+
"bucket": "ebt-books",
5754
"configs-comment": "All configs are relative to the _configs folder.",
5855
"configs": ["_config.live.yml"],
5956
"builds": [
57+
{
58+
"configs": [],
59+
"dir": "electric-book"
60+
},
6061
{
6162
"configs": ["_config.student.yml"],
6263
"dir": "audience/student"
@@ -79,14 +80,21 @@ Create `.github/workflows/deploy.config.json`:
7980
"options": ""
8081
}
8182
]
83+
},
84+
"vercel": {
85+
"trigger": {
86+
"live": "https://api.vercel.com/v1/integrations/deploy/prj_3JQKhLSRlptDAqiKPWyuKMGhbaGG/LbOKWUxQpc",
87+
"staging": "https://api.vercel.com/v1/integrations/deploy/prj_3JQKhLSRlptDAqiKPWyuKMGhbaGG/gKusn6nGUb"
88+
}
8289
}
8390
}
8491
```
8592

8693
In `deploy.config.json` configure the following:
8794

8895
1. The book server repo. This is an [Electric Book Server](https://github.com/electricbookworks/electric-book-server-template) instance (e.g. `core-book-server`) that serves projects from its `public` folder and is configured for its own continuous deployment.
89-
2. The branches that will trigger deployments on new commits. The workflow will push to the same branch on the deployment repo, matching `main` to `master` and vica versa.
96+
2. The S3 bucket to sync the built files to.
9097
3. Deployment configs to be used on all builds.
9198
4. The separate builds that need to be deployed. Each build has a deployment directory that will be pushed to the deployment repo's `public` directory. If the directory already exists, it will be replaced entirely by the new deployment. You can also configure build-specific configs for each.
9299
5. Media sync commands with S3.
100+
6. The Vercel deploy triggers. These are specific to the project and branch of the book server your project deploys to.

0 commit comments

Comments
 (0)