How to change directory using hosted runner via Bitbucket pipeline - bitbucket-pipelines

I have a simple pipeline where I just want to navigate to a specific folder on the server hosting the runner.
My runner is online, I added the host in the SSH Keys section, and my step is:
- step:
name: 'Write log to server'
services:
- docker
runs-on:
- self.hosted
- linux
script:
- export HOST_PROJECT_PATH=/home/project/myproject
- ls -a
- cd $HOST_PROJECT_PATH // folder not found but exists
The ls -a shows the content of my Bitbucket repository, and I can't cd to an existing directory (directory exists on the server).
Can I do it using pipeline & runner ? Do I need to use an other service ? Can't find any example or documentation.

Related

How to deploy a gitlab project to a specific directory on the server?

I have a project in gitlab and want to deploy this to a specific directory on a Linux server using the .gitlab-ci.yml for which I am facing an issue.
I have setup a gitlab runner for "cms-project" and added .gitlab-ci.yml to the root directory
When I push to the repository, the runner fetches the commits to the following directory
home/gitlab-runner/builds/ziwwUK3Jz/0/project/cms_project
Now I want the runner to fetch the commit to the dev server which is located in
/var/www/project-cms.com/html
I have tried the changes and below is the .gitlab-ci.yml file
job_main:
type: deploy
script: cd /var/www/project-cms.com/html && git pull
deploy:
stage: deploy
only:
- master
variables:
BRANCH: master
script:
- composer install
but I am getting the following error
Removing modules/contrib/
Removing vendor/
Skipping Git submodules setup
Executing "step_script" stage of the job script
$ cd /var/www/project-cms.com/html && git pull
error: cannot open .git/FETCH_HEAD: Permission denied
The user has the root permissions to the directory.
I have already gone through this link "https://stackoverflow.com/questions/37564681/gitlab-ci-how-to-deploy-the-latest-to-a-specific-directory" but that did not help
can anyone please help me to deploy the project to the "/var/www/project-cms.com/html" directory?
It is because you did not understand the difference between the gitlab-runner and your server
This code is running inside a "machine" called gitlab runner that is responsible to execute anything that you want. But it is not your web server.
To be able to deploy your code, you need to build a script that will run inside this runner and you need to understand that your entire code is inside this runner (this is the reason that you found your code inside the /builds folder.
So you need to tell to your runner to copy your code to your webserver, using any protocol/binary like ssh, ftp, sftp etc.
Edit: If your runner is running inside your webserver, you just need to copy the code to your folder, do not need to pull with git anymore
Some examples here: https://docs.gitlab.com/ee/ci/examples/
Docs: https://docs.gitlab.com/ee/ci/
Runner docs: https://docs.gitlab.com/runner/

How can i create a file on gitlab-runner server by .gitlab-ci.yml file?

I am learning GitLab CI/CD. I use https://gitlab.com/ as repository and I installed an Ubuntu server on virtual machine as local and Gitlab-runner is running on it.
I want to create a file by .gitlab-ci.yml file by touch test.txt on Gitlab-runner server (deploy server).
For example:
build-job:
stage: build
script:
- pwd
- touch /srv/test.txt
- ls /srv/
This was run on gitlab.com, not deploy server. All config tested and worked properly.

Self-hosting gitlab autodeploy to aws ec2 server

I'm asking this because because I cannot find a running example similar to my case. I have this self hosted Gitlab in a AWS EC2 machine (let's call this "machine 1" and I want to set autodeployment to my AWS EC2 remote server , called "machine 2".
My Gitlabs installation shows (machine 1):
gitlab-ce 10.4.4
gitlab-config-template 10.4.4
gitlab-cookbooks 10.4.4
gitlab-ctl 10.4.4
gitlab-healthcheck
gitlab-monitor
gitlab-pages
gitlab-psql
gitlab-rails
gitlab-scripts
gitlab-selinux
gitlab-shell
gitlab-workhorse
I have follow gitlab instructions to setting up CI & CD on gitlab documentation in my project I want to set autodeployment. The follow steps are follow:
1.I have create runner following gitlabs doc , not much to show here except (machine 2):
url: https://url.to.my.compute.amazonaws.com
Token : token given by gitlab
Executor: shell
Tags: build deploy qa stage
2.I have created my .gitlab-ci.yml (in root project) file with (even with two yml file version created i have tried):
yml 2:
stages:
- build
- deploy
build:
stage: build
script: echo "Building the app"
deploy_staging:
stage: deploy
script:
- echo "Deploy to staging server"
yml 1:
#develop stage
deploy:
stage: deploy
before_script:
#generate ssh key
- mkdir -p ~/.ssh
- echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
script:
- bash .gitlab-deploy.sh
environment:
name: develop
url: https://my.domain.com
when: manual
3.I have set two SECRET VARIABLES
SSH_PRIVATE_KEY and DEPLOY_SERVERS (with secret key and ips respectively)
4.I have add a deploy.sh file (in root of my project)
#!/bin/bash
#Get servers list
set -f
string=$DEPLOY_SERVERS
array=(${string//,/ })
#Iterate servers for deploy and pull last commit
for i in "${!array[#]}"do
echo "Deploy project on server ${array[i]}"
ssh ubuntu#${array[i]} "cd /var/www/html/app && git pull origin develop"
done
My gitlab-runner shows me at this momment:
gitlab-runner verify
WARNING: Running in user-mode.
WARNING: The user-mode requires you to manually start builds processing:
WARNING: $ gitlab-runner run
WARNING: Use sudo for system-mode:
WARNING: $ sudo gitlab-runner...
And Running as sudo as says it show my runner :
Verifying runner... is alive runner=
Verifying runner... is alive runner=
Verifying runner... is alive runner=
but still in gitlabs ui is getting a "STUCK" tags and the job tell me to "job is stuck, check runners"
Questions:
Are this all the steps to follow?
Do you see anything (or process) I miss in all this configuration?
In my gitlab remote I have "master" permissions, is this what i need to run a runner ?
how can i debug at this point ( i'm using gitlab-runner --debug verify ) is that all I can?
Thanks in advance for you help.
When runner is "specific" , stages need a "tag" like:
stages:
- build
- deploy
build:
stage: build
script: echo "Building the app"
deploy_staging:
stage: deploy
script:
- echo "Deploy to staging server"
tags:
- deploy

How to deploy to custom server after success CI in docker environment?

I already did CI, but now I want to deploy to my server. My server is the same machine where I do CI, but I do CI in docker-executor. So I can't have acces to server folders to update production.
There is my script:
image: node:9.11.2
cache:
paths:
- node_modules/
before_script:
- npm install
stages:
- test
- deploy
test:
stage: test
script:
- npm run test
deploy:
stage: deploy
script:
#here I want to go to /home/projectFolder and make git pull, npm i, npm start
# but I can't beause I run CI in docker-environment which hasn't acces to my server's dirictories.
First of all you should consider using gitlab auto cicd ( or use it as a base to customize if you dont want to use kubernetes)
You have multiple way to do so but the simplest way should be to use an alpine image and
- install ssh (if necessary)
- load your private ssh key ( from pipeline secrets)
- run your npm commands through ssh.
The cleanest way would be :
- generating adding a valid Dockerfile to your project
- adding docker image generation for each commit on master (in your pipeline)
- Adding docker rm running image (in your pipeline)
- Adding docker run the newly generated image (in your pipeline) (by sharing your docker volume)
- Make nginx redirect to your container.
I can give more detailed advice depending on what you decide to do.
Hoping i helped.

Uploading artifacts is disabled

Does anyone know how to resolve "Missing gitlab-runner. Uploading artifacts is disabled."
I have two ssh runner on my server. One for local ssh runner and another one is remote server ssh runner. The local ssh runner working perfectly. But when I call the remote shh runner it's building successfully but it shows "Missing gitlab-runner. Uploading artifacts is disabled." so that I'm not able to call the artifact in the next stage.
The gitlab-ci.yml file looks like this:
stages:
- build
- deploy
cache:
paths:
- .gradle/wrapper
- .gradle/caches
build:
stage: build
script:
- build script
artifacts:
paths:
- build/
only:
- master
deploy:
stage: deploy
script:
- deploy script
dependencies:
- build
only:
- master
You need to install gitlab-runner also on the ssh target host. Gitlab somehow needs this to upload artifacts. This is also documented now: https://docs.gitlab.com/runner/executors/ssh.html:
If you want to upload job artifacts, install gitlab-runner on the host you are connecting to via SSH.
You can put the gitlab-runner in the path ~/bin and add ~/bin to your variable PATH.

Resources