Skip to content

Commit

Permalink
Merge pull request #17 from MarkoSagadin/feature/ci
Browse files Browse the repository at this point in the history
Added support for CI, drastically improving DEVX
  • Loading branch information
Mochitto committed Jan 21, 2024
2 parents 53175e3 + 44be151 commit febd999
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 8 deletions.
18 changes: 18 additions & 0 deletions .github/label_pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Label PR
on:
pull_request:
types: [opened]

permissions:
contents: read

jobs:
pr-labeler:
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-22.04
steps:
- uses: TimonVS/pr-labeler-action@v4.1.1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions .github/pr-labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Add lables (on the left) to the PRs of specific branches (on the right)
pull request: feature/*
release: release/*
fix: fix/*
94 changes: 94 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Build

on:
workflow_call:
inputs:
checkout_ref:
required: true
type: string
push:
branches:
- "main"
pull_request:
types: [opened, reopened, synchronize]

jobs:
build-backend:
runs-on: ubuntu-22.04

steps:
- name: Checkout last PR commit
if: github.event_name == 'pull_request'
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Checkout last tag
if: github.event_name == 'workflow_dispatch'
uses: actions/checkout@v3
with:
ref: ${{ inputs.checkout_ref }}

- name: Checkout main
if: github.event_name == 'push'
uses: actions/checkout@v3
with:
ref: main

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: "pip"

- name: Install dependencies
run: make backend-install

- name: Run tests
run: make backend-test

- name: Build binary wheel and a source tarball
run: make backend-build

- name: Package artefacts
if: github.event_name == 'workflow_dispatch'
uses: actions/upload-artifact@v3
with:
name: dist
path: dist/*

build-frontend:
runs-on: ubuntu-22.04
strategy:
matrix:
node-version: ["14.x", "16.x", "18.x"]

steps:
- name: Checkout last PR commit
if: github.event_name == 'pull_request'
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Checkout last tag
if: github.event_name == 'workflow_dispatch'
uses: actions/checkout@v3
with:
ref: ${{ inputs.checkout_ref }}

- name: Checkout main
if: github.event_name == 'push'
uses: actions/checkout@v3
with:
ref: main

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

- name: Install dependencies
run: make frontend-install

- name: Build
run: make frontend-build
87 changes: 87 additions & 0 deletions .github/workflows/create-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: "Create Release"

on:
workflow_dispatch:
inputs:
version:
description:
"The version you want to release [v##.##.##]? (BTW, did you update
changelog?)"
required: true
env:
GIT_TERMINAL_PROMPT: 0

jobs:
update-changelog:
runs-on: ubuntu-22.04
steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
# Fetch history for all branches and tags
fetch-depth: 0

- name: Validate version input
id: validate-input
run: |
# Check if input version is in correct format
if [[ ! ${{ inputs.version }} =~ v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "::error::Invalid input version: wrong format!"
exit 1
fi
# Check if input version already exists as git tag
if [[ ! -z $(git tag | grep ${{ inputs.version }}) ]]; then
echo "::error::Invalid input version: it already exists!"
exit 1
fi
- name: Update Changelog
uses: thomaseizinger/keep-a-changelog-new-release@ec0871fec381db64ab389886ffe8e6fc2b661e2c
with:
tag: ${{ inputs.version }}

- name: Commit Changelog, create tag and push
run: |
# In order to make a commit, we need to initialize a user.
git config user.name "github-bot :robot:"
git config user.email noreply@github.com
git add CHANGELOG.md
git commit -m "Update CHANGELOG.md for Release ${{ inputs.version }}"
git tag ${{ inputs.version }}
git push
git push origin ${{ inputs.version }}
call-build:
needs: update-changelog
uses: ./.github/workflows/build.yaml
with:
checkout_ref: ${{ inputs.version }}

call-publish-release:
needs: call-build
uses: ./.github/workflows/publish-release.yaml
with:
release_version: ${{ inputs.version }}

cleanup-on-failure:
# Only run cleanup if either call-build or call-publish-release fail.
needs: [call-build, call-publish-release]
if: ${{ always() && contains(join(needs.*.result, ','), 'failure') }}
runs-on: ubuntu-22.04

steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
ref: ${{ inputs.version }}
# Fetch two commits, so you can hard reset below
fetch-depth: 2

- name: Cleanup tag and Changelog
run: |
git config user.name "github-bot :robot:"
git config user.email noreply@github.com
git reset --hard HEAD~1
git push --force origin HEAD:main
git tag -d ${{ inputs.version }}
git push --delete origin ${{ inputs.version }}
59 changes: 59 additions & 0 deletions .github/workflows/publish-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: "Publish Release"

on:
workflow_call:
inputs:
release_version:
required: true
type: string

jobs:
publish-new-release:
runs-on: ubuntu-22.04
environment:
name: pypi
url: https://pypi.org/project/markdown2anki/
permissions:
# IMPORTANT: this permissions are mandatory for trusted publishing with
# OIDC.
id-token: write
contents: write

steps:
- name: Preprocess version tag
run: |
# This is needed due to the mindsers/changelog-reader-action expecting
# version tag without 'v' prefix.
version_cut=$(echo "${{ inputs.release_version }}" | cut -c 2-)
echo "release_version=${{ inputs.release_version }}" >> $GITHUB_ENV
echo "release_version_cut=$version_cut" >> $GITHUB_ENV
- name: Checkout Repository
uses: actions/checkout@v3
with:
ref: ${{ env.release_version }}

- name: Get latest Changelog entry
id: changelog-reader
uses: mindsers/changelog-reader-action@v2.2.2
with:
version: ${{ env.release_version_cut }}

- name: Download artifact
uses: actions/download-artifact@v3
with:
name: dist
path: dist

- name: Publish distribution to PyPI
uses: pypa/gh-action-pypi-publish@release/v1

- name: Publish Release
uses: softprops/action-gh-release@v0.1.15
with:
files: dist/*
tag_name: ${{ env.release_version }}
body: |
# Release notes
${{ steps.changelog-reader.outputs.changes }}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

### Added
- Cards can now have metadata! TODO: link to readme header explaining metadata
- Improved developer experience by creating a Makefile with commonly used development commands. Its use is described in the "CONTRIBUTING.md" document. (@MarkoSagadin)
- Support for continuous integration with GitHub Actions. Backend and frontend will now build with every push to the `main` branch or to the opened PR. (@MarkoSagadin)
- Automated release process with GitHub Actions. A new release can now be manually triggered by providing the next version tag under the _Actions_ tab in the GitHub Web UI. (@MarkoSagadin)

### Fixed
- When reviewing card styles in Anki from its editor view, night mode now triggers the night-mode styles as expected. (@MarkoSagadin)
Expand Down
Loading

0 comments on commit febd999

Please sign in to comment.