-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ci): Automate Docker image publication when merging on master #1142
Changes from 3 commits
60c8079
350ee6c
412d097
ae34a18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ cypress/ | |
.git/ | ||
.github/ | ||
.githooks/ | ||
.circleci/ | ||
.husky/ | ||
.nodemon.json | ||
.editorconfig | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
name: Publish | ||
run-name: 🚀 Commit on ${{ github.ref_name }} | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
validate: | ||
name: 🤖 Validate | ||
uses: ./.github/workflows/validate.yml | ||
publish: | ||
name: 📦 Publish | ||
needs: validate | ||
runs-on: ubuntu-latest | ||
permissions: | ||
# To commit on the repo and add the tag | ||
contents: write | ||
# To publish the Docker image to GitHub Packages | ||
packages: write | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
steps: | ||
- name: 📁 Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: ⚙️ Setup node | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: '18.18' | ||
cache: 'yarn' | ||
|
||
- name: 📦 Install dependencies | ||
run: yarn install --frozen-lockfile | ||
|
||
- name: ❓ Check if a new version needs to be published | ||
id: publish-check | ||
continue-on-error: true | ||
run: yarn publish:check | ||
|
||
- name: 🆙 Bump version, tag commit, publish GitHub Release | ||
if: ${{ steps.publish-check.outcome == 'success' }} | ||
run: yarn publish:github | ||
|
||
- name: 🐋 Publish Docker image | ||
if: ${{ steps.publish-check.outcome == 'success' }} | ||
run: yarn publish:docker |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-disable import/no-commonjs */ | ||
/* eslint-disable no-console */ | ||
/* eslint-disable no-process-exit */ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const semanticRelease = require('semantic-release'); | ||
const { WritableStreamBuffer } = require('stream-buffers'); | ||
|
||
(async () => { | ||
console.log('Analyzing commits since last version...'); | ||
// Execute semantic-release with only the commit-analyzer step, to see if | ||
// a new release is needed | ||
const { nextRelease } = await semanticRelease( | ||
{ | ||
dryRun: true, | ||
plugins: ['@semantic-release/commit-analyzer'], | ||
verifyConditions: [], | ||
analyzeCommits: ['@semantic-release/commit-analyzer'], | ||
verifyRelease: [], | ||
generateNotes: [], | ||
prepare: [], | ||
publish: [], | ||
addChannel: [], | ||
success: [], | ||
fail: [], | ||
}, | ||
// Redirect output to new streams, to make the script silent | ||
{ | ||
stdout: new WritableStreamBuffer(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as we're not using them, I wonder if a builtin writable stream works too? no big deal though, but could avoid the dependency There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not familiar enough with streams to have found a builtin writable one. I went with the same dependency as the one actually used in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be done with: import {Writable} from 'node:stream'
new Writable({write(_chunk, _encoding, callback) { setImmediate(callback) }}) (but obviously this dep is also fine) |
||
stderr: new WritableStreamBuffer(), | ||
} | ||
); | ||
|
||
// Exit with 0 if a new version must be released, 1 if nothing to do | ||
if (nextRelease?.version) { | ||
console.log( | ||
`Commits analyzed warrant a release of version ${nextRelease.version}` | ||
); | ||
process.exit(0); | ||
} | ||
console.log('No new version to publish'); | ||
process.exit(1); | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/bin/sh | ||
# Publish the project on GitHub Packages | ||
# See: https://github.com/algolia/npm-search/pkgs/container/npm-search | ||
# | ||
# This script will be automatically run from GitHub Actions on each commits on | ||
# the main branch that warrants a release (ie. feat() and fix() commits). | ||
# | ||
# You can also run the script locally, but you'll need a GITHUB_TOKEN with the | ||
# write:packages scope. | ||
# See: https://github.com/settings/tokens | ||
set -e | ||
|
||
# Get version from package.json | ||
version=$(node -e "console.log(require('./package.json').version)") | ||
echo "Publishing: $version" | ||
echo "" | ||
|
||
# Build the image | ||
docker build \ | ||
--platform linux/amd64 \ | ||
--label "org.opencontainers.image.source=https://github.com/algolia/npm-search" \ | ||
--tag "ghcr.io/algolia/npm-search" \ | ||
--tag "ghcr.io/algolia/npm-search:${version}" \ | ||
. | ||
|
||
# Login to ghcr.io | ||
echo "${GITHUB_TOKEN}" | | ||
docker login ghcr.io \ | ||
--username $ \ | ||
--password-stdin | ||
|
||
# Push the image | ||
docker push "ghcr.io/algolia/npm-search" | ||
docker push "ghcr.io/algolia/npm-search:${version}" | ||
|
||
# Output | ||
echo "Version $version published" | ||
echo "https://github.com/algolia/npm-search/pkgs/container/npm-search" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/sh | ||
# Publish a new version on GitHub, including: | ||
# - Update package.json and CHANGELOG.md with new version and changes | ||
# - Tag the commit with the version number | ||
# - Release the source code on GitHub Releases (https://github.com/algolia/npm-search/releases) | ||
# | ||
# This script doesn't do anything if there is no new version to publish | ||
set -e | ||
|
||
yarn run semantic-release |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could just be:
and the file removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. I initially had the external script because there was a bit more code before and after, but you're right that it can now be simplified