Gitlab CI multiple branches - continuous-integration

I have two branches: master and test. When I push to the master branch, my code is deployed to the first server by gitlab-ci. I want to deploy to a different server whenever I push to the test branch. Is this possible using Gitlab CI?
master - 10.10.10.1
test - 10.10.10.2
My gitlab-ci.yml:
maven_build:
script:
- mvn install
- /opt/payara41/bin/./asadmin --passwordfile /home/asadminpass --user admin undeploy myApplication-ear-1.0-SNAPSHOT
- sudo /etc/init.d/glassfish restart
- /opt/payara41/bin/./asadmin --passwordfile /home/asadminpass --host localhost --user admin deploy --force /home/gitlab-runner/builds/10b25461/0/myapp/myAppPrototype/myApp-ear/target/myApplication-SNAPSHOT.ear
only:
- master

You're on the right track with only:.
Simply create two different steps, one with only: master and one with only: test.
Change the script: to deploy to a different server.
deploy_master:
script:
- <script to deploy to master server>
only:
- master
deploy_test:
script:
- <script to deploy to test server>
only:
- test

only
- dev
- staging
- master

If I understand what you are asking you can do the following
for master
Pushing changes:
stage: deploy
rules:
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"
for test
Pushing changes:
stage: deploy
rules:
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "test"
this will define the when based on your branch.
As for the how to deploy
in your script section you can add for master
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
- aws configure set region $AWS_DEFAULT_REGION
for test
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID_TEST
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY_TEST
- aws configure set region $AWS_DEFAULT_REGION_TEST
add all the variable in Settings->CICD ->Variables

Related

Gitlab pipeline error With CD/CI for AWS ec2 debian instance: This job is stuck because you don't have any active runners online

I want to create a CI/CD pipeline between gitlab and aws ec2 deployment.
My repository is nodejs/express web server project.
And I created a gitlab-ci.yaml
image: node:latest
cache:
paths:
- node_modules/
stages:
- build
- test
- staging
- openMr
- production
before_script:
- apt-get update -qq && apt-get install
Build:
stage: build
tags:
- node
before_script:
- yarn config set cache-folder .yarn
- yarn install
script:
- npm run build
Test:
stage: test
tags:
- node
before_script:
- yarn config set cache-folder .yarn
- yarn install --frozen-lockfile
script:
- npm run test
Deploy to Production:
stage: production
tags:
- node
before_script:
- mkdir -p ~/.ssh
- echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
script:
- bash ./gitlab-deploy/.gitlab-deploy.prod.sh
environment:
name: production
url: http://ec2-url.compute.amazonaws.com:81
When I push a new commit pipeline failed on build step. And I get a warning as :
This job is stuck because you don't have any active runners online or
available with any of these tags assigned to them: node
I checked my runner on gitlab settings/CI/CD
After that I checkked server
admin#ip-111.222.222.111:~$ gitlab-runner
statusRuntime platform arch=amd64 os=linux pid=18787 revision=98daeee0 version=14.7.0
FATAL: The --user is not supported for non-root users
You need to remove the tag node from your jobs. Runner tags are used to define which runner should pick up your jobs (https://docs.gitlab.com/ee/ci/runners/configure_runners.html#use-tags-to-control-which-jobs-a-runner-can-run). As there is no runner available which supports the tag node, your job gets stuck.
It doesn't look like your pipeline has any special requirements so you can just remove the tag so it can be picked up by every runner.
The runner that can be seen in your screenshot supports the tag shop_service_runner. So another option would be to change the tag node to shop_service_runner which would lead to this runner (and every runner with the same tags) being able to pick up this job.

GitLab Shared runner, deploy spring boot microservice app to custom server

Before i start, let me tell you that i'm newbie to Gitlab CI file :)
i'm looking to automate deployments of spring boot microservice app to custom server (a namecheap VPS).
(I'm using the Gitlab shared runner)
i have used jHipster ci-cd to generate the .gitlab-ci.yml file. Doing that, i have: build, package, release stages working.
i even could see the image repository built in Gitlab Container registery.
What last, is the deployment. i know that i have to use a docker container to deploy it, but i don't know how.
I'm stuck in deploying the image repository to my VPS.
(as it's my first microservice, my VPS is still new, having only java installed).
Here is my .gitlab-ci.yml file:
#image: jhipster/jhipster:v6.9.0
image: openjdk:11-jdk
cache:
key: "$CI_COMMIT_REF_NAME"
paths:
- .maven/
stages:
- check
- build
# - test
# - analyze
- package
- release
- deploy
before_script:
- chmod +x mvnw
- git update-index --chmod=+x mvnw
- export NG_CLI_ANALYTICS="false"
- export MAVEN_USER_HOME=`pwd`/.maven
nohttp:
stage: check
script:
- ./mvnw -ntp checkstyle:check -Dmaven.repo.local=$MAVEN_USER_HOME
maven-compile:
stage: build
script:
- ./mvnw -ntp compile -P-webpack -Dmaven.repo.local=$MAVEN_USER_HOME
artifacts:
paths:
- target/classes/
- target/generated-sources/
expire_in: 1 day
#maven-test:
# stage: test
# script:
# - ./mvnw -ntp verify -P-webpack -Dmaven.repo.local=$MAVEN_USER_HOME
# artifacts:
# reports:
# junit: target/test-results/**/TEST-*.xml
# paths:
# - target/test-results
# - target/jacoco
# expire_in: 1 day
maven-package:
stage: package
script:
- ./mvnw -ntp verify -Pprod -DskipTests -Dmaven.repo.local=$MAVEN_USER_HOME
artifacts:
paths:
- target/*.jar
- target/classes
expire_in: 1 day
# Uncomment the following line to use gitlabs container registry. You need to adapt the REGISTRY_URL in case you are not using gitlab.com
docker-push:
stage: release
variables:
REGISTRY_URL: registry.gitlab.com
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHA
dependencies:
- maven-package
script:
- ./mvnw -ntp compile jib:build -Pprod -Djib.to.image=$IMAGE_TAG -Djib.to.auth.username=gitlab-ci-token -Djib.to.auth.password=$CI_BUILD_TOKEN -Dmaven.repo.local=$MAVEN_USER_HOME
docker-deploy:
image: docker:stable-git
stage: deploy
script:
when: manual
only:
- master
Thanks for your help :)

How to deploy only changed files via atlassian/ftp-deploy:0.2.0 in Bitbucket Pipelines?

I am new to BitBucket pipelines, as I used Webhook for deploying my changes to FTP.
I have set reccommended atlassian/ftp-deploy:0.2.0 pipeline and it works fine, BUT I would like to set that ONLY CHANGED files are taken and sent to FTP.
image: node:10.15.3
pipelines:
default:
- step:
name: FTP Deploy
script: # Modify the commands below to build your repository.
- pipe: atlassian/ftp-deploy:0.2.0
variables:
USER: 'myFTPusername'
PASSWORD: 'myFTPpass'
SERVER: 'myFTPserver'
REMOTE_PATH: 'myFTPpath'
- step:
name: Deploy message
deployment: test
script:
- echo "Deploying to main environment"
Any help how to set this up so it sends only changed files to FTP?
Expected output is code for bitbucket-pipelines.yml
atlassian/ftp-deploy:0.3.6 delete all files, I found this image wagnerstephan/bitbucket-git-ftp:latest it's very useful, light and runs well and updates only changed files.
The first pipeline will run with an error, so select the commit then run again the pipeline with the init option selected.
image: wagnerstephan/bitbucket-git-ftp:latest
pipelines:
custom:
init:
- step:
script:
- git reset --hard
- git ftp init -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST/
deploy:
- step:
script:
- git reset --hard
- git ftp push -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST/--all
branches:
develop:
- step:
name: Deploy Develop
deployment: develop
script:
- git reset --hard
- git ftp push -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST/
master:
- step:
name: Deploy production
deployment: production
script:
- git reset --hard
- git ftp push -u "$FTP_USERNAME" -p "$FTP_PASSWORD" ftp://$FTP_HOST/
NOTE: check the name of the environments in the Bitbucket repository.

Unable to connect to a target server via SSH from a GitLab pipeline?

I have set up .gitlab-ci.yml. I am unable to login to the production server from gitlab. I have set the private and public key variables of my server in GITLAB but still getting timeout error in pipeline.
job1:
stage: build1
script:
- mvn package
variables:
SSH_PUBLIC_key: "$SSH_PUBLIC_key"
SSH_PRIVATE_KEY: "$SSH_PRIVATE_KEY"
artifacts:
paths:
- server
script:
- scp "myjar" root#"myIP":/tmp
job1:
stage: build1
script:
- mvn package
variables:
SSH_PUBLIC_key: "$SSH_PUBLIC_key"
SSH_PRIVATE_KEY: "$SSH_PRIVATE_KEY"
artifacts:
paths:
- server
script:
- scp "myjar" root#"myIP":/tmp
timeout error comes, when the instance (in your case the production instance) is not reachable from GitLab (can be hosted on VM, Kubernetes, etc). Please check if you are able to perform telnet/ssh manually from the GitLab hosted VM
Replace myIP with proper values and see if that helps.
telnet <myIP> 22
ssh <myIP>

How to deploy laravel app on digitalocean after succesfull test pass on Gitalab CI

I have managed to succesfully run laravel test build on Gitlab CI using Gitlab Runner on digitaocean (With help from tutorial HOW TO: LARAVEL TESTING ON GITLAB CI WITH DOCKER)
Now I am wondering how I can deploy it after succesfull test.
This is my deploy process on my staging env:
cd MY_PROJECT_ROOT_DIR
git reset --hard HEAD
git checkout master
git pull
composer install
composer update
php artisan migrate
php artisan db:seed
How I can manage to include this deploy after test is done?
My configuration of GitLab Runner is the same as those files on this repo
This is the content of my .gitlab-ci.yml file:
before_script:
- bash .gitlab-ci.sh
variables:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: secret
phpunit:php-laravel-env:mysql:
image: woohuiren/php-laravel-env:latest
services:
- mysql:latest
script:
- php vendor/bin/phpunit --colors
How I should change this file in order to execute deploy script after test passed?
You need to use "stages"
Basically you would update your current test setup to include a stage.
stages:
- test
- deploy
phpunit:php-laravel-env:mysql:
stage: test
image: woohuiren/php-laravel-env:latest
services: ...
deploy_my_site:
stage: deploy
...
These stages will get run in sequence by GitLab but will stop if there is any error.
If you are using Forge you could use the deploy stage to trigger a script to curl the forge deploy hook.

Resources