Gitlab CI multiple branches
Asked Answered
E

3

11

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
Emir answered 30/3, 2018 at 6:5 Comment(0)
P
13

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
Petrolic answered 3/4, 2018 at 11:48 Comment(2)
How to do this? This is two diffrent linux server (not application server). From where the second gitlab- runner knows whith branch they should deploy?Emir
@dbrekelmans, is this possible on gitlab.com or only if you have your own servers?Dorrisdorry
H
4
only
 - dev
 - staging
 - master
Havens answered 29/6, 2019 at 15:51 Comment(0)
I
2

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

Inwrought answered 6/6, 2022 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.