Skip to content

Commit

Permalink
ci: implement PR driven release workflow
Browse files Browse the repository at this point in the history
refs #345
  • Loading branch information
ygrishajev committed Dec 11, 2024
1 parent 52b6cc6 commit c45835c
Show file tree
Hide file tree
Showing 14 changed files with 424 additions and 299 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Build Image

on:
workflow_dispatch:
inputs:
tag:
description: 'Tag to build'
required: true
type: string
deployment-env:
description: 'Deployment environment'
type: choice
options:
- staging
- production
workflow_call:
inputs:
tag:
description: 'Tag to build'
required: true
type: string
deployment-env:
description: 'Deployment environment'
type: string

concurrency:
group: ${{ github.workflow }}

permissions:
contents: write
packages: write

jobs:
build:
name: Build Docker image
runs-on: ubuntu-latest

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

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Determine registry and app
id: determine
run: |
tag=${{ inputs.tag }}
force_build=""
if [[ "$tag" == console-api/* ]]; then
echo "registry=${{ vars.API_REGISTRY }}" >> $GITHUB_ENV
echo "app=api" >> $GITHUB_ENV
elif [[ "$tag" == console-web/* ]]; then
echo "registry=${{ vars.WEB_REGISTRY }}" >> $GITHUB_ENV
echo "app=deploy-web" >> $GITHUB_ENV
echo "force_build=-f" >> $GITHUB_ENV
else
echo "Error: Unknown tag format"
exit 1
fi
tag="${tag#*/}"
tag="${tag#v}"
echo "tag=$tag" >> $GITHUB_ENV
- name: Build Docker image
env:
DEPLOYMENT_ENV: ${{ inputs.deployment-env }}
run: |
./packages/docker/script/build.sh -r ${{ env.registry }} -t ${{ env.tag }} -a ${{ env.app }} ${{ env.force_build }}
84 changes: 84 additions & 0 deletions .github/workflows/create-github-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Create GitHub Release

on:
workflow_call:
inputs:
app:
description: 'The app to release'
required: true
type: string
outputs:
version:
description: 'The version released'
value: ${{ jobs.release.outputs.version }}

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

permissions:
contents: write

jobs:
release:
name: Create GitHub Release
runs-on: ubuntu-latest

outputs:
version: ${{ steps.bumps.outputs.version }}

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Get Version and Changelog Updates
id: bumps
run: |
package_file="apps/${{ inputs.app }}/package.json"
if [ ! -f "$package_file" ]; then
echo "Error: Package file $package_file does not exist."
exit 1
fi
current_version=$(jq -r '.version' "$package_file")
git_tag=$current_version
if [ "${{ inputs.app }}" = "deploy-web" ]; then
git_tag="console-web/v$git_tag"
elif [ "${{ inputs.app }}" = "api" ]; then
git_tag="console-api/v$git_tag"
else
echo "Error: Unsupported app type '${{ inputs.app }}'."
exit 1
fi
has_tag=$(git rev-parse "$git_tag" >/dev/null 2>&1 && echo "true" || echo "false")
if [ "$has_tag" = "false" ]; then
echo "version=$git_tag" >> $GITHUB_OUTPUT
echo "version=$git_tag"
changelog=$(script/extract-changelog.sh "$current_version" "apps/${{ inputs.app }}/CHANGELOG.md")
if [ -n "$changelog" ]; then
echo "changelog=$changelog"
echo "changelog<<EOF" >> $GITHUB_OUTPUT
echo "$changelog" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi
fi
- name: Create Release
if: ${{ steps.bumps.outputs.version != '' }}
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.bumps.outputs.version }}
release_name: ${{ steps.bumps.outputs.version }}
body: ${{ steps.bumps.outputs.changelog }}
prerelease: true
92 changes: 92 additions & 0 deletions .github/workflows/create-pre-release-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: Create Pre-Release PR

on:
push:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}

env:
NODE_VERSION: 20.14.0
GITHUB_EMAIL: "[email protected]"
GITHUB_NAME: "CI"

jobs:
check-releasability:
name: Check if is Releasable
runs-on: ubuntu-latest
outputs:
is-releasable: ${{ steps.check.outputs.is-releasable }}
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Check if is Releasable
id: check
run: |
last_commit=$(git log -1 --pretty=%B)
if [[ $last_commit == "chore(release): released version"* ]]; then
echo "is-releasable=false" >> $GITHUB_OUTPUT
else
echo "is-releasable=true" >> $GITHUB_OUTPUT
fi
create-pr:
name: Create Pre-Release PR with Updated Changelogs and Versions
needs: check-releasability
if: needs.check-releasability.outputs.is-releasable == 'true'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}

- name: Restore root node_modules cache
uses: actions/cache@v4
id: cache
with:
path: |
node_modules
apps/api/node_modules
apps/deploy-web/node_modules
packages/*/node_modules
key: common-${{ runner.os }}-${{ hashFiles('package-lock.json') }}

- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: npm ci -w packages/releaser

- name: Generate releases and build docker images
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config --global user.email "${{ env.GITHUB_EMAIL }}"
git config --global user.name "${{ env.GITHUB_NAME }}"
npm run release -w apps/api -- --verbose --ci
npm run release -w apps/deploy-web -- --verbose --ci
- name: Cleanup Previous Release Branch
run: |
git branch -D release/bumps || true
- name: Commit and Create PR
uses: peter-evans/create-pull-request@v7
with:
token: '${{ github.token }}'
branch: release/bumps
base: main
title: "Release bumps"
body: "This is an automated PR to update the changelogs and versions for the next release. Merging it will trigger release adn build workflows"
84 changes: 0 additions & 84 deletions .github/workflows/release-all-apps.yml

This file was deleted.

27 changes: 27 additions & 0 deletions .github/workflows/release-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release API

on:
push:
branches:
- main
paths:
- 'apps/api/package.json'

jobs:
release:
name: Create Release
uses: ./.github/workflows/create-github-release.yml
secrets: inherit
with:
app: api

build:
needs: release
name: Build Docker image
uses: ./.github/workflows/build-image.yml
secrets: inherit
permissions:
contents: write
packages: write
with:
tag: ${{ needs.release.outputs.version }}
Loading

0 comments on commit c45835c

Please sign in to comment.