Skip to content

Commit 726dc65

Browse files
committed
Adjust reusable workflows
1 parent a1867d2 commit 726dc65

File tree

4 files changed

+103
-40
lines changed

4 files changed

+103
-40
lines changed

.github/workflows/build.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ on:
1313
required: true
1414
description: "Name of the S3 registry bucket"
1515
type: string
16+
custom_hash:
17+
required: false
18+
description: "Custom hash used to cache the action on successful build"
19+
type: string
1620
build_dir:
1721
required: true
1822
description: "Location of the deploy bundle after running the build command"
@@ -63,11 +67,12 @@ jobs:
6367
- uses: actions/[email protected]
6468

6569
- name: Check S3 Cache
66-
uses: pleo-io/s3-cache-action@v3.0.0
70+
uses: pleo-io/s3-cache-action@v3.1.0
6771
id: s3-cache
6872
with:
6973
bucket-name: ${{ inputs.bucket_name }}
7074
key-prefix: build/${{ inputs.app_name }}
75+
custom-hash: ${{ inputs.custom_hash }}
7176
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_FRONTEND_REGISTRY }}
7277
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_FRONTEND_REGISTRY }}
7378
aws-region: eu-west-1

.github/workflows/deploy.yml

+46-20
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ on:
2222
default: "dist"
2323
description: "Directory where the bundle should be unpacked"
2424
type: string
25-
tree_hash:
25+
deploy_hash:
2626
required: true
2727
description: "Tree hash of the code to deploy"
2828
type: string
@@ -65,6 +65,25 @@ jobs:
6565
registry-url: "https://npm.pkg.github.com"
6666
scope: ${{ inputs.registry_scope }}
6767

68+
- name: Setup AWS Credentials for Origin Bucket Access
69+
uses: aws-actions/[email protected]
70+
with:
71+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
72+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
73+
aws-region: eu-west-1
74+
75+
- name: Check version already deployed
76+
id: is-version-already-deployed
77+
run: |
78+
DEPLOY_EXISTS=$(aws s3 ls s3://${{ inputs.bucket_name }}/html/${{ inputs.deploy_hash }}/ || true)
79+
if [ -z "$DEPLOY_EXISTS" ]; then
80+
echo "Version ${{ inputs.deploy_hash }} not yet deployed"
81+
echo "is_deployed=false" >> $GITHUB_OUTPUT
82+
else
83+
echo "Version ${{ inputs.deploy_hash }} already deployed"
84+
echo "is_deployed=true" >> $GITHUB_OUTPUT
85+
fi
86+
6887
# For feature preview deployments we're using the permalink as the deploy URL for the GitHub deployment
6988
# For deployments on the default branch we're using the domain name passed via inputs.
7089
- name: Get Deployment URL
@@ -75,17 +94,19 @@ jobs:
7594
echo "Could not determine default branch"
7695
exit 1
7796
fi
78-
SUBDOMAIN=$([[ $GITHUB_REF = "refs/heads/$DEFAULT_BRANCH" || $GITHUB_REF = "refs/heads/change-freeze-emergency-deploy" ]] && echo "" || echo "preview-${{ inputs.tree_hash }}.")
97+
SUBDOMAIN=$([[ $GITHUB_REF = "refs/heads/$DEFAULT_BRANCH" || $GITHUB_REF = "refs/heads/change-freeze-emergency-deploy" ]] && echo "" || echo "preview-${{ inputs.deploy_hash }}.")
7998
echo "url=https://${SUBDOMAIN}${{ inputs.domain_name }}" >> $GITHUB_OUTPUT
8099
81100
- name: Setup AWS Credentials for Registry Bucket Access
82101
uses: aws-actions/[email protected]
102+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
83103
with:
84104
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_FRONTEND_REGISTRY }}
85105
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_FRONTEND_REGISTRY }}
86106
aws-region: eu-west-1
87107

88108
- name: Download & Unpack Bundle
109+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
89110
run: |
90111
aws s3 cp ${{ inputs.bundle_uri }} bundle.tar.gz
91112
mkdir ${{ inputs.bundle_dir }} && tar -xvzf bundle.tar.gz -C ${{ inputs.bundle_dir }}
@@ -98,45 +119,57 @@ jobs:
98119
aws-region: eu-west-1
99120

100121
- name: Inject Environment Config
101-
if: inputs.inject_config_cmd
122+
if: inputs.inject_config_cmd && ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
102123
env:
103124
NODE_AUTH_TOKEN: ${{ secrets.GH_REGISTRY_NPM_TOKEN }}
104125
SPA_BUNDLE_DIR: ${{ inputs.bundle_dir }}
105126
SPA_ENV: ${{inputs.environment}}
106-
SPA_TREE_HASH: ${{ inputs.tree_hash}}
127+
SPA_TREE_HASH: ${{ inputs.deploy_hash}}
107128
run: ${{inputs.inject_config_cmd }}
108129

109130
- name: Copy Static Files
131+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
110132
run: |
111133
aws s3 cp ${{ inputs.bundle_dir }}/static s3://${{ inputs.bucket_name }}/static \
112134
--cache-control 'public,max-age=31536000,immutable' \
113135
--recursive
114136
115137
- name: Copy HTML Files
138+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
116139
run: |
117-
aws s3 cp ${{ inputs.bundle_dir }}/ s3://${{ inputs.bucket_name }}/html/${{ inputs.tree_hash }} \
140+
aws s3 cp ${{ inputs.bundle_dir }}/ s3://${{ inputs.bucket_name }}/html/${{ inputs.deploy_hash }} \
118141
--cache-control 'public,max-age=31536000,immutable' \
119142
--recursive \
120143
--exclude "static/*"
121144
122145
- name: Update .well-known Files If Exists
146+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
123147
run: |
124148
aws s3 cp \
125-
s3://${{ inputs.bucket_name }}/html/${{ inputs.tree_hash }}/.well-known/apple-app-site-association \
126-
s3://${{ inputs.bucket_name }}/html/${{ inputs.tree_hash }}/.well-known/apple-app-site-association \
149+
s3://${{ inputs.bucket_name }}/html/${{ inputs.deploy_hash }}/.well-known/apple-app-site-association \
150+
s3://${{ inputs.bucket_name }}/html/${{ inputs.deploy_hash }}/.well-known/apple-app-site-association \
127151
--content-type 'application/json' \
128152
--cache-control 'public,max-age=3600' \
129153
--metadata-directive REPLACE || echo "Failed updating .well-known files"
130154
155+
# We always copy deploy hash file even if we don't do an actual deployemnt as part of this commit
156+
# This is used to identify which version of the deployed app is associated with the current commit
157+
# In case we trigger rollback to this commit, we know what deployment to use based on the stored deploy_hash for this commit
158+
- name: Copy Deploy Hash File
159+
run: |
160+
echo ${{ inputs.deploy_hash }} >> ${{ github.sha }}
161+
aws s3 cp ${{ github.sha }} s3://${{ inputs.bucket_name }}/deploys/commits/${{ github.sha }}
162+
131163
- name: Update Cursor File
132164
id: cursor-update
133-
uses: pleo-io/spa-tools/actions/[email protected]
165+
uses: pleo-io/spa-tools/actions/[email protected]
166+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
134167
with:
135168
bucket_name: ${{ inputs.bucket_name }}
136169

137170
- name: Update PR Description
138-
uses: pleo-io/spa-tools/actions/post-preview-urls@spa-github-actions-v9.0.2
139-
if: github.event_name == 'pull_request'
171+
uses: pleo-io/spa-tools/actions/post-preview-urls@spa-github-actions-v10.0.0
172+
if: github.event_name == 'pull_request' && ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
140173
with:
141174
app_name: ${{ inputs.app_name }}
142175
links: |
@@ -145,15 +178,8 @@ jobs:
145178
{"name": "Current Permalink", "url": "${{ steps.deployment-url.outputs.url }}"}
146179
]
147180
148-
- name: Upload Deployed Bundle as Artifact
149-
uses: actions/[email protected]
150-
with:
151-
name: bundle-${{ inputs.tree_hash }}-${{ inputs.environment }}
152-
path: bundle.tar.gz
153-
154181
- name: Verify app deployment
155-
if: inputs.inject_config_cmd
182+
if: inputs.inject_config_cmd && ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
156183
# We expect the injection to insert the version in the HTML.
157-
run:
158-
curl --silent --show-error ${{ steps.deployment-url.outputs.url }} |
159-
grep -q ${{ inputs.tree_hash }}
184+
run: curl --silent --show-error ${{ steps.deployment-url.outputs.url }} |
185+
grep -q ${{ inputs.deploy_hash }}

reusable-workflows/build.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ on:
1313
required: true
1414
description: "Name of the S3 registry bucket"
1515
type: string
16+
custom_hash:
17+
required: false
18+
description: "Custom hash used to cache the action on successful build"
19+
type: string
1620
build_dir:
1721
required: true
1822
description: "Location of the deploy bundle after running the build command"
@@ -63,11 +67,12 @@ jobs:
6367
- uses: actions/[email protected]
6468

6569
- name: Check S3 Cache
66-
uses: pleo-io/s3-cache-action@v3.0.0
70+
uses: pleo-io/s3-cache-action@v3.1.0
6771
id: s3-cache
6872
with:
6973
bucket-name: ${{ inputs.bucket_name }}
7074
key-prefix: build/${{ inputs.app_name }}
75+
custom-hash: ${{ inputs.custom_hash }}
7176
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_FRONTEND_REGISTRY }}
7277
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_FRONTEND_REGISTRY }}
7378
aws-region: eu-west-1

reusable-workflows/deploy.yml

+45-18
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ on:
2222
default: "dist"
2323
description: "Directory where the bundle should be unpacked"
2424
type: string
25-
tree_hash:
25+
deploy_hash:
2626
required: true
2727
description: "Tree hash of the code to deploy"
2828
type: string
@@ -65,6 +65,25 @@ jobs:
6565
registry-url: "https://npm.pkg.github.com"
6666
scope: ${{ inputs.registry_scope }}
6767

68+
- name: Setup AWS Credentials for Origin Bucket Access
69+
uses: aws-actions/[email protected]
70+
with:
71+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
72+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
73+
aws-region: eu-west-1
74+
75+
- name: Check version already deployed
76+
id: is-version-already-deployed
77+
run: |
78+
DEPLOY_EXISTS=$(aws s3 ls s3://${{ inputs.bucket_name }}/html/${{ inputs.deploy_hash }}/ || true)
79+
if [ -z "$DEPLOY_EXISTS" ]; then
80+
echo "Version ${{ inputs.deploy_hash }} not yet deployed"
81+
echo "is_deployed=false" >> $GITHUB_OUTPUT
82+
else
83+
echo "Version ${{ inputs.deploy_hash }} already deployed"
84+
echo "is_deployed=true" >> $GITHUB_OUTPUT
85+
fi
86+
6887
# For feature preview deployments we're using the permalink as the deploy URL for the GitHub deployment
6988
# For deployments on the default branch we're using the domain name passed via inputs.
7089
- name: Get Deployment URL
@@ -75,17 +94,19 @@ jobs:
7594
echo "Could not determine default branch"
7695
exit 1
7796
fi
78-
SUBDOMAIN=$([[ $GITHUB_REF = "refs/heads/$DEFAULT_BRANCH" || $GITHUB_REF = "refs/heads/change-freeze-emergency-deploy" ]] && echo "" || echo "preview-${{ inputs.tree_hash }}.")
97+
SUBDOMAIN=$([[ $GITHUB_REF = "refs/heads/$DEFAULT_BRANCH" || $GITHUB_REF = "refs/heads/change-freeze-emergency-deploy" ]] && echo "" || echo "preview-${{ inputs.deploy_hash }}.")
7998
echo "url=https://${SUBDOMAIN}${{ inputs.domain_name }}" >> $GITHUB_OUTPUT
8099
81100
- name: Setup AWS Credentials for Registry Bucket Access
82101
uses: aws-actions/[email protected]
102+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
83103
with:
84104
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_FRONTEND_REGISTRY }}
85105
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_FRONTEND_REGISTRY }}
86106
aws-region: eu-west-1
87107

88108
- name: Download & Unpack Bundle
109+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
89110
run: |
90111
aws s3 cp ${{ inputs.bundle_uri }} bundle.tar.gz
91112
mkdir ${{ inputs.bundle_dir }} && tar -xvzf bundle.tar.gz -C ${{ inputs.bundle_dir }}
@@ -98,45 +119,57 @@ jobs:
98119
aws-region: eu-west-1
99120

100121
- name: Inject Environment Config
101-
if: inputs.inject_config_cmd
122+
if: inputs.inject_config_cmd && ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
102123
env:
103124
NODE_AUTH_TOKEN: ${{ secrets.GH_REGISTRY_NPM_TOKEN }}
104125
SPA_BUNDLE_DIR: ${{ inputs.bundle_dir }}
105126
SPA_ENV: ${{inputs.environment}}
106-
SPA_TREE_HASH: ${{ inputs.tree_hash}}
127+
SPA_TREE_HASH: ${{ inputs.deploy_hash}}
107128
run: ${{inputs.inject_config_cmd }}
108129

109130
- name: Copy Static Files
131+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
110132
run: |
111133
aws s3 cp ${{ inputs.bundle_dir }}/static s3://${{ inputs.bucket_name }}/static \
112134
--cache-control 'public,max-age=31536000,immutable' \
113135
--recursive
114136
115137
- name: Copy HTML Files
138+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
116139
run: |
117-
aws s3 cp ${{ inputs.bundle_dir }}/ s3://${{ inputs.bucket_name }}/html/${{ inputs.tree_hash }} \
140+
aws s3 cp ${{ inputs.bundle_dir }}/ s3://${{ inputs.bucket_name }}/html/${{ inputs.deploy_hash }} \
118141
--cache-control 'public,max-age=31536000,immutable' \
119142
--recursive \
120143
--exclude "static/*"
121144
122145
- name: Update .well-known Files If Exists
146+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
123147
run: |
124148
aws s3 cp \
125-
s3://${{ inputs.bucket_name }}/html/${{ inputs.tree_hash }}/.well-known/apple-app-site-association \
126-
s3://${{ inputs.bucket_name }}/html/${{ inputs.tree_hash }}/.well-known/apple-app-site-association \
149+
s3://${{ inputs.bucket_name }}/html/${{ inputs.deploy_hash }}/.well-known/apple-app-site-association \
150+
s3://${{ inputs.bucket_name }}/html/${{ inputs.deploy_hash }}/.well-known/apple-app-site-association \
127151
--content-type 'application/json' \
128152
--cache-control 'public,max-age=3600' \
129153
--metadata-directive REPLACE || echo "Failed updating .well-known files"
130154
155+
# We always copy deploy hash file even if we don't do an actual deployemnt as part of this commit
156+
# This is used to identify which version of the deployed app is associated with the current commit
157+
# In case we trigger rollback to this commit, we know what deployment to use based on the stored deploy_hash for this commit
158+
- name: Copy Deploy Hash File
159+
run: |
160+
echo ${{ inputs.deploy_hash }} >> ${{ github.sha }}
161+
aws s3 cp ${{ github.sha }} s3://${{ inputs.bucket_name }}/deploys/commits/${{ github.sha }}
162+
131163
- name: Update Cursor File
132164
id: cursor-update
133-
uses: pleo-io/spa-tools/actions/[email protected]
165+
uses: pleo-io/spa-tools/actions/[email protected]
166+
if: ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
134167
with:
135168
bucket_name: ${{ inputs.bucket_name }}
136169

137170
- name: Update PR Description
138-
uses: pleo-io/spa-tools/actions/post-preview-urls@spa-github-actions-v9.0.2
139-
if: github.event_name == 'pull_request'
171+
uses: pleo-io/spa-tools/actions/post-preview-urls@spa-github-actions-v10.0.0
172+
if: github.event_name == 'pull_request' && ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
140173
with:
141174
app_name: ${{ inputs.app_name }}
142175
links: |
@@ -145,15 +178,9 @@ jobs:
145178
{"name": "Current Permalink", "url": "${{ steps.deployment-url.outputs.url }}"}
146179
]
147180
148-
- name: Upload Deployed Bundle as Artifact
149-
uses: actions/[email protected]
150-
with:
151-
name: bundle-${{ inputs.tree_hash }}-${{ inputs.environment }}
152-
path: bundle.tar.gz
153-
154181
- name: Verify app deployment
155-
if: inputs.inject_config_cmd
182+
if: inputs.inject_config_cmd && ${{ steps.is-version-already-deployed.outputs.is_deployed == 'false'}}
156183
# We expect the injection to insert the version in the HTML.
157184
run:
158185
curl --silent --show-error ${{ steps.deployment-url.outputs.url }} |
159-
grep -q ${{ inputs.tree_hash }}
186+
grep -q ${{ inputs.deploy_hash }}

0 commit comments

Comments
 (0)