Skip to content

Commit

Permalink
chore(workflows): update deploy-to-netlify workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschuerch committed Jul 5, 2024
1 parent bf9dab8 commit e389808
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 80 deletions.
69 changes: 6 additions & 63 deletions .github/actions/deploy-to-netlify/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ inputs:
id:
description: The build id
required: true
comment_token:
description: The access_token to use for commenting.
required: true
comment_author:
description: The authors name to use for commenting.
required: true
netlify_auth_token:
description: Netlify personal-access-token for use in shell scripts and API access.
required: true
Expand All @@ -43,37 +37,14 @@ inputs:
description: The package that will be deployed
required: true

outputs:
preview-url:
description: The deployed preview url.
value: https://${{ steps.netlify_deploy.outputs.url_alias }}--${{ inputs.netlify_site_url }}

runs:
using: composite
steps:
- name: Find Preview Comment
uses: peter-evans/find-comment@v2
id: preview_comment
with:
token: ${{ inputs.comment_token }}
issue-number: ${{ inputs.id }}
comment-author: ${{ inputs.comment_author }}
body-includes: Preview environment ready

- name: Create Initial Preview Comment
uses: peter-evans/create-or-update-comment@v3
if: ${{ steps.preview_comment.outputs.comment-id == 0 }}
with:
token: ${{ inputs.comment_token }}
issue-number: ${{ inputs.id }}
edit-mode: replace
body: Preview environments are getting posted here, as soon as they are ready!
reactions: eyes

- name: Find Initial Preview Comment
uses: peter-evans/find-comment@v2
id: initial_preview_comment
with:
token: ${{ inputs.comment_token }}
issue-number: ${{ inputs.id }}
comment-author: ${{ inputs.comment_author }}
body-includes: Preview environments are getting posted here, as soon as they are ready!

- name: Install netlify-cli
shell: bash
run: pnpm i -g netlify-cli
Expand All @@ -86,33 +57,5 @@ runs:
# run command taken from https://gist.github.com/oneohthree/f528c7ae1e701ad990e6, shortened to 28 chars, prepended with build-number
run: |
url_alias=`echo "preview-${{ inputs.id }}" | iconv -t ascii//TRANSLIT | sed -E 's/[~\^]+//g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+\|-+$//g' | sed -E 's/^-+//g' | sed -E 's/-+$//g' | tr A-Z a-z`
netlify deploy --filter ${{inputs.package_name}} --build false --dir ${{ inputs.folder }} --site ${{ inputs.netlify_site_id }} --alias $url_alias
echo "url_alias=$url_alias" >> $GITHUB_OUTPUT
- name: Prepare Comment Message
id: comment
shell: bash
run: |
echo "message=Preview environment ready: https://${{ steps.netlify_deploy.outputs.url_alias }}--${{ inputs.netlify_site_url }}" >> $GITHUB_OUTPUT
- name: Replace Preview Comment
uses: peter-evans/create-or-update-comment@v3
if: ${{ steps.initial_preview_comment.outputs.comment-id != 0 }}
with:
token: ${{ inputs.comment_token }}
comment-id: ${{ steps.initial_preview_comment.outputs.comment-id }}
issue-number: ${{ inputs.id }}
edit-mode: replace
body: ${{ steps.comment.outputs.message }}
reactions: rocket

- name: Append Preview Comment
uses: peter-evans/create-or-update-comment@v3
if: ${{ steps.initial_preview_comment.outputs.comment-id == 0 && (steps.preview_comment.outputs.comment-id == 0 || !contains(steps.preview_comment.outputs.comment-body, steps.comment.outputs.message)) }}
with:
token: ${{ inputs.comment_token }}
comment-id: ${{ steps.preview_comment.outputs.comment-id }}
issue-number: ${{ inputs.id }}
edit-mode: append
body: ${{ steps.comment.outputs.message }}
reactions: rocket
netlify deploy --filter ${{inputs.package_name}} --build false --dir ${{ inputs.folder }} --site ${{ inputs.netlify_site_id }} --alias $url_alias
52 changes: 52 additions & 0 deletions .github/actions/preview/message/create/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Create preview message
description: Add a preview comment to the pr, if not already present

inputs:
access-token:
description: The access token to use for commenting.
required: true

runs:
using: composite
steps:
- uses: actions/github-script@v7
with:
github-token: ${{ inputs.access-token }}
script: |
const commentTitle = '**Related Previews**'
const commentInitialBody = 'Preview URLs will be added here, once they are ready... ![loader](https://github.com/swisspost/design-system/assets/9716662/49a75898-7093-4ffb-9460-071ff194459d)'
let comments
let previewComment
await getPreviewComment()
if (!previewComment) {
await github.rest.issues.createComment({
repo: context.repo.repo,
owner: context.repo.owner,
issue_number: context.issue.number,
body: `${commentTitle}\n${commentInitialBody}`
})
await getPreviewComment()
github.rest.reactions.createForIssueComment({
repo: context.repo.repo,
owner: context.repo.owner,
comment_id: previewComment.id,
content: 'eyes'
})
} else {
console.info('Skipped action, because preview comment already existed.')
}
async function getPreviewComment () {
comments = (await github.rest.issues.listComments({
repo: context.repo.repo,
owner: context.repo.owner,
issue_number: context.issue.number
})).data || []
previewComment = comments.find(c => c.user.login === 'swisspost-bot' && c.body.includes(commentTitle))
}
74 changes: 74 additions & 0 deletions .github/actions/preview/message/update/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Update preview message
description: Add a preview url to the existing preview comment, if not already present

inputs:
access-token:
description: The access token to use for commenting.
required: true
issue-number:
description: The issue number from the caller workflow.
required: true
preview-url:
description: The preview url to add in the comment.
required: true

runs:
using: composite
steps:
- uses: actions/github-script@v7
env:
ISSUE_NUMBER: ${{ inputs.issue-number }}
PREVIEW_URL: ${{ inputs.preview-url }}
with:
github-token: ${{ inputs.access-token }}
script: |
const { ISSUE_NUMBER, PREVIEW_URL } = process.env
const commentTitle = '**Related Previews**'
const commentInitialBody = 'Preview URLs will be added here, once they are ready... ![loader](https://github.com/swisspost/design-system/assets/9716662/49a75898-7093-4ffb-9460-071ff194459d)'
const comments = (await github.rest.issues.listComments({
repo: context.repo.repo,
owner: context.repo.owner,
issue_number: Number(ISSUE_NUMBER)
})).data || []
const previewComment = comments.find(c => c.user.login === 'swisspost-bot' && c.body.includes(commentTitle))
if (previewComment && !previewComment.body.includes(PREVIEW_URL)) {
github.rest.issues.updateComment({
repo: context.repo.repo,
owner: context.repo.owner,
comment_id: previewComment.id,
body: previewComment.body
.replace(commentInitialBody, '')
.concat(`- ${PREVIEW_URL}\n`)
})
const reactions = (await github.rest.reactions.listForIssueComment({
repo: context.repo.repo,
owner: context.repo.owner,
comment_id: previewComment.id,
})).data || []
const createReaction = reactions.find(r => r.user.login === 'swisspost-bot' && r.content === 'eyes')
const updateReaction = reactions.find(r => r.user.login === 'swisspost-bot' && r.content === 'rocket')
if (createReaction) {
github.rest.reactions.deleteForIssueComment({
repo: context.repo.repo,
owner: context.repo.owner,
comment_id: previewComment.id,
reaction_id: createReaction.id
})
}
if (!updateReaction) {
github.rest.reactions.createForIssueComment({
repo: context.repo.repo,
owner: context.repo.owner,
comment_id: previewComment.id,
content: 'rocket'
})
}
} else {
console.warn('Skipped action, because preview comment could not be found!')
}
54 changes: 54 additions & 0 deletions .github/workflows/build-documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Create preview message
uses: ./.github/actions/preview/message/create
with:
access-token: ${{ secrets.SWISSPOSTDEVS_ACCESS_TOKEN }}

- name: Setup
uses: ./.github/actions/setup-pnpm

Expand All @@ -38,3 +43,52 @@ jobs:
with:
name: design-system-documentation
folder: packages/documentation/storybook-static

deploy:
runs-on: ubuntu-latest
needs: build
# if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }}

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup-pnpm

- name: Download build artifacts
uses: ./.github/actions/artifact-download
id: build
with:
name: design-system-documentation
folder: build-output

- name: Get netlify site name
id: netlify
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const config = JSON.parse(fs.readFileSync('./packages/documentation/netlify.config.json', 'utf8'));
return config
- name: Deploy documentation to netlify
uses: ./.github/actions/deploy-to-netlify
id: deploy
with:
id: ${{ steps.build.outputs.id }}
netlify_auth_token: ${{ secrets.NETLIFY_AUTH_TOKEN }}
netlify_site_id: ${{ fromJSON(steps.netlify.outputs.result).siteId }}
netlify_site_url: ${{ fromJSON(steps.netlify.outputs.result).siteUrl }}
folder: ${{ steps.build.outputs.folder }}
comment_token: ${{ secrets.SWISSPOSTDEVS_ACCESS_TOKEN }}
comment_author: swisspost-bot
package_name: '@swisspost/design-system-documentation'

- name: Update preview message
uses: ./.github/actions/preview/message/update
with:
access-token: ${{ secrets.SWISSPOSTDEVS_ACCESS_TOKEN }}
issue-number: ${{ steps.build.outputs.id }}
preview-url: ${{ steps.deploy.outputs.preview-url }}
36 changes: 19 additions & 17 deletions .github/workflows/deploy-documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup
uses: ./.github/actions/setup-pnpm
Expand All @@ -37,18 +35,22 @@ jobs:
id: netlify
run: echo "STATE=$(jq . ./packages/documentation/netlify.config.json)" >> $GITHUB_OUTPUT

- name: TEST
run: echo ${{ fromJSON(steps.netlify.outputs.STATE) }}

# - name: Deploy documentation to netlify
# uses: ./.github/actions/deploy-to-netlify
# id: deploy
# with:
# id: ${{ steps.build.outputs.id }}
# netlify_auth_token: ${{ secrets.NETLIFY_AUTH_TOKEN }}
# netlify_site_id: ${{ fromJSON(steps.netlify.outputs.STATE).siteId }}
# netlify_site_url: ${{ fromJSON(steps.netlify.outputs.STATE).siteUrl }}
# folder: ${{ steps.build.outputs.folder }}
# comment_token: ${{ secrets.SWISSPOSTDEVS_ACCESS_TOKEN }}
# comment_author: swisspost-bot
# package_name: '@swisspost/design-system-documentation'
- name: Deploy documentation to netlify
uses: ./.github/actions/deploy-to-netlify
id: deploy
with:
id: ${{ steps.build.outputs.id }}
netlify_auth_token: ${{ secrets.NETLIFY_AUTH_TOKEN }}
netlify_site_id: ${{ fromJSON(steps.netlify.outputs.STATE).siteId }}
netlify_site_url: ${{ fromJSON(steps.netlify.outputs.STATE).siteUrl }}
folder: ${{ steps.build.outputs.folder }}
comment_token: ${{ secrets.SWISSPOSTDEVS_ACCESS_TOKEN }}
comment_author: swisspost-bot
package_name: '@swisspost/design-system-documentation'

- name: Update preview message
uses: ./.github/actions/preview/message/update
with:
access-token: ${{ secrets.SWISSPOSTDEVS_ACCESS_TOKEN }}
issue-number: ${{ steps.build.outputs.id }}
preview-url: ${{ steps.deploy.outputs.preview-url }}

0 comments on commit e389808

Please sign in to comment.