Skip to content

Commit

Permalink
feat(plugin): Add ability to push multiple docker images
Browse files Browse the repository at this point in the history
This feature allows specifying the plugin multiple times in order to push multiple images to a
docker repository.

iteratec#4
  • Loading branch information
DanielHabenicht committed Apr 26, 2019
1 parent d9b787c commit 96850c9
Show file tree
Hide file tree
Showing 21 changed files with 844 additions and 223 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ node_modules
dist

*.log

develop.ts
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM scratch
ARG TEST
155 changes: 115 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,51 @@

A [semantic-release](https://github.com/semantic-release/semantic-release) plugin to use semantic versioning for docker images.

## verifyConditions

verifies that environment variables for authentication via username and password are set.
It uses a registry server provided via config or environment variable (preferred) or defaults to docker hub if none is given.
It also verifies that the credentials are correct by logging in to the given registry.

## prepare

tags the specified image with the version number determined by semantic-release and additional tags provided in the configuration.
In addition it supports specifying a complete image name (CIN) via configuration settings according to the canonical format specified by docker:

`[registryhostname[:port]/][username/]imagename[:tag]`

## publish
## Configuration

pushes the tagged images to the registry.
### Installation

## Configuration
`npm i --save @iteratec/semantic-release-docker`

### docker registry authentication
### Docker registry authentication

The `docker registry` authentication is **required** and can be set via environment variables.

### Environment variables

| Variable | Description |
|--------------------------|-------------------------------------------------------------------------------------------|
| ------------------------ | ----------------------------------------------------------------------------------------- |
| DOCKER_REGISTRY_URL | The hostname and port used by the desired docker registry. Leave blank to use docker hub. |
| DOCKER_REGISTRY_USER | The user name to authenticate with at the registry. |
| DOCKER_REGISTRY_PASSWORD | The password used for authentication at the registry. |

### Options

| Option | Description |
|----------------|--------------------------------------------------------------------------------------------|
| additionalTags | _Optional_. An array of strings allowing to specify additional tags to apply to the image. |
| imageName | **_Required_** The name of the image to release. |
| registryUrl | _Optional_. The hostname and port used by the the registry in format `hostname[:port]`. Omit the port if the registry uses the default port |
| repositoryName | _Optional_. The name of the repository in the registry, e.g. username on docker hub |

### Usage

full configuration:
``` json
#### Full configuration

```json
{
"verifyConfig": ["@iteratec/semantic-release-docker"],
"prepare": {
"path": "@iteratec/semantic-release-docker",
"additionalTags": ["test", "demo"],
"imageName": "my-image",
"registryUrl": "my-private-registry:5678",
"respositoryName": "my-repository"
},
"prepare": [
{
"path": "@iteratec/semantic-release-docker",
"additionalTags": ["test", "demo"],
"imageName": "my-image",
"registryUrl": "my-private-registry:5678",
"respositoryName": "my-repository"
}
],
"publish": {
"path": "@iteratec/semantic-release-docker"
}
}
```
results in `my-private-registry:5678/my-repository/my-image` with tags `test`, `demo` and the `<semver>` determined by `semantic-release`.

minimum configuration:
``` json
Results in `my-private-registry:5678/my-repository/my-image` with tags `test`, `demo` and the `<semver>` determined by `semantic-release`.

#### Minimum configuration

```json
{
"verifyConfig": ["@iteratec/semantic-release-docker"],
"prepare": {
Expand All @@ -79,4 +62,96 @@ minimum configuration:
}
}
```
results in `my-image:<semver>`

Results in `my-image:<semver>`.

### Options

| Option | Description |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| additionalTags | _Optional_. An array of strings allowing to specify additional tags to apply to the image. |
| imageName | **_Required_** The name of the image to release. |
| registryUrl | _Optional_. The hostname and port used by the the registry in format `hostname[:port]`. Omit the port if the registry uses the default port |
| repositoryName | _Optional_. The name of the repository in the registry, e.g. username on docker hub |

## Steps

### verifyConditions

It uses a registry server provided via config or environment variable (preferred) or defaults to docker hub if none is given.

1. Verifies that environment variables for authentication via username and password are set.
2. It also verifies that the credentials are correct by logging in to the given registry.

### prepare

Tags the specified image with the version number determined by semantic-release and additional tags provided in the configuration.
In addition it supports specifying a complete image name (CIN) via configuration settings according to the canonical format specified by docker:

`[registryhostname[:port]/][username/]imagename[:tag]`

### publish

Pushes the tagged images to the registry.

## Contribute

### Develop

1. Create a develop.ts file in the root of this Git-Repository and copy this:

```typescript
import { SemanticReleaseConfig, SemanticReleaseContext } from 'semantic-release';
import { prepare, publish, verifyConditions } from './src';
import { DockerPluginConfig } from './src/models';

process.env.DOCKER_REGISTRY_USER = '<Your Docker Registry User>';
process.env.DOCKER_REGISTRY_PASSWORD = '<Your Docker Registry Password>';

const config: SemanticReleaseConfig = {
branch: '',
noCi: true,
repositoryUrl: '',
tagFormat: ''
};
const context: SemanticReleaseContext = {
logger: {
// tslint:disable-next-line:no-empty
log: (message: string) => {}
},
options: {
branch: '',
noCi: true,
prepare: [
{
additionalTags: ['latest'],
imageName: 'testimage',
repositoryName: '<your test repository>',
path: '@iteratec/semantic-release-docker'
} as DockerPluginConfig,
{
additionalTags: ['latest'],
imageName: 'testimage1',
repositoryName: '<your test repository>',
path: '@iteratec/semantic-release-docker'
} as DockerPluginConfig
],
repositoryUrl: '',
tagFormat: ''
},
nextRelease: {
version: '1.0.3',
gitHead: '45jh345g',
gitTag: 'v1.0.3',
notes: 'Nothing special'
}
};
context.logger.log = (string: string) => {
console.log(string);
};
verifyConditions(config, context);
prepare(config, context);
publish(config, context);
```

2. Simply run the "Debug" VS Code Task
30 changes: 30 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
jobs:
- job: Build
pool:
name: Hosted Ubuntu 1604
demands: npm
steps:
- task: NodeTool@0
displayName: "Use Node 10"
inputs:
versionSpec: 10.x
- task: Npm@1
displayName: "Install dependencies"
inputs:
verbose: false
- task: Npm@1
displayName: Build
inputs:
command: custom
customCommand: run build
- task: Npm@1
displayName: Test
inputs:
command: custom
customCommand: run test
- script: |
npm run release
displayName: Publish
env:
NPM_TOKEN: $(NPMTOKEN)
GITHUB_TOKEN: $(GITHUBTOKEN)
27 changes: 10 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"build": "rimraf dist && tsc",
"postbuild": "cpx package.json dist/ && cpx package-lock.json dist/",
"postbuild": "cpx package.json dist/ && cpx package-lock.json dist/ && cpx README.md dist/",
"commit": "git-cz",
"test": "mocha -r chai -r chai-as-promised -r ts-node/register src/**/*.spec.ts"
},
Expand All @@ -31,7 +31,7 @@
"@commitlint/config-conventional": "^7.1.2",
"@types/chai": "^4.1.3",
"@types/chai-as-promised": "^7.1.0",
"@types/dockerode": "^2.5.4",
"@types/dockerode": "^2.5.13",
"@types/mocha": "^5.2.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
Expand Down
6 changes: 6 additions & 0 deletions src/models/PluginSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DockerPluginConfig } from './dockerPluginConfig';

export interface PluginSettings {
path: '@iteratec/semantic-release-docker';
defaultValues: DockerPluginConfig;
}
9 changes: 9 additions & 0 deletions src/models/authentication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Credentials } from './credentials';

/**
* Authentication
* From: https://docs.docker.com/engine/api/v1.37/#section/Authentication
*/
export interface Authentication extends Credentials {
serveraddress: string;
}
4 changes: 4 additions & 0 deletions src/models/credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Credentials {
username: string;
password: string;
}
7 changes: 7 additions & 0 deletions src/models/dockerPluginConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { SemanticReleasePlugin } from "semantic-release";
export interface DockerPluginConfig extends SemanticReleasePlugin {
additionalTags?: string[];
imageName: string;
registryUrl?: string;
repositoryName?: string;
}
3 changes: 3 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { Authentication } from './authentication';
export { DockerPluginConfig } from './dockerPluginConfig';
export { Credentials } from './credentials';
12 changes: 12 additions & 0 deletions src/plugin-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PluginSettings } from "./models/PluginSettings";

export const pluginSettings: PluginSettings = {
path: "@iteratec/semantic-release-docker",
defaultValues: {
additionalTags: [],
imageName: "",
path: "@iteratec/semantic-release-docker",
registryUrl: "",
repositoryName: ""
}
};
Loading

0 comments on commit 96850c9

Please sign in to comment.