Home > Software engineering >  How to copy a file to ec2 via ssh? (CI/CD using gitlab CI)
How to copy a file to ec2 via ssh? (CI/CD using gitlab CI)

Time:01-03

So I have set up ci/cd using gitlab and is now able to do

  1. Build the Docker image
  2. Tag it properly
  3. Push it to ECR
  4. SSH to EC2 instance
  5. Pull image to the EC2 instance

However, I still need to run the docker image for it to be complete.

Right now, I am using the --env_file to specify the env_file for that container, but I still have to create the env file manually on the ec2 instance first.

Is there a way for me to just copy and replace the .env file I have in my repository to the ec2 instance, so it can be updated from that file instead of having to redo it every time there's a change?

CodePudding user response:

You can use scp <path-to-env-file> <ec2-user@><ec2-address>:<path-to-docker-directory>.

Another solution: You could also build Ansible Playbook and execute all of your steps. You would need to write your steps in form of ansible tasks or roles, then target the correct host. For example steps from 1->3 are excuted locally, and 5->7 (where 6 is copying .env file and 7 is starting the docker container) to be remotely (on EC2 instance) executed. More on this: https://www.redhat.com/en/topics/automation/what-is-an-ansible-playbook#:~:text=Ansible Playbooks are lists of,as which user executes it.

  • Related