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

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/

Related

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.

How to run a script from repo A to the pipeline B in Gitlab

I have two repositories in GitLab, repositories A and B let's say.
Repo A contains:
read_ci.yml
read_ci.sh
read_ci.yml contains:
stages:
- initialise
create checksum from pipeline:
stage: initialise
script:
- chmod +x read_ci.sh
- source ./read_ci.sh
Repo B contains:
gitlab-ci.yml
gitlab-ci.yml contains:
include:
project: 'Project/project_name'
file:
- '.gitlab-ci.yml'
ref: main
Obviously, this doesn't do what my intention is.
What I want to achieve is in the project B pipeline to run the project A script.
The reason is that I want project A to be called from multiple different pipelines and run there.
an alternative to this for GitLab: Azure Pipelines. Run script from resource repo
Submodules would absolutely work as Davide mentions, though it's kinda like using a sledgehammer to hang a picture. If all you want is a single script from the repository, just download it into your container. Use the v4 API with your CI_JOB_TOKEN to download the file, then simply run it using sh. If you have many files in your secondary repository and want access to them all, then use Submodules as Davide mentiones, and make sure your CI job retrieves them by setting the submodule strategy like this:
variables:
GIT_SUBMODULE_STRATEGY: normal
If you want to run the project A script in the project B pipeline, you can add the repository B as a git submodule in A
git submodule add -b <branch-B> <git-repository-B> <target-dir>
You need also to add in the CI job, the variable GIT_SUBMODULE_STRATEGY: recursive.

Is it possible in gitlab runner to checkout the repo without .git folder during deployment?

During deployment using gitlab runner it checkouts out the codebase including the .git folder, I presume this is the default behaviour?
Without manually cleaning the .git folder in our deployment script, is there an option to use git archive instead?
eg.
deploy-dev:
when: manual
stage: deploy
script:
- bash deploy.sh $CI_COMMIT_REF_SLUG $CI_BUILD_TOKEN
include-git: false
I have tried searching on google but couldn't find any relevant results (maybe due to bad search keywords)

GitLab CI including a project

So I have many many small projects on gitlab, all use the same CI script to build. To make entire system cleaner I put the CI script in its own project and the small projects CI script just pull the script from the central project i.e.
stages:
- build
include:
- project: 'Company/CI-Templates'
file: '/Swift-Apple/Swift_Apple_Build.yml'
With the central script being like
stages:
- build
before_script:
- git submodule sync --recursive
- git submodule update --init --recursive
build_project:
stage: build
script:
- xcodebuild archive -scheme "$AppName" -archivePath "build/App.xcarchive"
- xcodebuild -exportArchive -archivePath "build/App.xcarchive" -exportPath "build/" -exportOptionsPlist "ExportOptions.plist"
- appcenter login --token $AppCenterToken
- appcenter distribute release -f "$IPAPath" -g Collaborators -r "$Reason" --app $AppCenterPath
Now this runs as expected, the central script when called runs local in the project. The issue I have is "ExportOptions.plist" is a separate file that ideally id like to be in the central CI location, but as the script runs local when called, im unsure what file path to put in.
Could you download "ExportOptions.plist" from your central CI project with curl? Then you can load it locally.

Building a Docker image for a Node.js app in GitLab CI

I'm working on a Node.js application for which my current Dockerfile looks like this:
# Stage 0
# =======
FROM node:10-alpine as build-stage
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install
COPY . ./
RUN yarn build
# Stage 1
# =======
FROM nginx:mainline-alpine
COPY --from=build-stage /app/build /usr/share/nginx/html
I'd like to integrate this into a GitLab CI pipeline but I'm not sure if I got the basic idea. So far I know that I need to create a .gitlab-ci.yml file which will be later picked up by GitLab.
My basic idea is:
I push my code changes to GitLab.
GitLab builds a new Docker image based on my Dockerfile.
GitLab pushes this newly create image to a "production" server (later).
So, my question is:
My .gitlab-ci.yml should then contain something like a build job which triggers... what? The docker build command? Or do I need to "copy" the Dockerfile content to the CI file?
GitLab CI executes the pipeline in the Runners that need to be registered into the project using generated tokens (Settings/CI CD/Runners). You also can used Shared Runners for multiple projects. The pipeline is configured with the .gitlab-ci.yml file and you can build, test, push and deploy docker images using the yaml file, when something is done in the repo (push to branch, merge request, etc).
It’s also useful when your application already has the Dockerfile that
can be used to create and test an image
So basically you need to install the runner, register it with the token of your project (or use Shared Runners) and configure your CI yaml file. The recommended aproach is docker in docker but it is up to you. You can also check this basic example. Finally you can deploy your container directly into Kubernetes, Heroku or Rancher. Remember to safely configure your credentials and secrets in Settings/Variables.
Conclusion
GitLab CI is awesome, but I recommend you to firstly think about your git workflow to use in order to set the stages in the .gitlab-ci.yml file. This will allow you to configure your node project as a pipeline an then it would be easy to export to other tools such as Jenkins pipelines or Travis for example.
build job trigger:
option 1:
add when: manual in the job and you can run the job by manual in CI/CD>Pipelines
option 2:
only:
- <branchname>
in this case the job start when you push into the defined branch
(this my personal suggest)
option 3:
add nothin' and the job will run every time when you push code
Of corse you can combine the options above.
In addition may star the job with web request by using the job token.
docker build command will work in pipeline. I think in script section.
Requirements docker engine on the gitlab-runner which pick the job.
Or do I need to "copy" the Dockerfile content to the CI file?
no

Resources