Skip to content

Commit

Permalink
Improve CI setup (#17)
Browse files Browse the repository at this point in the history
* Rename `tests` directory to `test`
* Reorganize CI scripts
  • Loading branch information
pkosiec committed Aug 19, 2018
1 parent 30ff31f commit e30e1e2
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 43 deletions.
22 changes: 22 additions & 0 deletions .ci/run_in_all_projects.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
TASK_TYPE=$1
CI_DIR_NAME=".ci"

echo "=== Task $TASK_TYPE ==="

echo "Setting environmental variables..."
export CI_BUILD_NUMBER=$TRAVIS_BUILD_NUMBER
export CI_PULL_REQUEST=$TRAVIS_PULL_REQUEST
export CI_PULL_REQUEST_BRANCH=$TRAVIS_PULL_REQUEST_BRANCH
export CI_BRANCH=$TRAVIS_BRANCH

echo "Scanning all directories on root..."
for dir in */ ; do
SCRIPT_PATH="$dir$CI_DIR_NAME/$TASK_TYPE.sh"
if [ ! -f "$SCRIPT_PATH" ]; then
continue
fi

echo ">> Running $TASK_TYPE in directory $dir..."
source $SCRIPT_PATH
done
41 changes: 2 additions & 39 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,7 @@ services:
- mongodb

script:
# Mongo Seeding library
- echo "Building Mongo Seeding test image..."
- docker build -t $CORE_IMAGE_NAME:$TRAVIS_BUILD_NUMBER -f ./core/Dockerfile .
- echo "Running Mongo Seeding tests..."
- docker run --rm --network="host" -e CODACY_PROJECT_TOKEN=$CODACY_PROJECT_TOKEN $CORE_IMAGE_NAME:$TRAVIS_BUILD_NUMBER
# Mongo Seeding Docker Image
- echo "Building Mongo Seeding Docker image..."
- cd ./docker-image
- docker build -t $DOCKER_IMAGE_REPOSITORY:$TRAVIS_BUILD_NUMBER .
- . ./.ci/run_in_all_projects.sh script

after_success:
# Tag and push Mongo Seeding Docker image
- echo "Tagging Mongo Seeding Docker image..."
- cd ../docker-image
- export NPM_VERSION=$(node -p -e "require('./package.json').version");
- echo "Package version $NPM_VERSION";
- export TAG=`if [[ $TRAVIS_PULL_REQUEST == "false" ]] && [[ $TRAVIS_BRANCH == "master" ]]; then
echo "latest"; else
echo "PR-$TRAVIS_PULL_REQUEST_BRANCH";
fi`
- if [[ $TRAVIS_PULL_REQUEST == "false" ]] && [[ $TRAVIS_BRANCH == "master" ]]; then
export ALREADY_EXISTS=$(curl https://hub.docker.com/v2/repositories/$DOCKER_IMAGE_REPOSITORY/tags/?page_size=10000 | jq -r "[.results | .[] | .name == \"$NPM_VERSION\"] | any");
fi
- if [ $ALREADY_EXISTS == "true" ]; then
echo "Skipping pushing image with the same NPM Version tag";
exit 0;
fi
- echo "Tag $TAG";
- docker login -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD";
- docker tag $DOCKER_IMAGE_REPOSITORY:$TRAVIS_BUILD_NUMBER $DOCKER_IMAGE_REPOSITORY:$TAG;
- echo "Pushing the Mongo Seeding Docker image with $TAG tag..."
# Push image
- docker push $DOCKER_IMAGE_REPOSITORY:$TAG;
# Push additional tag with version if necessary
- if [[ $TRAVIS_PULL_REQUEST == "false" ]] && [[ $TRAVIS_BRANCH == "master" ]]; then
echo "Tagging the Mongo Seeding Docker image with $NPM_VERSION tag...";
docker tag $DOCKER_IMAGE_REPOSITORY:$TRAVIS_BUILD_NUMBER $DOCKER_IMAGE_REPOSITORY:$NPM_VERSION;
echo "Pushing the Mongo Seeding Docker image with $NPM_VERSION tag...";
docker push $DOCKER_IMAGE_REPOSITORY:$NPM_VERSION;
fi

- . ./.ci/run_in_all_projects.sh after_success
9 changes: 9 additions & 0 deletions core/.ci/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

IMAGE_NAME="core-test"

echo "Building test image..."
docker build -t $IMAGE_NAME:$CI_BUILD_NUMBER -f ./core/Dockerfile .

echo "Running tests with coverage reporting..."
docker run --rm --network="host" -e CODACY_PROJECT_TOKEN=$CODACY_PROJECT_TOKEN $IMAGE_NAME:$CI_BUILD_NUMBER
2 changes: 1 addition & 1 deletion core/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/node_modules
/dist
/coverage
/tests/**/.*
/test/**/.*
2 changes: 1 addition & 1 deletion core/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ RUN npm i
COPY ./.git/ /.git/
COPY $DIR/tsconfig.json /app/
COPY $DIR/src/ /app/src/
COPY $DIR/tests/ /app/tests/
COPY $DIR/test/ /app/test/

#
# Build app
Expand Down
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"lcov",
"json"
],
"testRegex": "(/tests/[^_].*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"testRegex": "(/test/[^_].*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"exclude": [
"node_modules",
"tests"
"test",
"coverage"
]
}
39 changes: 39 additions & 0 deletions docker-image/.ci/after_success.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

echo "Tagging Docker image..."
cd ./docker-image

NPM_VERSION=$(node -p -e "require('./package.json').version");
echo "Package version: $NPM_VERSION";

TAG=$(if [[ $CI_PULL_REQUEST == "false" ]] && [[ $CI_BRANCH == "master" ]]; then
echo "latest";
elif [[ $CI_PULL_REQUEST == "false" ]]; then
echo "snapshot-branch-$CI_BRANCH";
else
echo "snapshot-PR-$CI_PULL_REQUEST_BRANCH";
fi)

if [[ $CI_PULL_REQUEST == "false" ]] && [[ $CI_BRANCH == "master" ]]; then
ALREADY_EXISTS=$(curl https://hub.docker.com/v2/repositories/$DOCKER_IMAGE_REPOSITORY/tags/?page_size=10000 | jq -r "[.results | .[] | .name == \"$NPM_VERSION\"] | any")
fi

if [[ $ALREADY_EXISTS == "true" ]]; then
echo "Skipping pushing image with the same NPM Version tag";
exit 0;
fi

echo "Tagging the Docker image with $TAG tag...";
docker login -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD";
docker tag $DOCKER_IMAGE_REPOSITORY:$CI_BUILD_NUMBER $DOCKER_IMAGE_REPOSITORY:$TAG;

echo "Pushing the Docker image with $TAG tag..."
docker push $DOCKER_IMAGE_REPOSITORY:$TAG;

# Push additional tag with version if necessary
if [[ $CI_PULL_REQUEST == "false" ]] && [[ $CI_BRANCH == "master" ]]; then
echo "Tagging the Docker image with $NPM_VERSION tag...";
docker tag $DOCKER_IMAGE_REPOSITORY:$CI_BUILD_NUMBER $DOCKER_IMAGE_REPOSITORY:$NPM_VERSION;
echo "Pushing the Docker image with $NPM_VERSION tag...";
docker push $DOCKER_IMAGE_REPOSITORY:$NPM_VERSION;
fi
6 changes: 6 additions & 0 deletions docker-image/.ci/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

echo "Building image..."
cd ./docker-image
docker build -t $DOCKER_IMAGE_REPOSITORY:$CI_BUILD_NUMBER .
cd ..

0 comments on commit e30e1e2

Please sign in to comment.