Skip to content

Commit

Permalink
add github scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
seljabali committed Mar 29, 2024
1 parent 295dd7c commit 61ba58d
Show file tree
Hide file tree
Showing 13 changed files with 462 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @seljabali
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: monthly
- package-ecosystem: npm
directory: "/"
schedule:
interval: monthly
open-pull-requests-limit: 4
7 changes: 7 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### 💬 Description


### 🔗 Github Issues Link
https://github.com/eTipio/dashboard-issues/issues/

### 🌉 Screenshots
11 changes: 11 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
changelog:
categories:
- title: ✨ Enhancements
labels:
- '*'
exclude:
labels:
- dependencies
- title: 🔨 Dependencies
labels:
- dependencies
33 changes: 33 additions & 0 deletions .github/scripts/calculate_next_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys
from datetime import datetime
import subprocess
import re

def get_latest_tag():
tags = subprocess.check_output(['git', 'tag', '--sort=-creatordate']).decode().strip().split('\n')
valid_tags = [tag for tag in tags if re.match(r'\d{4}\.\d{2}\.\d{2}', tag)]
return valid_tags[0] if valid_tags else None

def calculate_next_tag():
now = datetime.utcnow()
year, week, _ = now.isocalendar()
week_str = f"{week:02d}" # Ensure week is two digits

latest_tag = get_latest_tag()
if latest_tag:
latest_year, latest_week, latest_release = map(int, latest_tag.split('.'))
if latest_year == year and f"{latest_week:02d}" == week_str:
next_release = latest_release + 1
else:
next_release = 1
else:
next_release = 1

# Here's the critical part ensuring the release number has a leading zero
next_release_str = f"{next_release:02d}"

next_tag = f"{year}.{week_str}.{next_release_str}"
return next_tag

if __name__ == "__main__":
print(calculate_next_tag())
31 changes: 31 additions & 0 deletions .github/scripts/generate_release_notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import subprocess
import re

def get_commits_since_last_tag():
try:
latest_tag = subprocess.check_output(['git', 'describe', '--tags', '--abbrev=0'], stderr=subprocess.DEVNULL).decode().strip()
# print(f"Latest tag: {latest_tag}") # Debug print
except subprocess.CalledProcessError:
latest_tag = ""
print("No tags found.") # Debug print

commit_logs = subprocess.check_output(['git', 'log', f'{latest_tag}..HEAD', '--pretty=format:%H -- %an -- %s']).decode().strip().split('\n')
# print(f"Commit logs since last tag: {commit_logs}") # Debug print
return commit_logs

def generate_release_notes():
notes = "## ✨ Enhancements\n"
commit_logs = get_commits_since_last_tag()
if commit_logs:
for log in commit_logs:
hash, author, message = log.split(' -- ')
# Attempt to extract PR number from commit message
pr_match = re.search(r'\(#(\d+)\)', message)
pr_number = pr_match.group(1) if pr_match else ''
notes += f"* {message} by {author}\n"
else:
notes += "No changes were made since the last release.\n"
return notes

if __name__ == "__main__":
print(generate_release_notes())
42 changes: 42 additions & 0 deletions .github/workflows/build-dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# https://github.com/ahmadnassri/action-dependabot-auto-merge

name: Dependabot-Build-AutoMerge

on:
push:
branches: [ develop ]
pull_request_target:
types: [opened, synchronize, reopened, ready_for_review]

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
matrix:
node-version: [20.x]

# If the PR is coming from a fork (pull_request_target), ensure it's opened by "dependabot[bot]".
# Otherwise, clone it normally. Source: https://hugo.alliau.me/2021/05/04/migration-to-github-native-dependabot-solutions-for-auto-merge-and-action-secrets/#share-your-secrets-with-dependabot
if: |
(github.event_name == 'pull_request_target' && github.actor == 'dependabot[bot]')
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: NpmRcGen
run: |
echo "//npm.pkg.github.com/:_authToken=${{ secrets.NODE_AUTH_TOKEN }}" > .npmrc
echo "@etipio:registry=https://npm.pkg.github.com/" >> .npmrc
- name: Install Yarn
run: npm i yarn -g

- name: Build
run: yarn install && yarn build-staging
43 changes: 43 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Build

on:
push:
branches: [ develop ]
pull_request:
types: [opened, synchronize, reopened, ready_for_review]

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
matrix:
node-version: [20.x]

# If the PR is coming from a fork (pull_request_target), ensure it's opened by "dependabot[bot]".
# Otherwise, clone it normally. Source: https://hugo.alliau.me/2021/05/04/migration-to-github-native-dependabot-solutions-for-auto-merge-and-action-secrets/#share-your-secrets-with-dependabot
if: |
(github.event_name == 'pull_request' && github.actor != 'dependabot[bot]')
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: NpmRcGen
run: |
echo "//npm.pkg.github.com/:_authToken=${{ secrets.NODE_AUTH_TOKEN }}" > .npmrc
echo "@etipio:registry=https://npm.pkg.github.com/" >> .npmrc
- name: Install Yarn
run: npm i yarn -g

- name: Install Packages
run: yarn install

- name: Build
run: yarn build-staging
33 changes: 33 additions & 0 deletions .github/workflows/create-new-tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Create Tag

on:
workflow_dispatch:

jobs:
tag-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history so we can get all tags

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install Python dependencies
run: pip install PyYAML # Assuming PyYAML is a dependency for your scripts

- name: Calculate next tag
id: next-tag
run: echo "::set-output name=tag::$(python .github/scripts/calculate_next_tag.py)"
shell: bash

- name: Create the new tag
run: |
git config user.name github-actions
git config user.email [email protected]
git tag ${{ steps.next-tag.outputs.tag }}
git push origin ${{ steps.next-tag.outputs.tag }}
67 changes: 67 additions & 0 deletions .github/workflows/create-tag-and-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Create Tag & Release

on:
workflow_dispatch:

jobs:
tag-and-release:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Necessary for scripts to access all tags and .github/release.yml

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install dependencies
run: pip install PyYAML

- name: Calculate next tag
id: next-tag
run: echo "::set-output name=tag::$(python .github/scripts/calculate_next_tag.py)"
shell: bash

- name: Generate release notes v1
id: generate-notes-v1
run: echo "::set-output name=notes::$(python .github/scripts/generate_release_notes.py .github/release.yml)"
shell: bash

- name: Generate release notes v2
id: generate-notes-v2
run: |
NOTES=$(python .github/scripts/generate_release_notes.py | jq -aRs .)
echo "::set-output name=notes::$NOTES"
shell: bash
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Generate release notes v3
id: generate-notes
run: |
NOTES=$(python .github/scripts/generate_release_notes.py)
NOTES="${NOTES//'%'/'%25'}"
NOTES="${NOTES//$'\n'/'%0A'}"
NOTES="${NOTES//$'\r'/'%0D'}"
echo "::set-output name=notes::$NOTES"
- name: Create the new tag
run: |
git config user.name github-actions
git config user.email [email protected]
git tag ${{ steps.next-tag.outputs.tag }}
git push origin ${{ steps.next-tag.outputs.tag }}
- name: Create Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.next-tag.outputs.tag }}
release_name: ${{ steps.next-tag.outputs.tag }}
body: ${{ steps.generate-notes.outputs.notes }}
draft: false
prerelease: false
96 changes: 96 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: Deploy

on:
workflow_dispatch:
inputs:
deploy_environment:
description: "Environment to deploy"
required: true
default: 'staging'
options:
- staging
- prod
push:
branches: [ develop ]

jobs:
deploy:
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
matrix:
node-version: [20.x]
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: npmrcgen
run: |
echo "//npm.pkg.github.com/:_authToken=${{ secrets.NODE_AUTH_TOKEN }}" > .npmrc
echo "@etipio:registry=https://npm.pkg.github.com/" >> .npmrc
- name: Install Yarn
run: npm i yarn -g

- name: Install Dependencies
run: |
yarn install
- name: Build Staging
if: (github.event.inputs.deploy_environment == null) || (github.event.inputs.deploy_environment == 'staging')
run: |
yarn build-staging
- name: Build Prod
if: github.event.inputs.deploy_environment == 'prod'
run: |
yarn build-production
- name: Publish to Staging
if: (github.event.inputs.deploy_environment == null) || (github.event.inputs.deploy_environment == 'staging')
run: |
aws s3 cp --recursive --acl public-read ./build s3://etip-dashboard-staging-deploybucket-1v8nys1pq3bkd/
aws s3 cp --acl public-read --cache-control="max-age=0, no-cache, no-store, must-revalidate" ./build/index.html s3://etip-dashboard-staging-deploybucket-1v8nys1pq3bkd/
aws cloudfront create-invalidation --distribution-id E2D63IYQCIN8NK --paths /index.html /sw.js /manifest.webmanifest
- name: Publish to Production
if: github.event.inputs.deploy_environment == 'prod'
run: |
aws s3 cp --recursive --acl public-read ./build s3://etip-dashboard-prod-12jkcoiyba/
aws s3 cp --acl public-read --cache-control="max-age=0, no-cache, no-store, must-revalidate" ./build/index.html s3://etip-dashboard-prod-12jkcoiyba/
aws cloudfront create-invalidation --distribution-id ELADF4BMR76L2 --paths /index.html /sw.js /manifest.webmanifest
notify:
needs: deploy
if: ${{ always() }}
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- name: Slack Notification to staging
if: ${{ (github.event.inputs.deploy_environment == null) || (github.event.inputs.deploy_environment == 'staging') }}
uses: slackapi/slack-github-action@v1
with:
payload: |
{ "text": "etip-dashboard\n\n env: staging\n deploy result: ${{ needs.build.result }}\n ${{ github.event.head_commit.url }}" }
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_STAGING }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
- name: Slack Notification to production
if: ${{ github.event.inputs.deploy_environment == 'prod' }}
uses: slackapi/slack-github-action@v1
with:
payload: |
{ "text": "etip-dashboard\n\n env: prod\n deploy result: ${{ needs.build.result }}\n ${{ github.event.head_commit.url }}" }
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_PROD }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
Loading

0 comments on commit 61ba58d

Please sign in to comment.