|
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 |
22 |
| - await execa( |
23 |
| - 'docker', |
24 |
| - [ |
25 |
| - 'tag', |
26 |
| - `${process.env.CI_REGISTRY_IMAGE}:latest`, |
27 |
| - `${process.env.CI_REGISTRY_IMAGE}:${version}` |
28 |
| - ], |
29 |
| - { |
30 |
| - stdio: 'inherit' |
31 |
| - } |
32 |
| - ) |
| 27 | + // tag image if not latest |
| 28 | + if (version !== 'latest') { |
| 29 | + await execa( |
| 30 | + 'docker', |
| 31 | + [ |
| 32 | + 'tag', |
| 33 | + `${process.env.CI_REGISTRY_IMAGE}:latest`, |
| 34 | + `${process.env.CI_REGISTRY_IMAGE}:${version}` |
| 35 | + ], |
| 36 | + { |
| 37 | + stdio: 'inherit' |
| 38 | + } |
| 39 | + ) |
| 40 | + } |
| 41 | + // push image |
33 | 42 | await execa(
|
34 | 43 | 'docker',
|
35 | 44 | ['push', `${process.env.CI_REGISTRY_IMAGE}:${version}`],
|
36 | 45 | { stdio: 'inherit' }
|
37 | 46 | )
|
38 |
| - await execa('docker', ['push', `${process.env.CI_REGISTRY_IMAGE}:latest`], { |
39 |
| - stdio: 'inherit' |
40 |
| - }) |
| 47 | + } |
| 48 | + |
| 49 | + try { |
| 50 | + // parse version to parts |
| 51 | + const major = semver.major(version) |
| 52 | + const minor = semver.minor(version) |
| 53 | + const patch = semver.patch(version) |
| 54 | + const prerelease = semver.prerelease(version) |
| 55 | + const isProdRelease = prerelease.length === 0 |
| 56 | + |
| 57 | + // first release version as it is |
| 58 | + await publish(version) |
| 59 | + |
| 60 | + if (isProdRelease) { |
| 61 | + // on production release X:latest, X:1.1, X:1 |
| 62 | + await publish(`${major}.${minor}`) |
| 63 | + await publish(`${major}`) |
| 64 | + await publish('latest') |
| 65 | + return |
| 66 | + } else { |
| 67 | + const [channel] = prerelease |
| 68 | + |
| 69 | + // on other channels release X:channel, X:1.1.1-channel |
| 70 | + await publish(channel) |
| 71 | + await publish(`${major}.${minor}.${patch}-${channel}`) |
| 72 | + } |
41 | 73 | } catch (err) {
|
42 | 74 | throw new Error('docker push failed')
|
43 | 75 | }
|
|
0 commit comments