Jenkins pipeline advise required - linux

I want to create a very simple pipeline that will clone a repo from Github and then copy a JSON file to a directory. But there is a catch here and requires your guide if that is possible or not. It is on a Linux machine and the folder structure is like this:-
/var/www/myjenkins.com/
/var/www/myapi.com/html/json/
On my Linux machine, this Jenkin is running which is deployed on the location as /var/www/myjenkins.com/ and the new pipeline which I need to create in Jenkin has to perform the job of copying a JSON file (after git clone) to another location as /var/www/myapi.com/html/json/ which is out of root level of Jenkins. Is that really possible to traverse out of the root level of Jenkins then copy the file into another location?
Would be glad if you can advise on this. Thanks

Related

Gitlab where is config.toml

Im working on adding code quality to gitlab.
one of the step requires making changes to config.toml.
I have .gitlab-ci.yml at root level of project repository.and pipeline picks up this file.
where do i define config.toml ? at root level or i need to create a new folder like
/etc/gitlab-runner/config.toml
The config.toml file is what configures a runner. You wouldn't have one unless you're running your own gitlab-runners, in which case it would be in /etc/gitlab/config.toml on the host running the runner. More information on Gitlab Runner and running it yourself is available here.

Create a Jenkins job to deploy a file on a server

I am working on a Jenkins job that will download a specific file from a GIT repo and then deploy it on a server to a specified location.
Jenkins job will ask below variables from the user:
Commit ID and name of the file in repo that needs to be copied.
Path on the target server where the file needs to be copied.
Here's what I have done so far:
Installed and configured the GIT so it will pull the repo to the Jenkins server
Configured the target servers so Jenkins can copy the file to one of the 3 target servers using 'execute shell option'.
In the build action, wrote an ssh command that will checkout a specific commit of a file from the git repo.
Wrote a shell command that will scp the checked out file and to the one of the 3 target servers. Of course the target server will be provided by the user.
Here's what the code looks like so far:
git checkout $commit $filename
scp $filename username#$servername:/path/to/deploy/
The challenge is how to feed the variables to the shell script.
Is it even possible to do it this way? Any help would be appreciated.
Below solution worked.
Added the GIT repo to the Jenkins freestyle job
After checking the option, 'this project is parameterized', I added 3 variables as shown.
Then modified the shell script as below
#!/bin/bash
pwd
ssh ansible#$server_name "cp "$File_path"/"$file_name" "$File_path"/"$file_name"_"$(date +"%Y-%m-%d")";"
scp $file_name ansible#$server_name:$File_path

build.artifactstagingdirectory doesn't exist Azure Devops

I'm trying to publish a ClickOnce application. To do so, I'm building the project with the msbuild publish target argument, copying the files to the artifact staging directory, then attempting to upload that directory via FTP.
However, the ftpupload task is failing:
With the following error:
550 The system cannot find the file specified.
For whatever reason, removing the / at the begging of the Remote Directory field fixed it. Most say the / at the beginning is needed, however. It might vary from one FTP server to another.
550 The system cannot find the file specified.
This error doesn't indicate that the specified files are missing in build.artifactstagingdirectory folder.
Actually the task would always succeed with the warning Could not find any files to upload even when the build.artifactstagingdirectory folder is empty:
To make the FTP upload task work successfully, you may need to check your Remote directory input of that task and make sure your FTP remote Server is configured well.
1.Assuming your remote home directory is C:\FTPfolder. You should use /TestFolder/as input if you want to upload the files to C:\FTPfolder\TestFolder. (Be careful about the slash)
2.If your upload task would create a new folder in remote directory during this process, you should make sure the user account has the related directory access like creating sub-folders...
PS: I run this pipeline in my self-hosted agent to upload files to remote directory in remote machine with Serv-U ftp server. The task works well in my side.

Create a git repository on server side

I have a big problem and I can't understand this topic. I have a server with a website. I created a repository there with git init. Than I made a git add * to add all files from my server to the repository. Than I made a commit to commit all files to the repository.
Than I cloned it with git clone ssh://username#mysite.com/wordpress/.git to my local client.
All worked fine and I got a copy from my project. No I changed something on my local version and made a commit with a push. I looked in FileZilla but the content in the file don't changed. In the other direction when I changed something on the sever and pulled it to the local copy I saw the changes. Do you know why the changes which I made on the local copy are not visible on my sever?
Thank you for your help!
You need to push changes to a central repository that both your local machine and server can pull from (or add them as remotes for each other). A service such as GitHub works nicely for this. Here are instructions for a full workflow that works well for this. Updated instructions can be found in this gist. This workflow uses hooks to do the heavy lifting so that updates to your server are automated.
Using Git to Manage a Live Web Site
Overview
As a freelancer, I build a lot of web sites. That's a lot of code changes to track. Thankfully, a Git-enabled workflow with proper branching makes short work of project tracking. I can easily see development features in branches as well as a snapshot of the sites' production code. A nice addition to that workflow is that ability to use Git to push updates to any of the various sites I work on while committing changes.
You'll need to have Git installed on your development machines as well as on the server or servers where you wish to host your website. This process can even be adapted to work with multiple servers such as mirrors behind a load balancer.
Setting up Passwordless SSH Access
The process for updating a live web server relies on the use of post hooks within the Git environment. Since this is fully automated, there is no opportunity to enter login credentials while establishing the SSH connection to the remote server. To work around this, we are going to set up passwordless SSH access. To begin, you will need to SSH into your server.
ssh user#hostname
Next, you'll need to make sure you have a ~/.ssh in your user's home directory. If not, go ahead and create one now.
mkdir ~/.ssh
On Mac and Linux, you can harness the power of terminal to do both in one go.
if [ ! -d ~/.ssh ]; then mkdir ~/.ssh; fi
Next you'll need to generate a public SSH key if you don't already have one. List the files in your ~/.ssh directory to check.
ls -al ~/.ssh
The file you're looking for is usually named similarly to id_rsa.pub. If you're not sure, you can generate a new one. The command below will create an SSH key using the provided email as a label.
ssh-keygen -t rsa -b 4096 -C "your_email#example.com"
You'll probably want to keep all of the default settings. This will should create a file named id_rsa in the ~/.ssh directory created earlier.
When prompted, be sure to provide a secure SSH passphrase.
If you had to create an SSH key, you'll need to configure the ssh-agent program to use it.
ssh-add ~/.ssh/id_rsa
If you know what you are doing, you can use an existing SSH key in your ~/.ssh directory by providing the private key file to ssh-agent.
If you're still not sure what's going on, you should two files in your ~/.ssh directory that correspond to the private and public key files. Typically, the public key will be a file by the same name with a .pub extension added. An example would be a private key file named id_rsa and a public key file named id_rsa.pub.
Once you have generated an SSH key on your local machine, it's time to put the matching shared key file on the server.
ssh user#hostname 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub
This will add your public key to the authorized keys on the remote server. This process can be repeated from each development machine to add as many authorized keys as necessary to the server. You'll know you did it correctly when you close your connection and reconnect without being prompted for a password.
Configuring the Remote Server Repository
The machine you intend to use as a live production server needs to have a Git repository that can write to an appropriate web-accessible directory. The Git metadata (the .git directory) does not need to be in a web-accessible location. Instead, it can be anywhere that is user-writeable by your SSH user.
Setting up a Bare Repository
In order to push files to your web server, you'll need to have a copy of your repository on your web server. You'll want to start by creating a bare repository to house your web site. The repository should be set up somewhere outside of your web root. We'll instruct Git where to put the actual files later. Once you decide on location for your repository, the following commands will create the bare repository.
mkdir mywebsite.git
cd mywebsite.git
git init --bare
A bare repository contains all of the Git metadata without any HEAD. Essentially, this means that your repository has a .git directory, but does not have any working files checked out. The next step is to create a Git hook that will check out those files any time you instruct it to.
If you wish to run git commands from the detached work tree, you'll need to set the environmental variable GIT_DIR to the path of mywebsite.git before running any commands.
Add a Post-Receive Hook
Create a file named post-receive in the hooks directory of your repository with the following contents.
#!/bin/sh
GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f
Once you create your hook, go ahead and mark it as executable.
chmod +x hooks/post-receive
GIT_WORK_TREE allows you to instruct Git where the working directory should be for a repository. This allows you to keep the repository outside of the web root with a detached work tree in a web accessible location. Make sure the path you specify exists, Git will not create it for you.
Configuring the Local Development Machine
The local development machine will house the web site repository. Relevant files will be copied to the live server whenever you choose to push those changes. This means you should keep a working copy of the repository on your development machine. You could also employ the use of any centralized repository including cloud-based ones such as GitHub or BitBucket. Your workflow is entirely up to you. Since all changes are pushed from the local repository, this process is not affected by how you choose to handle your project.
Setting up the Working Repository
On your development machine, you should have a working Git repository. If not, you can create on in an existing project directory with the following commands.
git init
git add -A
git commit -m "Initial Commit"
Add a Remote Repository Pointing to the Web Server
Once you have a working repository, you'll need to add a remote pointing to the one you set up on your server.
git remote add live ssh://server1.example.com/home/user/mywebsite.git
Make sure the hostname and path you provide point to the server and repository you set up previously. Finally, it's time to push your current website to the live server for the first time.
git push live +master:refs/head/main
This command instructs Git to push the current main branch to the live remote. (There's no need to send any other branches.) In the future, the server will only check out from the main branch so you won't need to specify that explicitly every time.
Build Something Beautiful
Everything is ready to go. It's time to let the creative juices flow! Your workflow doesn't need to change at all. Whenever you are ready, pushing changes to the live web server is as simple as running the following command.
git push live
Setting receive.denycurrentbranch to "ignore" on the server eliminates a warning issued by recent versions of Git when you push an update to a checked-out branch on the server.
Additional Tips
Here are a few more tips and tricks that you may find useful when employing this style of workflow.
Pushing Changes to Multiple Servers
You may find the need to push to multiple servers. Perhaps you have multiple testing servers or your live site is mirrored across multiple servers behind a load balancer. In any case, pushing to multiple servers is as easy as adding more urls to the [remote "live"] section in .git/config.
[remote "live"]
url = ssh://server1.example.com/home/user/mywebsite.git
url = ssh://server2.example.com/home/user/mywebsite.git
Now issuing the command git push live will update all of the urls you've added at one time. Simple!
Ignoring Local Changes to Tracked Files
From time to time you'll find there are files you want to track in your repository but don't wish to have changed every time you update your website. A good example would be configuration files in your web site that have settings specific to the server the site is on. Pushing updates to your site would ordinarily overwrite these files with whatever version of the file lives on your development machine. Preventing this is easy. SSH into the remote server and navigate into the Git repository. Enter the following command, listing each file you wish to ignore.
git update-index --assume-unchanged <file...>
This instructs Git to ignore any changes to the specified files with any future checkouts. You can reverse this effect on one or more files any time you deem necessary.
git update-index --no-assume-unchanged <file...>
If you want to see a list of ignored files, that's easy too.
git ls-files -v | grep ^[a-z]
References
Deploy Your Website Changes Using Git
A simple Git deployment strategy for static sites
Using Git to manage a website
Ignoring Local Changes to Tracked Files in Git
pushing the code merely updates the remote repository's references.
It doesn't change the checked out working copy.
Consider that you could add a colleague's repository as a remote. If you pushed and the behaviour was that it would auto-checkout that new code, that would affect what they're working on.
It sounds like what you really want is a continuous integration tool, be it something full featured or merely an rsync triggered from a git hook.
you should only ever push to a bare repository (unless you know exactly what you are doing; and even then, you should only ever push to a bare repository).
you shouldn't clone a working copy's .git/ directory.

How to connect local and remote Mercurial repos?

Typically, in Mercurial, I create a new project by:
Create a new remote repo
Clone the repo locally
Make changes to the local repo
Push those changes to the remote repo
The "remote repo" here is actually our "central/originating" DVCS server (http://ourhg.ourorg.example.com, etc.).
I am now in a situation where I had to use a code generation tool to produce the source code for a simple web app. So the source code exists before the remote repo exists on our hg server. I'm looking for the exact shell commands I need to execute to get this properly pushed to the remote repo.
I believe it should be something like this:
Use the code generator to generate the code, say, at /home/myuser/myapp.
Initialize an hg repo for myapp locally on my machine (hg init)
Add the generated source code for myapp to this local repo (hg add, then hg commit)
On ourhg.ourorg.example.com, create the new remote repo (manual steps)
???
Push the changes sitting in my local repo to the remote repo (hg push)
I know there is something missing in between Step #4 (creation of the remote repo) and Step #6 (pushing to the remote repo). There surely needs to be some "connection" step where my local repo and the remote repo realize they represent the same project/source code/etc. This is my hangup here, so I ask: what is Step #5?
what is Step #5?
Discover URL of this repo. Because it's empty repo, you can don't worry about "related|unrelated"
There's nothing you need to do to associate them. You can do hg push URL_OF_REMOTE on your local and it will work. If you don't want to have to provide the URL each time you can edit (creating if necessary) .hg/hgrc in the repo and set the default= value in the [paths] section. Something like this
[paths]
default=URL_OF_REMOTE
That's optional though.
Just use your origial steps, with caveat:
Create a new remote repo
Clone the repo locally (this will set default path of local to remote).
Make changes to the local repo (Use the code generator app to generate the code in the local repo).
Push those changes to the remote repo.
With this flow, you don't have to manually update the path to the remote repo.

Resources