If statement with "unexpected end of file" on circleCI - bash

I'm trying to do a condition in my tests that print echo "showing dev branch" if my branch name is development but I'm receiving this error
if [ "${CIRCLE_BRANCH}" == "development"]; then echo "showing dev branch" fi
bash: -c: line 2: syntax error: unexpected end of file
if [ "${CIRCLE_BRANCH}" == "development"]; then echo "showing dev branch" fi returned exit code 1
See my circle.yml below:
general:
artifacts:
- "test_evidences"
branches:
only:
- development
machine:
node:
version: 6.10.3
dependencies:
pre:
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome.deb
- sudo sed -i 's|HERE/chrome\"|HERE/chrome\" --disable-setuid-sandbox|g' /opt/google/chrome/google-chrome
- rm google-chrome.deb
- npm install
- npm install -g grunt grunt-cli
override:
- node_modules/.bin/webdriver-manager update
test:
pre:
- sleep 60
override:
- if [ "${CIRCLE_BRANCH}" == "development"]; then
echo "showing dev branch"
fi
- grunt apiTests
- node_modules/.bin/protractor conf.js
- sed -i -- 's,//,/,g' test_evidences/htmlReport.html

Problem solved!
My new circle.yml file is:
#!/usr/bin/env bash
general:
artifacts:
- "test_evidences"
branches:
only:
- development
machine:
node:
version: 6.10.3
dependencies:
pre:
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome.deb
- sudo sed -i 's|HERE/chrome\"|HERE/chrome\" --disable-setuid-sandbox|g' /opt/google/chrome/google-chrome
- rm google-chrome.deb
- npm install
- npm install -g grunt grunt-cli
override:
- node_modules/.bin/webdriver-manager update
test:
pre:
- sleep 60
override:
- if [ "${CIRCLE_BRANCH}" == "development" ]; then
echo "showing dev branch";
fi
- grunt apiTests
- node_modules/.bin/protractor conf.js
- sed -i -- 's,//,/,g' test_evidences/htmlReport.html

Related

I am trying to add Bandit to my gitlab-ci.yml file but on executing an egrep command the job fails without any error message

The .gitlab-ci.yml file is below, this is the part where I am executing the Test Stage. After running the egrep command, it just fails without any error message
stage: test
image:
name: python:latest
entrypoint:
- "/usr/bin/env"
- "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
script:
- ls -la
- python --version
- pip3 install --upgrade pip
- pip3 install --upgrade setuptools
- pip3 install bandit
- bandit ../lambda_function_code -r | tee ./output_test.log
- egrep "Severity:\sHigh" output_test.log | wc -l
- |
if [ $severity_count -neq 0 ]; then
exit 1
fi
- echo ${TF_ROOT}
Pipeline Output (the egrep commaned run successfully, but then after that nothing get runs and the pipeline fails)
I am trying to find text with Severity: High/medium/low from the output_test.log file generated by the bandit command.
What am I doing wrong here?
You want to store the output of egrep "Severity:\sHigh" output_test.log | wc -l into the $severity_count variable.
Try the following:
stage: test
image:
name: python:latest
entrypoint:
- "/usr/bin/env"
- "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
script:
- ls -la
- python --version
- pip3 install --upgrade pip
- pip3 install --upgrade setuptools
- pip3 install bandit
- bandit ../lambda_function_code -r | tee ./output_test.log
- severity_count=$(egrep "Severity:\sHigh" output_test.log | wc -l)
- |
if [ $severity_count -neq 0 ]; then
exit 1
fi
- echo ${TF_ROOT}
The $(...) around the command tells the script to evaluate the shell statement within the parenthesis.

Gitlab ci with unit testing that connects to MySQL

I have built a Golang project, I want to deploy the app when successfully tested with GitLab ci, but when do some tests, it fails because cannot connect to MySQL.
I want to use the Golang image and MySQL image in one stage.
This is my current pipeline. On stage test, on before script fails (/bin/bash: line 130: mysql: command not found)
# To contribute improvements to CI/CD templates, please follow the Development guide
at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Go.gitlab-ci.yml
image: golang:latest
services:
- mysql:latest
stages:
- test
- build
- deploy
variables:
MYSQL_DATABASE: "db"
MYSQL_USER: "user"
MYSQL_PASSWORD: "password"
MYSQL_ROOT_PASSWORD: "password"
format:
stage: test
variables:
# Configure mysql environment variables (https://hub.docker.com/_/mysql/)
MYSQL_DATABASE: $MYSQL_DATABASE
MYSQL_PASSWORD: $MYSQL_PASSWORD
MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
services:
- mysql:latest
before_script:
- mysql --version
script:
- go fmt $(go list ./... | grep -v /vendor/)
- go vet $(go list ./... | grep -v /vendor/)
- go test -race $(go list ./... | grep -v /vendor/)
compile:
stage: build
script:
- mkdir -p typing
- go build -o typing ./...
artifacts:
paths:
- typing
deploy:
image: google/cloud-sdk:alpine
stage: deploy
allow_failure: true
script:
- "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )"
# Run ssh-agent (inside the build environment)
- eval $(ssh-agent -s)
# Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
- ssh-add <(echo "$SSH_PRIVATE_KEY" | base64 -d)
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
- (echo "$SSH_PRIVATE_KEY" | base64 -d) > file
- echo "$SSH_PUBLIC_KEY" > file.pub
- chmod 600 file
- echo $SERVICE_ACCOUNT > file.json
- gcloud auth activate-service-account --key-file file.json
- gcloud compute scp typing/* --project="project-id" --zone="zone" vm-name:/home/ubuntu
- ssh -i file ubuntu#public-ip 'sudo ./kill.sh; sudo ./start.sh'
artifacts:
paths:
- typing
How can I achieve that?
Thanks in advance.
In the stage test, the job is based on the golang image as a result it does not come packaged with the MySQL client.
In order to reach the MySQL service you defined you need to install the client
If I am not mistaken the Golang image is based on debian, so something like this
before_script:
- apt get-update
- apt get-install -y default-mysql-client
- mysql --version

Trigger after merge successful?

I have this .gitlab-ci.yml script --- it triggers if I push directly on the master. How can I trigger this if there's a successful merge on the master branch?
before_script:
- apt-get update -qq
- apt-get install -qq git
# Setup SSH deploy keys
- 'which ssh-agent || ( apt-get install -qq openssh-client )'
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
deploy_staging:
type: deploy
environment:
name: staging
url: domain.com
script:
- ssh user#domain.com "cd /server/directory && git fetch && git pull origin master && exit"
only:
- master
Like the reference notes
test:
stage: test
script: ./test
only:
- merge_requests
see https://docs.gitlab.com/ee/ci/merge_request_pipelines/index.html

How Do I Echo Yaml To A File In Bitbucket Pipelines

I want to write a .bowerrc file at the start of my pipeline.
echo '{ "allow_root": true }' > /root/.bowerrc
How do I escape all the quotes and colons to make it valid in yaml?
image: java:8
pipelines:
default:
- step:
script:
- echo '{ "allow_root": true }' > /root/.bowerrc
just remove the whitespaces
- npm install -g bower && echo '{"allow_root":true}' > /root/.bowerrc && npm install -g grunt-cli

Travis if statement does not execute

For some reason when I use an if statement it will not execute the configure for it. Here is my full travis.yml below.
travis.yml:
language: php
php:
- '5.6.32'
- '7.0.26'
- '7.1.12'
- '7.2.0'
os:
- windows
- linux
git:
depth: 1
matrix:
fast_finish: true
sudo: false
before_install:
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2.0" ]]; then git clone -b stable https://github.com/jedisct1/libsodium.git; fi
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2.0" ]]; then cd libsodium && sudo ./configure && sudo make check && sudo make install && cd ..; fi
install:
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2.0" ]]; then pecl install libsodium; fi
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2.0" ]]; then echo "extension=sodium.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi
- travis_retry composer install --no-interaction
- wget -c -nc --retry-connrefused --tries=0 https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar
- chmod +x coveralls.phar
- php coveralls.phar --version
before_script:
- mkdir -p build/logs
- ls -al
script:
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
after_success:
- travis_retry php coveralls.phar -v
branches:
only: master
cache:
directories:
- vendor
- $HOME/.cache/composer
So for some reason
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2.0" ]]; then
export git clone -b stable https://github.com/jedisct1/libsodium.git;
fi
does not work and I got this solution from this
My goal is to execute certain lines on certain PHP version.
Is there anything I missed?
EDIT:
you extracted the first 3 characters of the TRAVIS_PHP_VERSIONand compared it with 5 characters.. of course that doesn't work. you can either try:
if [[ ${TRAVIS_PHP_VERSION:0:5} == "7.2.0" ]]
or
if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2" ]]
END EDIT
I had some problems running scripts inside the Travis config file, since i wanted blank lines and comments and Travis got confused with that. So in general i would recommend to do the scripting in a separate bash file.
The easiest way is to do all the bash scripting in a script file and use the script files in your travis.yml
before_install: ./travis-scripts/before_install.sh
Now you can write your scripts with the bash syntax and they work right away.
If you still want to write the scripts inside the travis file, try that (not everything worked for me everytime):
install: >
if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2" ]]; then pecl install libsodium; fi;
if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.2" ]]; then echo "extension=sodium.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;
travis_retry composer install --no-interaction;
wget -c -nc --retry-connrefused --tries=0 https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar;
chmod +x coveralls.phar;
php coveralls.phar --version;
Or leave it as it is and only put the if statements in quotes.

Resources