Skip to content

Commit

Permalink
feat: refactor workflows and add actions
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinLupa committed Dec 8, 2024
1 parent c0b557e commit 6606cfe
Show file tree
Hide file tree
Showing 12 changed files with 584 additions and 329 deletions.
23 changes: 23 additions & 0 deletions .github/actions/pr-number/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: "Extract Pull Request Number"

description: "Extract the pull request number that triggered the workflow."

outputs:
pr-number:
description: "The pull request number that triggered the workflow."
value: ${{ steps.extract-pr-number.outputs.pr-number }}

runs:
using: "composite"
steps:
- name: Get pull request number
id: extract-pr-number
shell: bash
run: |
# Retrieve the pull request number
PR_NUMBER="${{ github.event.pull_request.number }}"
echo "Extracted pull request number: $PR_NUMBER"
# Set the pull request number as output
echo "pr-number=$PR_NUMBER" >> $GITHUB_OUTPUT
86 changes: 86 additions & 0 deletions .github/actions/slack-notifications/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: Slack Notification
description: Send Slack notification for success or failure with deployment details

inputs:
app-name:
description: 'Name of the app being deployed'
required: true
deployment-url:
description: 'URL of the deployed app (output from a previous job)'
required: true
deployment-status:
description: 'Status of the deployment (success or failure)'
required: true
slack-channel-id:
description: 'Slack channel to send notifications'
required: true
slack-notifications-webhook-url:
description: 'Slack webhook URL for sending notifications'
required: true

runs:
using: composite
steps:
- name: Map app name to Vercel project
shell: bash
id: vercel-project
run: |
if [ "${{ inputs.app-name }}" == "Unified-web App" ]; then
echo "project-name=dfds-unified-web" >> $GITHUB_OUTPUT
elif [ "${{ inputs.app-name }}" == "Login App" ]; then
echo "project-name=unified-web-login" >> $GITHUB_OUTPUT
fi
- name: Set Slack notification message
shell: bash
id: set-message
run: |
if [ "${{ inputs.deployment-status }}" == "failure" ]; then
echo "message=Deployment failed* :fire_engine:\nThe deployment triggered by *${{ github.actor }}* failed." >> $GITHUB_OUTPUT
echo "text=GitHub Action failed" >> $GITHUB_OUTPUT
elif [ "${{ inputs.deployment-status }}" == "success" ]; then
echo "message=Deployment succeeded* :rocket:\n*${{ github.actor }}* triggered a successful deployment.\nYou can access the deployed app here: <${{ inputs.deployment-url }}>" >> $GITHUB_OUTPUT
echo "text=GitHub Action success" >> $GITHUB_OUTPUT
fi
- name: Notify Slack
uses: slackapi/slack-github-action@v1.27.0
with:
channel-id: ${{ inputs.slack-channel-id }}
payload: |
{
"text": "${{ steps.set-message.outputs.text }}",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*${{ inputs.app-name }} - ${{ steps.set-message.outputs.message }}"
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": ":github: View action"
},
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "View deployments"
},
"url": "https://vercel.com/<organization>/${{ steps.vercel-project.outputs.project-name }}/deployments"
}
]
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ inputs.slack-notifications-webhook-url }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
102 changes: 46 additions & 56 deletions .github/actions/vercel/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,70 @@ name: Deploy to Vercel
description: Deploy the app using Vercel CLI

inputs:
app-directory:
description: 'The directory of the app to be deployed'
required: true
vercel-org-id:
description: 'The Vercel organization ID for the app'
required: true
vercel-project-id:
description: 'The Vercel project ID for the app'
required: true
vercel-token:
description: 'The Vercel authentication token'
app-name:
description: 'The name of the app to deploy.'
required: true
vercel-environment:
description: 'The Vercel environment to deploy to'
description: 'The Vercel environment to deploy to.'
required: true
default: 'preview'
domains:
description: 'The list of domains to alias the deployment, one per line.'
required: true

outputs:
deployment-url:
description: "Vercel's deployment URL."
value: ${{ steps.vercel-deploy.outputs.deployment-url }}

runs:
using: composite
steps:
- name: Validate Vercel environment input
shell: bash
run: |
if [[ "${{ inputs.vercel-environment }}" != "preview" && "${{ inputs.vercel-environment }}" != "production" ]]; then
echo "Invalid vercel-environment value. Must be 'preview' or 'production'."
exit 1
fi
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install global dependencies
shell: bash
run: |
npm install -g pnpm
npm install -g vercel@canary
- name: Setup PNPM
uses: './.github/actions/setup-pnpm'

- name: Change to app directory
- name: Install Vercel CLI
shell: bash
run: cd ${{ inputs.app-directory }}

- name: Install project dependencies
shell: bash
run: pnpm install --frozen-lockfile
run: pnpm install -g vercel@latest

- name: Pull Vercel environment information
shell: bash
env:
VERCEL_ORG_ID: ${{ inputs.vercel-org-id }}
VERCEL_PROJECT_ID: ${{ inputs.vercel-project-id }}
run: vercel pull --yes --environment=${{ inputs.environment }} --token=${{ inputs.vercel-token }}
run: vercel pull --yes --environment=${{ inputs.vercel-environment }} --token=${{ env.VERCEL_TOKEN }}

- name: Build project artifacts
- name: Build and Deploy Project Artifacts to Vercel
shell: bash
env:
VERCEL_ORG_ID: ${{ inputs.vercel-org-id }}
VERCEL_PROJECT_ID: ${{ inputs.vercel-project-id }}
run: |
if [ "${{ inputs.vercel-environment }}" == "production" ]; then
vercel build --prod --token=${{ inputs.vercel-token }}
# Conditionally add --prod flag if vercel-environment is production
if [[ "${{ inputs.vercel-environment }}" == "production" ]]; then
vercel build --prod --token=${{ env.VERCEL_TOKEN }}
vercel deploy --prebuilt --prod --token=${{ env.VERCEL_TOKEN }} > domain.txt
else
vercel build --token=${{ inputs.vercel-token }}
vercel build --token=${{ env.VERCEL_TOKEN }}
vercel deploy --prebuilt --token=${{ env.VERCEL_TOKEN }} > domain.txt
fi
- name: Deploy project artifacts to Vercel
# Alias the deployment to all provided domains
while IFS= read -r domain; do
if [[ -n "$domain" ]]; then # Only process non-empty lines
vercel alias set `cat domain.txt` "$domain" --scope=${{ env.VERCEL_ORG_ID }} --token=${{ env.VERCEL_TOKEN }}
fi
done <<< "${{ inputs.domains }}"
- name: Set deployment URL output
id: vercel-deploy
shell: bash
env:
VERCEL_ORG_ID: ${{ inputs.vercel-org-id }}
VERCEL_PROJECT_ID: ${{ inputs.vercel-project-id }}
run: |
if [ "${{ inputs.vercel-environment }}" == "production" ]; then
vercel deploy --prebuilt --prod --token=${{ inputs.vercel-token }}
else
vercel deploy --prebuilt --token=${{ inputs.vercel-token }}
fi
# Extract the first domain from the list of domains
first_domain=$(echo "${{ inputs.domains }}" | head -n 1)
# Set the deployment URL output using the first domain
echo "deployment-url=https://$first_domain" >> $GITHUB_OUTPUT
- name: Comment pull request with deployment URL
if: ${{ github.event_name == 'pull_request' }}
uses: thollander/actions-comment-pull-request@v3
with:
message: |
**${{ inputs.app-name }}** - Deployment succeeded :rocket:
You can access the deployed app here: ${{ steps.vercel-deploy.outputs.deployment-url }}
comment-tag: $${{ steps.vercel-deploy.outputs.deployment-url }}
20 changes: 20 additions & 0 deletions .github/workflows/contentful-webhook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Contentful Webhook - Redeploy apps

on:
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
unified-web-app-preview-deployment:
name: Deploy Unified-web App (Vercel Preview)
uses: ./.github/workflows/reusable-vercel-deploy.yml
with:
app-name: 'Unified-web App'
github-environment: 'dev'
vercel-environment: 'preview'
domains: |
${{ needs.get-pr-number.outputs.pr-number }}-deployment-domain.vercel.app
secrets: inherit
66 changes: 0 additions & 66 deletions .github/workflows/ignore.yml

This file was deleted.

Loading

0 comments on commit 6606cfe

Please sign in to comment.