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

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)

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 to copy files from projects artifact to another project using cicd

i go a CICD that make an artifact and save them in public folder
i need to copy some files of these artifacts to another project
what script i can't use or what the best way to do it?
Assuming, you want to copy file into another gitlab project.
Once artifact is built in a job, in a stage (ie : build), it will be available in all jobs of the next stage (ie : deploy).
build-artifact:
stage: build
script:
- echo "build artifact"
artifacts:
name: "public"
paths:
- "public/"
deploy_artifact:
stage: deploy
script:
- cd public/ # here are your artifact files.
- ls -l
- cd ..
# now implement copy strategy,
# for example in a git repo :
- git clone https://myusername:mypassord#myrepo.gitlab.com/mygroup/another_project.git
- cd another_project
- cp ../public/source_file ./target_file
- git add target_file
- git commit -m "added generated file"
- git push
If your destination is not a git repository, you can simply replace by a scp directly to server, or any other copy strategy, directly in the script.
Another way to do it is to get the last artifact of project A, inside the ci of Project B.
Please see https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html#retrieve-job-artifacts-for-other-projects
When you do trigger to downstream pipelines, just use needs from job that push artifacts, then it will be passed to downstream.

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.

Is it possible to create gitlab-pages per branch [duplicate]

This question already has answers here:
Deploying GitLab pages for different branches
(4 answers)
Closed 3 years ago.
I have a vanilla JavaScript application that I host on GitLab Pages. Recently, I have been making changes and bug fixes that have been breaking the site, and I haven't noticed until already having pushed the changes.
In an effort to reduce user exposure to bugs, I would like to publish two sites in separate folders:
public/ : master branch; the official website
public/staging/ : staging branch; the nightly build
I would like for these to correspond to two different branches: master and staging
Reading GitLab CI for GitLab Pages, it sounds like this is not even possible. I'm hoping I'm reading this wrong.
default:
image: node:latest
test:
stage: test
script:
- npm install
- node test.js
only:
- staging
- master
staging:
stage: deploy
environment: staging
script:
- mkdir -p public/staging
- cp -r www public/staging
artifacts:
paths:
- public
only:
- staging
pages:
stage: deploy
environment: production
script:
- mkdir -p public
- cp -r www public
artifacts:
paths:
- public
only:
- master
Is this possible? Is it possible to deploy two different folders from two different branches?
Interestingly, it is possible to post from any branch, just not from any job.
To do this, I needed to make two changes:
I need to know the current state of the published data
I need to change directory based on the current branch
GitLab has the ability to cache folders. Generally this is used to speed up builds by caching downloaded drivers. There is no reasons I could not use this to store the public folder. This way when I make changes to staging, I will remember the state of the root application:
cache:
paths:
- public
The next trick would be to publish pages to the appropriate folder, depending on current branch being built. To do this, we can look to GitLab CI/CD Environment Variables; in particular:
CI_COMMIT_REF_SLUG: The current branch
CI_DEFAULT_BRANCH: the default branch (master)
Knowing these two values, we can do a bit of bash to determine the correct place to write the content to.
pages:
stage: deploy
script:
- dir="$CI_COMMIT_REF_SLUG"
- if [ "$CI_COMMIT_REF_SLUG" == "$CI_DEFAULT_BRANCH" ]; then dir=""; fi;
- dir="public/$dir"
- echo "Deploying to $dir"
- mkdir -p $dir
- cp -r www $dir
artifacts:
paths:
- public
only:
- staging
- master
Don't forget to limit pages to only staging and master.
WARNING
I'm not satisfied with this.
I think it would be better to maintain the cache somewhere completely different and copy them in at a later stage, but completely re-writing the public folder each time.
The current solution will build cruft over time, but the basic idea is sound.
You can only publish changes to GitLab pages through your master branch, just as you describe. The only thing that GitLab pages does though, is to put files in the public folder in the job called pages. These files can be whatever files that you want though, as long as you manage to get them to this folder through the GitLab job.
You could try something like this:
pages:
...
script:
- mkdir -p public
- cp -r www public
- git checkout origin/staging
- mkdir -p public/staging
- cp -r www public/staging
I haven't tested this, so please let me know if it doesn't work!
If you run a GitLab job, it usually has all of the git history of your repo. There are settings that changes this though, both in git and in GitLab, so you have to make sure that you always get all of your git history to the pages job. If you have a folder that hasn't been added to git, like public, git should not change anything in it when you checkout another branch.
I think that you should also be able to set up the GitLab pages job with a schedule, so that the pages job is run even if only the staging branch has been updated, but not the master branch.

Gitlab artifact isnt created

i have the following .gitlab.yaml file
stages:
- build
- test
compile:
stage:build
script:
- stuff_happening
test_1:
stage: test
script:
- do_something_1
artifacts:
when: on_failure
name: "$CI_COMMIT_REF_NAME"
paths:
- /root/dir
When test_1 is executed , it creates a folder dir inside root
I want to add it to artifacts but , i get an error saying:
no matching files. If I add - ls root in the job , I can see the folder.
There is an open question in support forum, but still no response there.
Can anyone help? Thanks
The issue here is that you are trying to upload files that are outside of the project's scope.
From the official documentation on artifacts:
You can only use paths that are within the project workspace.
The reason is obvious; if a git runner were allowed to upload anything outside its workspace, it would cause a serious security issues.
However, if you really wanted to upload anything outside the runner's workspace, you might want to try copying the files outside into the project's root folder, and uploading it from there.
The git runners are usually registered as user:group git-runner:git-runner and so it will likely require sudo privilege if you wanted it to copy files from /root.
According to https://docs.gitlab.com/ee/ci/yaml/README.html#artifacts
You can try adding an additional slash after your path.
paths:
- /root/dir/
Examples from the link above:
Send all files in binaries and .config:
artifacts:
paths:
- binaries/
- .config

Resources