Skip to content

Commit

Permalink
Merge pull request #7 from Totodore/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Totodore authored Dec 8, 2020
2 parents 12738de + 1537fb4 commit c1cd1a0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
33 changes: 30 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ Docker-CI watch for container creations, it means that you don't have to restart

Docker-CI will then create a route corresponding to this pattern : ```http(s)://0.0.0.0[:port]/deploy/:appName``` where the appName correspond to the name you gave to your container or to the name you gave through the option ```docker-ci.name```
You can then set a Github Automation with an [Image building](https://github.com/actions/starter-workflows/blob/a571f2981ab5a22dfd9158f20646c2358db3654c/ci/docker-publish.yml) and you can then add a webhook to trigger the above url when the image is built and stored in the Github Package Registry

## Base configuration :
This is the default configuration, you just have to add docker-ci.enable in your docker-compose.yml :

|Name|Type|Description|
|----|----|-----------|
| ```docker-ci.enable```|```boolean```|Enable CI for this container, an endpoint will be created for this container and whenever it will be called the container image will be repulled and the container will be recreated (total update of the container)|
| ```docker-ci.name```|```string (Optional)```|Set a custom name for the endpoint, by default it is the name of the container|


## Authentification
In case your package is private, you can specify credentials in your config :

|Name|Type|Description|
|----|----|-----------|
| ```docker-ci.username```|```string (Optional)```|Set a username for the docker package registry auth|
| ```docker-ci.password```|```string (Optional)```|Set a password or a token for the docker package registry auth|
| ```docker-ci.auth-server```|```string (Optional)```|Set an auth server for the docker package registry auth|


## Example

### docker-compose.yml of docker-ci app
Expand All @@ -29,15 +49,19 @@ services:
version: "3.7"
services:
app:
image: ghcr.io/totodore/automate:latest ##The github registry link
image: ghcr.io/totodore/automate:latest ##The package registry link
container_name: automate
tty: true
expose:
- 80
restart: always
labels:
- "docker-ci.enabled=true"
- "docker-ci.name=automate"
- "docker-ci.name=automate" #This argument is optional by default it is the name of the container (container_name)
# The following is only if you use auth to get private package
- "docker-ci.password=MyPasswordOrToken" #Registry Password or token
- "docker-ci.username=MyRegistryUsername"
- "docker-ci.auth-server=MyRegistryURL" #Ex for Github Registry : https://ghcr.io or https://docker.pkg.github.com
```
### docker-publish in the github repo :
Expand Down Expand Up @@ -99,8 +123,11 @@ jobs:
WEBHOOK_URL: ${{ secrets.DEPLOY_WEBHOOK_URL }} #This Docker secret correspond to http(s)://IP[:port]/deploy/automate
```
## Labels available :
## All Labels :
|Name|Type|Description|
|----|----|-----------|
| ```docker-ci.enable```|```boolean```|Enable CI for this container, an endpoint will be created for this container and whenever it will be called the container image will be repulled and the container will be recreated (total update of the container)|
| ```docker-ci.name```|```string (Optional)```|Set a custom name for the endpoint, by default it is the name of the container|
| ```docker-ci.username```|```string (Optional)```|Set a username for the docker package registry auth|
| ```docker-ci.password```|```string (Optional)```|Set a password or a token for the docker package registry auth|
| ```docker-ci.auth-server```|```string (Optional)```|Set an auth server for the docker package registry auth|
15 changes: 12 additions & 3 deletions src/docker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DockerCiLabels } from './models/docker-ci-labels.model';
import { DockerEventsModel } from './models/docker-events.model';
import { DockerImagesModel } from './models/docker-images.model';
import * as Docker from "dockerode";
import { Logger } from "./utils/logger";

Expand Down Expand Up @@ -58,12 +59,20 @@ export class DockerManager {
* Pull an image from its tag
* @returns true in case of success
*/
public async pullImage(imageName: string): Promise<boolean> {
public async pullImage(imageName: string, containerLabels: DockerCiLabels): Promise<boolean> {
try {
const imageInfos = await this.getImage(imageName).inspect();
this._logger.log("Pulling : ", ...imageInfos.RepoTags);
let authConf: DockerImagesModel.PullImageAuth | undefined;
if (containerLabels["docker-ci.username"] && containerLabels["docker-ci.password"] && containerLabels["docker-ci.auth-server"]) {
authConf = {
username: containerLabels["docker-ci.username"],
password: containerLabels["docker-ci.password"],
serveraddress: containerLabels["docker-ci.auth-server"]
}
}
for(const tag of imageInfos.RepoTags)
await this._docker.pull(tag);
await this._docker.pull(tag, authConf && { authconfig: authConf });
return true;
} catch (e) {
this._logger.error("Error pulling image", e);
Expand All @@ -83,6 +92,6 @@ export class DockerManager {
this._logger.log("Removing container");
await container.remove();
this._logger.log("Recreating container");
(await this._docker.createContainer(infos.Config)).start();
(await this._docker.createContainer({ ...infos.Config, name: infos.Name })).start();
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class App {
private async _onUrlTriggered(id: string) {
try {
const containerInfos = await this._dockerManager.getContainer(id).inspect();
if (!await this._dockerManager.pullImage(containerInfos.Image))
if (!await this._dockerManager.pullImage(containerInfos.Image, containerInfos.Config.Labels))
throw "Error Pulling Image";
await this._dockerManager.recreateContainer(id);
} catch (e) {
Expand Down
5 changes: 3 additions & 2 deletions src/models/docker-ci-labels.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface DockerCiLabels {
"docker-ci.enable"?: string;
"docker-ci.name"?: string;
"docker-ci.secret"?: string;
"docker-ci.url"?: string;
"docker-ci.password"?: string;
"docker-ci.username"?: string;
"docker-ci.auth-server"?: string;
}
6 changes: 6 additions & 0 deletions src/models/docker-images.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ export namespace DockerImagesModel {
platform?: string;
"X-Registry-Auth"?: string;
}

export interface PullImageAuth {
username: string;
password: string;
serveraddress: string;
}
}

0 comments on commit c1cd1a0

Please sign in to comment.