Feature Request: before_script and after_script per stage
I have a gitlab-ci.yml that really needs to be able to run a before_script and after_script for either a job or stage.
Essentially I want to build a docker container, then run it, then run some e2e tests, then stop the container.
I can't yet work out how to do this, as the container will not die when the tests are done, it needs to be told to stop.
Here's what I would like to be able to do.
# Pan Galactic Continuous Integration Script
# For use with Gitlab CI
#
# Variables
# - DOCKER_REPOSITORY = the location of the docker docker repository to push to
#
# We use two variables provided by gitlab CI to ensure our build is unique, and we will
# work with the same build/image throughout this script
# - CI_PROJECT_ID
# - CI_BUILD_REF
# - CI_BUILD_REF_NAME = branch name
# Define the stages of our CI script
stages:
- build
- test
- integration
- deploy # We only run deploy on master
######################################### Build ##################################################
# Build our image
build:
stage: build
script:
- docker build -t $CI_PROJECT_ID$CI_BUILD_REF .
######################################### Test ###################################################
# Run our unit tests
#
# use the regex Lines\s*:\s*\d+.\d+\%
# to collect test coverage
unit-tests:
stage: test
script: docker run $CI_PROJECT_ID$CI_BUILD_REF npm run run:tests-ci
before_script:
stage: integration
script:
- docker build -t $CI_PROJECT_ID$CI_BUILD_REF .
- docker run -e NODE_ENV=testing -e VIRTUAL_HOST=pg-testing -e VIRTUAL_PORT=$VIRTUAL_PORT -e BROWSERSTACK_USERNAME=$BROWSERSTACK_USERNAME -e BROWSERSTACK_ACCESS_KEY=$BROWSERSTACK_ACCESS_KEY --name $CI_PROJECT_ID$CI_BUILD_REF $CI_PROJECT_ID$CI_BUILD_REF npm run start
# Run our integration E2E tests
#
# to collect test coverage
integration-tests:
stage: integration
script:
- docker exec -it $CI_PROJECT_ID$CI_BUILD_REF npm run run:nightwatch-integration
after_script:
stage: integration
script:
- docker stop --name $CI_PROJECT_ID$CI_BUILD_REF
# Node Security Project
#
# Check our currently install module versions against NSP
# If there are warnings that will cause failures, log them, but don't break the build
# remove `allow_failure: true` if you want the build to break
nsp:
stage: test
allow_failure: true
script: docker run $CI_PROJECT_ID$CI_BUILD_REF nsp check
######################################### Deploy #################################################
# Deploy the image to our repository
# this will put each branch in your repository
pushImageBranch:
stage: deploy
exclude:
- master
- staging
script:
- docker tag -f $CI_PROJECT_ID$CI_BUILD_REF:$CI_BUILD_REF_NAME $DOCKER_REPOSITORY:$CI_BUILD_REF_NAME
- docker push $DOCKER_REPOSITORY:$CI_BUILD_REF_NAME
# Push staging to :staging
# this will put each branch in your repository
pushImageBranch:
stage: deploy
only:
- staging
script:
- docker tag -f $CI_PROJECT_ID$CI_BUILD_REF:$CI_BUILD_REF_NAME $DOCKER_REPOSITORY:$CI_BUILD_REF_NAME
- docker push $DOCKER_REPOSITORY:$CI_BUILD_REF_NAME
# Push master to :latest
pushImageLatest:
stage: deploy
only:
- master
script:
- docker tag -f $CI_PROJECT_ID$CI_BUILD_REF:latest $DOCKER_REPOSITORY:latest
- docker push $DOCKER_REPOSITORY:latest