AWS Code Commit

謙謙陌上桑發表於2020-10-13

AWS CodeCommit 是完全託管的原始碼控制服務,可託管安全的 Git 儲存庫,相當於一個Private GitHub。它可讓團隊在安全且高度可擴充套件的生態系統中輕鬆協作處理程式碼。使用 CodeCommit,無需執行自己的原始碼控制系統,也無需擔心基礎設施的擴充套件能力。可以使用 CodeCommit 將來自原始碼的任何資料安全儲存為二進位制檔案,而且它可以無縫相容您現有的 Git 工具。

下面,我們一起探索一下CodeCommit的基本用法,來實現你的第一個Commit。

1. Create an AWS CodeCommit Repository

Create in the AWS Console:
Service Menu -> CodeCommit -> Create repository
On the Create repository page, configure:

  • Name: My-Repo
  • Description: My first repository
  • Click Create
    An empty repo called My-Repo will be created in CodeCommit. Connection details will be shown.

2. Create an EC2 Instance

Create an EC2 instance in the AWS Console, and record down the public IP address.

3. Connect the CodeCommit to the EC2 instance

通常有兩種方式:

  • SSH keys for AWS CodeCommit
  • HTTPS Git credentials for AWS CodeCommit
    這裡,我們使用 SSH keys for AWS CodeCommit

Before this step, you need to create a Key pair for authentication. Say the name is “KEYPAIR.pem”.
Save the .pem file to your local computer.

  • chmod command: change the access permissions of file system objects
  • ssh command: can ssh to the EC2 public IP
chmod 400 KEYPAIR.pem
ssh -i KEYPAIR.pem ec2-user@EC2PublicIP
chmod 400 qwikLABS-L2990-19969242.pem
ssh -i qwikLABS-L2990-19969242.pem ec2-user@3.230.119.106
  • Type yes when prompted to allow a first connection to this remote SSH server.
  • Because you are using a key pair for authentication, you will not be prompted for a password.

4. In the EC2, create a Local Repo using Git

  • Install the git client:
sudo yum install -y git
  • Use the Git credentials helper with the AWS credentials profile, and enables the Git credentials helper to send the path to repositories.
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
  • You will now need to obtain the HTTPS URL of your AWS CodeCommit repository.
  • In the AWS CodeCommit Management Console, click the Clone URL then click Clone HTTPS
  • The URL look similar to below:
    https://git-codecommit.us-east-1.amazonaws.com/v1/repos/My-Repo
  • In the remote SSH session, copy and paste: git clone URL(Replace the URL with the CodeCommit clone URL), and run the command
  • You should see the following message:
    Cloning in to ‘my-demo-repo’…
    Warning: You appear to have cloned an empty repository.

5. Make a Code Change in the EC2, and Commit to the Repo

To create your first commit in your local repo.

  • Use the following command to create two files with content in your local repo:
cd ~/My-Repo
echo "The domestic cat (Felis catus or Felis silvestris catus) is a small, usually furry, domesticated, and carnivorous mammal." >cat.txt
echo "The domestic dog (Canis lupus familiaris) is a canid that is known as man's best friend." >dog.txt
  • List the directory to see the files were created
ls
  • Run this command to stage the change in your local repo:
git add cat.txt dog.txt
  • View the status of your repo:
git status

You will see the two files are ready to be committed to the repo.

  • Run the command to commit the change in your local repo:
git commit -m "Added cat.txt and dog.txt"
  • View the details about the commit you just made:
git log

6. Push your first Commit

  • Push the commit from your local repo to your AWS CodeCommit Repository.
git push -u origin master

Once you pushed code to the AWS CodeComit repo, you can view the contents using the AWS CodeCommit console.

  • In the CodeCommit Management Console, ensure you are in the My-Repo repo.
  • Refresh your screen. The two files that you added to your repo should be displayed.
  • Click each file to view the contents.

Congratulations! You have created an AWS CodeCommit repo and can now consider how to bring its features and capabilities to your development workflow. These include:

  • Collaboration
  • Encryption
  • Access Control
  • High Availability and Durability
  • Unlimited Repositories
  • Easy Access and Integration

相關文章