<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nelson Romero</title>
    <description>The latest articles on Forem by Nelson Romero (@nromero125).</description>
    <link>https://forem.com/nromero125</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F113219%2Fc24d1350-73ba-452d-84a4-3494d3fe4774.jpg</url>
      <title>Forem: Nelson Romero</title>
      <link>https://forem.com/nromero125</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nromero125"/>
    <language>en</language>
    <item>
      <title>How to deploy to dokku with Gitlab CI</title>
      <dc:creator>Nelson Romero</dc:creator>
      <pubDate>Fri, 20 Sep 2019 02:18:20 +0000</pubDate>
      <link>https://forem.com/nromero125/how-to-deploy-to-dokku-with-gitlab-ci-89f</link>
      <guid>https://forem.com/nromero125/how-to-deploy-to-dokku-with-gitlab-ci-89f</guid>
      <description>&lt;p&gt;Recently in the company I work for we had the need to do automatic deployments from gitlab to staging or production, but we didn't know how to do it. So I started researching on the internet, but I couldn't find accurate information. After a good time searching and trying I could do it, so I decided to do a tutorial about it.&lt;/p&gt;

&lt;p&gt;In this tutorial we will use a Laravel(6.0) application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get started&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Assuming you already have dokku installed and configured. We need to create our project in gitlab in push some code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generating an SSH keypair for Gitlab&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We need a SSH keypair to let gitlab push our project to dokku, so we will create a new one. With the next command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-keygen -t rsa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ff77f5py9o82gq80iru4f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ff77f5py9o82gq80iru4f.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case I’ll left the passphrase empty. The command will generate to files &lt;strong&gt;gitlab-test&lt;/strong&gt; and &lt;strong&gt;gitlab-test.pub&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding the key to dokku&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will add the new key to our dokku instance so that gitlab can deploy for us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat ~/.ssh/gitlab-test.pub | ssh -i "KeyPair.pem"  user@&amp;lt;yourdomainorip&amp;gt; "sudo sshcommand acl-add dokku gitlab-test"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I use the argument -i "KeyPair.pem" because in my case, dokku is hosted in EC2, if your dokku instance is hosted in another provider, you don't need that argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat ~/.ssh/gitlab-test.pub | ssh user@&amp;lt;yourdomainorip&amp;gt; "sudo sshcommand acl-add dokku gitlab-test"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Adding the key to Gitlab&lt;/strong&gt;&lt;br&gt;
Print out the content of your private key and copy all the content:&lt;br&gt;
-----BEGIN RSA PRIVATE KEY-----&lt;br&gt;
......&lt;br&gt;
......&lt;br&gt;
......&lt;br&gt;
-----END RSA PRIVATE KEY-----&lt;/p&gt;

&lt;p&gt;then open gitlab, go to the the project that you want to deploy and go the Settings, then CI/CD.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4bl1f45bcewyr72rb4v0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4bl1f45bcewyr72rb4v0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Expand the Variables tab and add a new variable with the key name SSH_PRIVATE_KEY and the VALUE will be the content of your private key that you copied(let the state unprotected). Then save the variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The .gitlab-ci.yml file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a file called .gitlab-ci.yml in your project and add the following content:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.gitlab.com/ee/ci/yaml/" rel="noopener noreferrer"&gt;More info about this file here&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image: ilyasemenov/gitlab-ci-git-push

before_script:
  ##
  ## Install ssh-agent if not already installed, it is required by Docker.
  ## (change apt-get to yum if you use an RPM-based image)
  ##
  - 'which ssh-agent || ( apt-get update -y &amp;amp;&amp;amp; apt-get install openssh-client -y )'

  ##
  ## Run ssh-agent (inside the build environment)
  ##
  - eval $(ssh-agent -s)

  ##
  ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
  ## We're using tr to fix line endings which makes ed25519 keys work
  ## without extra base64 encoding.
  ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
  ##
  - echo "$SSH_PRIVATE_KEY"
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -

  ##
  ## Create the SSH directory and give it the right permissions
  ##
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

stages:
  - deploy

deploy to staging:
  stage: deploy
  environment: staging
  only:
    - master
  script: git-push dokku@&amp;lt;yourdomain&amp;gt;:&amp;lt;dokkuappname&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace  with the domain or ip of your dokku instance and the  with the name of your app in dokku. &lt;a href="https://docs.gitlab.com/ce/ci/ssh_keys/README.html" rel="noopener noreferrer"&gt;More info about the before_script action here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The image that we use in the file is a CI runner image that allows to deploy a Gitlab project to a git repo (useful for Dokku, Heroku, Deis, etc.). &lt;a href="https://github.com/IlyaSemenov/gitlab-ci-git-push" rel="noopener noreferrer"&gt;Here the github repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After addd this file, push your code to gitlab and open your project in gitlab, go to the CI/CD options and go to Pipelines. You will see a pipeline in process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fyvhg9dxarcfql91dunsp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fyvhg9dxarcfql91dunsp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6l4lnvyv3muhtecnia3f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6l4lnvyv3muhtecnia3f.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This pipeline will deploy your project to dokku automatically. And this is all, thanks for reading.&lt;/p&gt;

</description>
      <category>dokku</category>
      <category>laravel</category>
      <category>devops</category>
    </item>
    <item>
      <title>How to change tables structures with migration without losing your data in Laravel</title>
      <dc:creator>Nelson Romero</dc:creator>
      <pubDate>Thu, 08 Nov 2018 19:41:32 +0000</pubDate>
      <link>https://forem.com/nromero125/how-to-change-tables-structures-with-migration-without-losing-your-data-in-laravel-26fc</link>
      <guid>https://forem.com/nromero125/how-to-change-tables-structures-with-migration-without-losing-your-data-in-laravel-26fc</guid>
      <description>&lt;p&gt;This is something that I had always asked myself. "How to add a new column to a table without having to go directly to the database or rollback the migrations?"&lt;/p&gt;

&lt;p&gt;Laravel does not generate the migrations automatically through the models as Django or Entity Framework does, but we need to do it manually.&lt;/p&gt;

&lt;p&gt;Imagine that you have an application in production and you need to add a new column to the user table. Instead of rollback your migrations (which would cause the data to be lost) you can create a new migration to update the existing table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's go&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We have the initial migration of the user table and we want to add a column called picture:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsefiv7ui7dj59v1q3x3q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsefiv7ui7dj59v1q3x3q.png" alt="Initial Migration" width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We execute the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan make:migration add_picture_column_to_user_table --table=users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command would create a new migration file in which we would add our new column.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqho97zzletnmk734s51j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqho97zzletnmk734s51j.png" alt="New Migration" width="800" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then run the migrations using &lt;code&gt;php artisan migrate&lt;/code&gt; and that's all. You'll have this new column in your users table without losing previously stored data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzfyjsjdu2pc2xe31g0pt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzfyjsjdu2pc2xe31g0pt.png" alt="Database Screenshot" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
