[CI/CD] Git - Github - Actions

이정훈·2023년 4월 13일
0

IaC

목록 보기
7/8
post-thumbnail
  • git에 있는 내용을 github에 올려 actions를 이용해서 ec2한대와 연결 후 ansible과 terraform을 실행해 보고자 한다.
  • ansible과 terraform을 github actions에서 바로 실행할 수가 없어 sh 실행용 컴퓨터를 만들어 주어서 진행하 였다.
  • 실행할 수 는 있지만 깃 액션도 가상의 컴퓨터로 진행이 된다. 근데 버전이 달라 질수 있기데 하나의 컴터로 돌리고자 한다.

EC2 준비

sudo apt update
sudo apt install -y ansible
sudo apt-get install -y gnupg software-properties-common

wget -O- https://apt.releases.hashicorp.com/gpg | \ gpg --dearmor | \ sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \ https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \ sudo tee /etc/apt/sources.list.d/hashicorp.list

sudo apt update
sudo apt-get install -y terraform unzip
sudo apt-get install python3-pip

sudo pip install boto3

sudo apt-get install awscli

pip install --upgrade boto3
pip install --upgrade requests
pip install --upgrade --user awscli

sudo aws configure
  • EC2에서 terraform과 ansible이 실행될수 있도록 관련프로그램을 설치해 줬다.
  • ssh로 접근할 수 있도록 aws cli와 aws configure만 만들어 줬다.

Actions

name: 'Deploy'

on:
  push:
    branches: [ "master" ]
  pull_request:

permissions:
  contents: read

jobs:
  deploy:
    name: 'Deploy'
    runs-on: ubuntu-latest
    environment: production

    defaults:
      run:
        shell: bash

    steps:
    # Checkout the repository to the GitHub Actions runner
    - name: Checkout
      uses: actions/checkout@v3

    - name: SSH Commands
      uses: appleboy/ssh-action@v0.1.6
      with:
        host: ${{ secrets.AWS_SSH_HOST }}
        username: ${{ secrets.AWS_SSH_USERNAME }}
        key: ${{ secrets.AWS_SSH_KEY }}
        port: ${{ secrets.AWS_SSH_PORT }}
        # 각 필요한 secrets는 settings에서 만들어 주도록 하자
        # host = 실행한 ec2, username=ubuntu, 
        # 3key=terraform.pem의 내용들을 복사 붙여넣기, port=22(ssh접속은 여기서 하니깐!)
        script_stop: true
        script: |

            
          sudo rm -rf tmp

          sudo git clone https://github.com/gugucone999/cicd.git tmp

          sudo cp -r tmp/* 3tier/

          sudo ls -al

          sudo chmod 400 3tier/terraform.pem

          sudo sh 3tier/run.sh

          sudo rm -rf tmp
profile
싱숭생숭늉

0개의 댓글