Skip to content

Commit

Permalink
ci: Automate the release process (#596)
Browse files Browse the repository at this point in the history
This is essentially the same thing that we do in apify/apify-sdk-python,
apify/crawlee-python, etc., but for Javascript.

---------

Co-authored-by: Apify Release Bot <[email protected]>
  • Loading branch information
janbuchar and Apify Release Bot authored Nov 1, 2024
1 parent d07452b commit 37f3127
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 64 deletions.
103 changes: 103 additions & 0 deletions .github/workflows/pre_release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Create a pre-release

on:
# Push to master will deploy a beta version
push:
branches:
- master
tags-ignore:
- "**" # Ignore all tags to prevent duplicate builds when tags are pushed.

concurrency:
group: release
cancel-in-progress: false

jobs:
release_metadata:
if: "!startsWith(github.event.head_commit.message, 'docs') && !startsWith(github.event.head_commit.message, 'ci') && startsWith(github.repository, 'apify/')"
name: Prepare release metadata
runs-on: ubuntu-latest
outputs:
version_number: ${{ steps.release_metadata.outputs.version_number }}
changelog: ${{ steps.release_metadata.outputs.changelog }}
steps:
- uses: apify/workflows/git-cliff-release@main
name: Prepare release metadata
id: release_metadata
with:
release_type: prerelease
existing_changelog_path: CHANGELOG.md

wait_for_checks:
name: Wait for code checks to pass
runs-on: ubuntu-latest
steps:
- uses: lewagon/[email protected]
with:
ref: ${{ github.ref }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
check-regexp: (Build & Test .*|Lint|Docs build)
wait-interval: 5

update_changelog:
needs: [ release_metadata, wait_for_checks ]
name: Update changelog
runs-on: ubuntu-latest
outputs:
changelog_commitish: ${{ steps.commit.outputs.commit_long_sha || github.sha }}

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}

- name: Use Node.js 22
uses: actions/setup-node@v4
with:
node-version: 22

- name: Update package version in package.json
run: npm version --no-git-tag-version --allow-same-version ${{ needs.release_metadata.outputs.version_number }}

- name: Update CHANGELOG.md
uses: DamianReeves/write-file-action@master
with:
path: CHANGELOG.md
write-mode: overwrite
contents: ${{ needs.release_metadata.outputs.changelog }}

- name: Commit changes
id: commit
uses: EndBug/add-and-commit@v9
with:
author_name: Apify Release Bot
author_email: [email protected]
message: "chore(release): Update changelog and package version [skip ci]"

publish_to_npm:
name: Publish to NPM
needs: [ release_metadata, wait_for_checks ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.update_changelog.changelog_commitish }}
- name: Use Node.js 22
uses: actions/setup-node@v4
with:
node-version: 22
cache: 'npm'
cache-dependency-path: 'package-lock.json'
- name: Install dependencies
run: |
echo "access=public" >> .npmrc
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc
npm ci
- # Check version consistency and increment pre-release version number for beta only.
name: Bump pre-release version
run: node ./.github/scripts/before-beta-release.js
- name: Build module
run: npm run build
- name: Publish to NPM
run: npm publish --tag beta
155 changes: 93 additions & 62 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,103 +1,134 @@
name: Check & Release Beta
name: Create a release

on:
# Push to master will deploy a beta version
push:
branches:
- master
# This will allow releasing beta versions from non-master releases.
# TODO: We need to release this version with other tags than beta. It can mess up the beta version as it will override it with older code.
- version/**
# A release via GitHub releases will deploy a latest version
release:
types: [ published ]
# Trigger a stable version release via GitHub's UI, with the ability to specify the type of release.
workflow_dispatch:
inputs:
release_type:
description: Release type
required: true
type: choice
default: auto
options:
- auto
- custom
- patch
- minor
- major
custom_version:
description: The custom version to bump to (only for "custom" type)
required: false
type: string
default: ""

concurrency:
group: release
cancel-in-progress: false

jobs:
build_and_test:
name: Build & Test
if: ${{ !contains(github.event.head_commit.message, '[skip ci]') }}
release_metadata:
name: Prepare release metadata
runs-on: ubuntu-latest
outputs:
version_number: ${{ steps.release_metadata.outputs.version_number }}
tag_name: ${{ steps.release_metadata.outputs.tag_name }}
changelog: ${{ steps.release_metadata.outputs.changelog }}
release_notes: ${{ steps.release_metadata.outputs.release_notes }}
steps:
- uses: apify/workflows/git-cliff-release@main
name: Prepare release metadata
id: release_metadata
with:
release_type: ${{ inputs.release_type }}
custom_version: ${{ inputs.custom_version }}
existing_changelog_path: CHANGELOG.md

strategy:
fail-fast: false
matrix:
node-version: [ 16, 18, 20 ]

wait_for_checks:
name: Wait for code checks to pass
runs-on: ubuntu-latest
steps:
- name: Cancel Workflow Action
uses: styfle/[email protected]
- uses: lewagon/[email protected]
with:
access_token: ${{ github.token }}
ref: ${{ github.ref }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
check-regexp: (Build & Test .*|Lint|Docs build)
wait-interval: 5

update_changelog:
needs: [ release_metadata, wait_for_checks ]
name: Update changelog
runs-on: ubuntu-latest
outputs:
changelog_commitish: ${{ steps.commit.outputs.commit_long_sha || github.sha }}

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}

- name: Use Node.js ${{ matrix.node-version }}
- name: Use Node.js 22
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache-dependency-path: 'package-lock.json'
node-version: 22

- name: Install Dependencies
run: npm ci
- name: Update package version in package.json
run: npm version --no-git-tag-version --allow-same-version ${{ needs.release_metadata.outputs.version_number }}

- name: Run Tests
run: npm test
- name: Update CHANGELOG.md
uses: DamianReeves/write-file-action@master
with:
path: CHANGELOG.md
write-mode: overwrite
contents: ${{ needs.release_metadata.outputs.changelog }}

lint:
name: Lint
runs-on: ubuntu-latest
- name: Commit changes
id: commit
uses: EndBug/add-and-commit@v9
with:
author_name: Apify Release Bot
author_email: [email protected]
message: "chore(release): Update changelog and package version [skip ci]"

create_github_release:
name: Create github release
needs: [release_metadata, update_changelog]
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4
- name: Use Node.js 20
uses: actions/setup-node@v4
- name: Create release
uses: softprops/action-gh-release@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache-dependency-path: 'package-lock.json'
- run: npm ci
- run: npm run lint

tag_name: ${{ needs.release_metadata.outputs.tag_name }}
name: ${{ needs.release_metadata.outputs.version_number }}
target_commitish: ${{ needs.update_changelog.outputs.changelog_commitish }}
body: ${{ needs.release_metadata.outputs.release_notes }}

# The deploy job is long but there are only 2 important parts. NPM publish
# and triggering of docker image builds in the apify-actor-docker repo.
deploy:
publish_to_npm:
name: Publish to NPM
needs: [ build_and_test, lint ]
needs: [ update_changelog ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js 20
with:
ref: ${{ needs.update_changelog.changelog_commitish }}
- name: Use Node.js 22
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
node-version: 22
cache: 'npm'
cache-dependency-path: 'package-lock.json'
- name: Install dependencies
run: |
echo "access=public" >> .npmrc
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc
npm ci
- # Determine if this is a beta or latest release
name: Set Release Tag
run: echo "RELEASE_TAG=$(if [ ${{ github.event_name }} = release ]; then echo latest; else echo beta; fi)" >> $GITHUB_ENV
- # Check version consistency and increment pre-release version number for beta only.
name: Bump pre-release version
if: env.RELEASE_TAG == 'beta'
run: node ./.github/scripts/before-beta-release.js
- name: Build module
run: npm run build
- name: Publish to NPM
run: npm publish --tag ${{ env.RELEASE_TAG }}
- # Latest version is tagged by the release process so we only tag beta here.
name: Tag Version
if: env.RELEASE_TAG == 'beta'
run: |
git_tag=v`node -p "require('./package.json').version"`
git tag $git_tag
git push origin $git_tag
run: npm publish --tag latest

env:
NODE_AUTH_TOKEN: ${{ secrets.APIFY_SERVICE_ACCOUNT_NPM_TOKEN }}
Expand Down
Loading

0 comments on commit 37f3127

Please sign in to comment.