|
1 | 1 | const execa = require('execa')
|
| 2 | +const semver = require('semver') |
2 | 3 |
|
3 | 4 | /**
|
4 | 5 | * @typedef {import('./index').Context} Context
|
5 | 6 | */
|
6 | 7 | /**
|
7 |
| - * Push new docker image. |
| 8 | + * Tag and push new docker images with semantic versioning. |
8 | 9 | *
|
9 | 10 | * @param {Object} pluginConfig - The plugin configuration.
|
10 | 11 | * @param {Context} context - The semantic-release context.
|
11 | 12 | * @returns {Promise} A `Promise` that resolve to docker push command.
|
12 | 13 | */
|
13 | 14 | module.exports = async (pluginConfig, { nextRelease: { version }, logger }) => {
|
14 |
| - try { |
| 15 | + /** |
| 16 | + * Tag and push docker image by version. |
| 17 | + * |
| 18 | + * @param {string} version - Tag version. |
| 19 | + * @returns {Promise} A `Promise` that resolve to docker push command. |
| 20 | + */ |
| 21 | + async function publish (version) { |
15 | 22 | logger.log(
|
16 | 23 | `Pushing version ${
|
17 | 24 | process.env.CI_REGISTRY_IMAGE
|
18 | 25 | }:${version} to GitLab Container Registry`
|
19 | 26 | )
|
20 |
| - |
21 |
| - // Push both new version and latest |
| 27 | + // tag image |
22 | 28 | await execa(
|
23 | 29 | 'docker',
|
24 |
| - [ |
25 |
| - 'tag', |
26 |
| - `${process.env.CI_REGISTRY_IMAGE}:latest`, |
27 |
| - `${process.env.CI_REGISTRY_IMAGE}:${version}` |
28 |
| - ], |
| 30 | + ['tag', `${process.env.CI_REGISTRY_IMAGE}:${version}`], |
29 | 31 | {
|
30 | 32 | stdio: 'inherit'
|
31 | 33 | }
|
32 | 34 | )
|
| 35 | + // push image |
33 | 36 | await execa(
|
34 | 37 | 'docker',
|
35 | 38 | ['push', `${process.env.CI_REGISTRY_IMAGE}:${version}`],
|
36 | 39 | { stdio: 'inherit' }
|
37 | 40 | )
|
38 |
| - await execa('docker', ['push', `${process.env.CI_REGISTRY_IMAGE}:latest`], { |
39 |
| - stdio: 'inherit' |
40 |
| - }) |
| 41 | + } |
| 42 | + |
| 43 | + try { |
| 44 | + // parse version to parts |
| 45 | + const { major, minor, patch, prerelease } = semver(version) |
| 46 | + const isProdRelease = prerelease.length === 0 |
| 47 | + |
| 48 | + // first release version as it is |
| 49 | + await publish(version) |
| 50 | + |
| 51 | + if (isProdRelease) { |
| 52 | + // on production release X:latest, X:1.1, X:1 |
| 53 | + await publish(`${major}.${minor}`) |
| 54 | + await publish(`${major}`) |
| 55 | + await publish('latest') |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + const [channel] = prerelease |
| 60 | + |
| 61 | + // on other channels release X:channel, X:1.1.1-channel |
| 62 | + await publish(channel) |
| 63 | + await publish(`${major}.${minor}.${patch}-${channel}`) |
41 | 64 | } catch (err) {
|
42 | 65 | throw new Error('docker push failed')
|
43 | 66 | }
|
|
0 commit comments