Skip to content

Commit bc924c0

Browse files
authored
nginx.org deploy to aws (#48)
1 parent 04ed2db commit bc924c0

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
name: nginx.org build
2+
3+
on:
4+
workflow_call:
5+
secrets:
6+
AWS_ACCOUNT_ID:
7+
required: true
8+
AWS_ROLE_NAME_PROD:
9+
required: true
10+
AWS_ROLE_NAME_STAGING:
11+
required: true
12+
ALLOWED_USERS:
13+
required: true
14+
inputs:
15+
deployment_env:
16+
required: false
17+
type: string
18+
default: staging
19+
url_prod:
20+
required: false
21+
type: string
22+
default: nginx.org/preview
23+
url_staging:
24+
required: false
25+
type: string
26+
default: nginx.org/previews
27+
28+
permissions:
29+
contents: read
30+
id-token: write
31+
32+
defaults:
33+
run:
34+
shell: 'bash -Eeo pipefail -x {0}'
35+
36+
jobs:
37+
build:
38+
name: build
39+
runs-on: ubuntu-latest
40+
env:
41+
AWS_REGION: eu-central-1
42+
43+
steps:
44+
- name: Install dependencies
45+
run: |
46+
sudo apt-get update
47+
sudo apt-get install -y libxslt1-dev xsltproc libxml2-utils netpbm python-is-python3
48+
49+
- name: Checkout
50+
uses: actions/checkout@v4
51+
52+
- name: Check prod access
53+
if: ${{ inputs.deployment_env == 'prod' }}
54+
run: |
55+
if [ "$GITHUB_REF" != "refs/heads/main" ]; then
56+
echo "Error: Production deployments are only allowed from the main branch."
57+
exit 1
58+
fi
59+
60+
if [ "$GITHUB_REPOSITORY_OWNER" != "nginx" ] && [ "$GITHUB_REPOSITORY_OWNER" != "nginxinc" ]; then
61+
echo "Error: This workflow is only allowed in repositories owned by 'nginx' or 'nginxinc'."
62+
exit 1
63+
fi
64+
65+
ALLOWED="${{ secrets.ALLOWED_USERS }}"
66+
for user in $ALLOWED; do
67+
if [ "$GITHUB_ACTOR" == "$user" ]; then
68+
echo "User $GITHUB_ACTOR is allowed to deploy to prod"
69+
exit 0
70+
fi
71+
done
72+
73+
echo "User $GITHUB_ACTOR is NOT allowed to deploy to prod"
74+
exit 1
75+
76+
- name: Configure AWS credentials
77+
uses: aws-actions/configure-aws-credentials@v4
78+
with:
79+
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ inputs.deployment_env == 'prod' && secrets.AWS_ROLE_NAME_PROD || secrets.AWS_ROLE_NAME_STAGING }}
80+
aws-region: ${{ env.AWS_REGION }}
81+
82+
- name: Determine S3 path
83+
id: s3path
84+
run: |
85+
SAFE_REPO="${GITHUB_REPOSITORY//\//-}"
86+
if [[ "${{ inputs.deployment_env }}" == "prod" ]]; then
87+
BUCKET="nginx-org-prod"
88+
PATH_PART="preview"
89+
PUBLIC_URL="${{ inputs.url_prod }}"
90+
else
91+
BUCKET="nginx-org-staging"
92+
PATH_PART="previews/${GITHUB_SHA}"
93+
PUBLIC_URL="${{ inputs.url_staging }}/${GITHUB_SHA}/"
94+
fi
95+
echo "bucket=$BUCKET" >> $GITHUB_OUTPUT
96+
echo "path=$PATH_PART" >> $GITHUB_OUTPUT
97+
echo "s3_uri=s3://$BUCKET/$SAFE_REPO/$PATH_PART/" >> $GITHUB_OUTPUT
98+
echo "public_url=$PUBLIC_URL" >> $GITHUB_OUTPUT
99+
echo "safe_repo=$SAFE_REPO" >> $GITHUB_OUTPUT
100+
101+
- name: Build site
102+
run: |
103+
set -e
104+
make all
105+
make gzip
106+
make images
107+
make genapi
108+
make all
109+
make copy NGINX_ORG=www
110+
111+
# Verify build output
112+
if [ ! -d www ]; then
113+
echo "Error: Build did not create www/ directory"
114+
exit 1
115+
fi
116+
117+
- name: Add deployment metadata
118+
run: |
119+
TIMESTAMP="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
120+
mkdir -p meta
121+
echo "$GITHUB_SHA deployed at $TIMESTAMP" > meta/.deployed.txt
122+
{
123+
echo "sha=$GITHUB_SHA"
124+
echo "repo=$GITHUB_REPOSITORY"
125+
echo "actor=$GITHUB_ACTOR"
126+
echo "timestamp=$TIMESTAMP"
127+
} > meta/.tags.txt
128+
cp meta/.deployed.txt www/
129+
cp meta/.tags.txt www/
130+
131+
- name: Sync www/ to S3
132+
run: |
133+
aws s3 sync www/ s3://${{ steps.s3path.outputs.bucket }}/${{ steps.s3path.outputs.safe_repo }}/${{ steps.s3path.outputs.path }}/ --delete --exact-timestamps
134+
135+
- name: Show uploaded files
136+
run: |
137+
aws s3 ls s3://${{ steps.s3path.outputs.bucket }}/${{ steps.s3path.outputs.safe_repo }}/${{ steps.s3path.outputs.path }}/ --recursive
138+
139+
- name: Deployment summary
140+
run: |
141+
{
142+
echo "### Deployment Summary"
143+
echo ""
144+
echo "| Key | Value |"
145+
echo "|------------------|-------|"
146+
echo "| deployment_env | ${{ inputs.deployment_env }} |"
147+
echo "| repository | $GITHUB_REPOSITORY |"
148+
echo "| actor | $GITHUB_ACTOR |"
149+
echo "| commit | $GITHUB_SHA |"
150+
echo "| S3 path | ${{ steps.s3path.outputs.s3_uri }} |"
151+
echo "| Public URL | ${{ steps.s3path.outputs.public_url }} |"
152+
} >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)