Skip to content

Commit 92c2b3c

Browse files
author
Amir Moualem
authored
Merge pull request #93 from snyk/ci_experiments
Semantic Release & Test the image we build
2 parents d5753b4 + 05d6d4e commit 92c2b3c

File tree

6 files changed

+59
-25
lines changed

6 files changed

+59
-25
lines changed

.travis.yml

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,50 @@ install:
1919
- npm install
2020
jobs:
2121
include:
22+
######################## PR TO STAGING ########################
2223
# PR tests for staging branch
2324
- stage: Test-PR-staging
2425
if: branch = staging AND type = pull_request
2526
script: npm run test
2627
name: kubernetes-monitor PR tests
27-
# merge tests before building the product on staging branch
28-
- stage: Test-Merge-staging
28+
######################## MERGE TO STAGING ########################
29+
- stage: test-and-build
2930
if: branch = staging AND type = push
30-
script: npm run test # TODO: only check linting, unittest
31-
name: kubernetes-monitor merge initial tests
32-
# building the product on staging branch after merging
33-
- stage: Build
34-
if: branch = staging AND type = push
35-
# we will need to change the tag to test-candidate, then use the image, then re-tag to test-approved
36-
script: docker login --username ${DOCKERHUB_USER} --password ${DOCKERHUB_PASSWORD} && docker build -t snyk/kubernetes-monitor:test-approved --no-cache . && docker push snyk/kubernetes-monitor:test-approved && ./scripts/slack-notify-push.sh test-approved
37-
name: build the kubernetes-monitor image
38-
# TODO: run integration tests with the image we just built, then mark it as test-passed
31+
script: npm run lint &&
32+
npm run build &&
33+
npm run test:unit &&
34+
docker login --username ${DOCKERHUB_USER} --password ${DOCKERHUB_PASSWORD} &&
35+
npx semantic-release &&
36+
NEW_VERSION=`cat ./package.json | jq -r '.version'` &&
37+
IMAGE_NAME_CANDIDATE=snyk/kubernetes-monitor:${NEW_VERSION}-candidate &&
38+
./scripts/build-image.sh ${IMAGE_NAME_CANDIDATE} &&
39+
docker push ${IMAGE_NAME_CANDIDATE} &&
40+
./scripts/slack-notify-push.sh ${IMAGE_NAME_CANDIDATE} &&
41+
KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG=${IMAGE_NAME_CANDIDATE} npm run test:integration &&
42+
IMAGE_NAME_APPROVED=snyk/kubernetes-monitor:${NEW_VERSION}-approved &&
43+
docker tag ${IMAGE_NAME_CANDIDATE} ${IMAGE_NAME_APPROVED} &&
44+
docker push ${IMAGE_NAME_APPROVED} &&
45+
./scripts/slack-notify-push.sh ${IMAGE_NAME_APPROVED}
46+
name: Test and Build
47+
######################## PR TO MASTER ########################
3948
- stage: pre-publish
4049
if: branch = master AND type = pull_request AND head_branch = staging
4150
script: ./scripts/slack-notify-pr.sh
4251
name: pre-publish notification
52+
######################## MERGE TO MASTER ########################
4353
- stage: Publish
4454
if: branch = master AND type = push
45-
script: docker login --username ${DOCKERHUB_USER} --password ${DOCKERHUB_PASSWORD} && docker pull snyk/kubernetes-monitor:test-approved && docker tag snyk/kubernetes-monitor:test-approved snyk/kubernetes-monitor:latest && docker push snyk/kubernetes-monitor:latest && ./scripts/slack-notify-push.sh latest
55+
script: LATEST_TAG=`git describe --abbrev=0 --tags ${TRAVIS_COMMIT}` &&
56+
IMAGE_NAME_APPROVED=snyk/kubernetes-monitor:${LATEST_TAG}-approved &&
57+
IMAGE_NAME_PUBLISHED=snyk/kubernetes-monitor:${LATEST_TAG} &&
58+
docker login --username ${DOCKERHUB_USER} --password ${DOCKERHUB_PASSWORD} &&
59+
docker pull ${IMAGE_NAME_APPROVED} &&
60+
docker tag ${IMAGE_NAME_APPROVED} ${IMAGE_NAME_PUBLISHED} &&
61+
docker push ${IMAGE_NAME_PUBLISHED} &&
62+
./scripts/slack-notify-push.sh ${IMAGE_NAME_PUBLISHED} &&
63+
docker tag ${IMAGE_NAME_APPROVED} snyk/kubernetes-monitor:latest &&
64+
docker push ${IMAGE_NAME_PUBLISHED} &&
65+
./scripts/slack-notify-push.sh snyk/kubernetes-monitor:latest
4666
name: publish the kubernetes-monitor (npm, container, helm)
4767
branches:
4868
only:

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "Snyk kubernetes Monitioring Container",
44
"main": "dist/index.js",
55
"scripts": {
6+
"pretest": "./scripts/build-image.sh",
67
"test": "npm run lint && npm run test:unit && npm run test:integration",
78
"test:unit": "./tap test/unit -R spec",
89
"test:integration": "./tap test/integration -Rdot --timeout=600",
@@ -61,6 +62,7 @@
6162
"url": "https://github.com/snyk/kubernetes-monitor.git"
6263
},
6364
"release": {
65+
"branch": "staging",
6466
"verifyConditions": [
6567
"@semantic-release/github"
6668
],

scripts/build-image.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#! /bin/bash
2+
3+
# default value for our name and tag, when we don't want to push the image
4+
# for example when testing locally or on opening a PR
5+
LOCAL_DISCARDABLE_IMAGE=snyk/kubernetes-monitor:local
6+
7+
# allow overriding name and tag, when we intend to push the image
8+
# should happen on merging to `staging`
9+
NAME_AND_TAG=${1:-$LOCAL_DISCARDABLE_IMAGE}
10+
11+
docker build -t ${NAME_AND_TAG} .

scripts/slack-notify-push.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#! /bin/bash
2-
curl -X POST -H 'Content-Type:application/json' -d '{"attachments": [{"color": "warning", "fallback": "Build Notification: $TRAVIS_BUILD_WEB_URL", "title": "Kubernetes-Monitor Publish Notification", "text": ":egg_fancy: Published Kubernetes-Monitor with tag: '$1' :egg_fancy:"}]}' $SLACK_WEBHOOK
2+
curl -X POST -H 'Content-Type:application/json' -d '{"attachments": [{"color": "warning", "fallback": "Build Notification: $TRAVIS_BUILD_WEB_URL", "title": "Kubernetes-Monitor Publish Notification", "text": ":egg_fancy: Published Kubernetes-Monitor: `'$1'` :egg_fancy:"}]}' $SLACK_WEBHOOK

test/integration/kubernetes.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ tap.test('deploy snyk-monitor', async (t) => {
3838
try {
3939
// attempt to clean up ...
4040
await tearDown();
41+
} catch (teardownError) {
42+
// ignore cleanup errors
4143
} finally {
4244
// ... but make sure the test suite doesn't proceed if the setup failed
4345
process.exit(-1);

test/setup.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,7 @@ async function exportKubeConfig(clusterName = 'kind'): Promise<void> {
9898
console.log('Exported K8s config!');
9999
}
100100

101-
async function buildDockerImage(imageNameAndTag = 'snyk-k8s-monitor:test'): Promise<void> {
102-
console.log('Building kubernetes-monitor Docker image...');
103-
await exec(`docker build -t ${imageNameAndTag} --no-cache .`);
104-
console.log('Built Docker image!');
105-
}
106-
107-
async function loadImageInCluster(imageNameAndTag = 'snyk-k8s-monitor:test'): Promise<void> {
101+
async function loadImageInCluster(imageNameAndTag): Promise<void> {
108102
console.log(`Loading image ${imageNameAndTag} in cluster...`);
109103
await exec(`./kind load docker-image ${imageNameAndTag}`);
110104
console.log(`Loaded image ${imageNameAndTag}!`);
@@ -147,12 +141,12 @@ export async function getDeloymentJson(deploymentName: string, namespace: string
147141
return JSON.parse(getDeploymentResult.stdout);
148142
}
149143

150-
function createTestYamlDeployment(newYamlPath: string, integrationId: string): void {
144+
function createTestYamlDeployment(newYamlPath: string, integrationId: string, imageNameAndTag: string): void {
151145
console.log('Creating test deployment...');
152146
const originalDeploymentYaml = readFileSync('./snyk-monitor-deployment.yaml', 'utf8');
153147
const deployment = parse(originalDeploymentYaml);
154148

155-
deployment.spec.template.spec.containers[0].image = 'snyk-k8s-monitor:test';
149+
deployment.spec.template.spec.containers[0].image = imageNameAndTag;
156150
deployment.spec.template.spec.containers[0].imagePullPolicy = 'Never';
157151

158152
// This is important due to an odd bug when running on Travis.
@@ -241,6 +235,12 @@ async function cleanUpMonitorSetup(): Promise<void> {
241235
}
242236

243237
Test.prototype.deployMonitor = async (): Promise<string> => {
238+
let imageNameAndTag = process.env['KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG'];
239+
if (imageNameAndTag === undefined || imageNameAndTag === '') {
240+
// the default, determined by ./script/build-image.sh
241+
imageNameAndTag = 'snyk/kubernetes-monitor:local';
242+
}
243+
244244
const k8sRelease = await getLatestStableK8sRelease();
245245
const osDistro = platform();
246246

@@ -250,8 +250,7 @@ Test.prototype.deployMonitor = async (): Promise<string> => {
250250
await createKindCluster();
251251
await exportKubeConfig();
252252

253-
await buildDockerImage();
254-
await loadImageInCluster();
253+
await loadImageInCluster(imageNameAndTag);
255254

256255
const namespace = 'snyk-monitor';
257256
await createNamespace(namespace);
@@ -270,7 +269,7 @@ Test.prototype.deployMonitor = async (): Promise<string> => {
270269
await createDeploymentFromImage('alpine-from-sha', someImageWithSha, servicesNamespace);
271270

272271
const testYaml = 'snyk-monitor-test-deployment.yaml';
273-
createTestYamlDeployment(testYaml, integrationId);
272+
createTestYamlDeployment(testYaml, integrationId, imageNameAndTag);
274273

275274
await applyK8sYaml('./snyk-monitor-cluster-permissions.yaml');
276275
await applyK8sYaml('./snyk-monitor-test-deployment.yaml');

0 commit comments

Comments
 (0)