Fix: develop.yml docker install #12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build And Deploy for develop branch to test server | |
on: | |
push: | |
branches: [ "develop" ] | |
permissions: | |
contents: read | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: "adopt" | |
- name: Make folder resources | |
run: | | |
cd ./server/src/main | |
mkdir resources | |
cd resources | |
- name: Make application.properties | |
run: | | |
cd ./server/src/main/resources | |
touch ./application.properties | |
echo "${{ secrets.PROPERTIES_DEVELOP }}" > ./application.properties | |
- name: Build with Gradle | |
run: | | |
cd server | |
chmod +x ./gradlew | |
./gradlew clean build -x test | |
- name: Docker build & push to docker repo | |
run: | | |
cd server | |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
docker build -f Dockerfile -t ${{ secrets.DOCKER_REPO }}:latest . | |
docker push ${{ secrets.DOCKER_REPO }}:latest | |
pull-and-deploy: | |
needs: build-and-push | |
runs-on: ubuntu-latest | |
steps: | |
- name: Install Docker | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.TEST_EC2_PUBLIC_IP }} | |
username: ubuntu | |
key: ${{ secrets.TEST_SSH_PRIVATE_KEY }} | |
envs: GITHUB_SHA | |
script: | | |
if ! command -v docker &> /dev/null; then | |
if [[ "$OS_CHECK" =~ "ubuntu" ]] || [[ "$OS_CHECK" =~ "Ubuntu" ]]; then | |
sudo apt-get update | |
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - | |
sudo apt-key fingerprint 0EBFCD88 # 안해도 됨 | |
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | |
sudo apt-get update | |
sudo apt-get install docker-ce docker-ce-cli containerd.io | |
sudo usermod -aG docker $USER | |
sudo service docker restart | |
echo "Docker for Ubuntu download complete!" | |
elif [[ "$OS_CHECK" =~ "centos" ]]; then | |
sudo yum install -y yum-utils | |
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo | |
sudo yum install docker-ce docker-ce-cli containerd.io | |
sudo systemctl start docker | |
sudo usermod -aG docker $USER | |
sudo service docker restart | |
echo "Docker for CentOS download complete!" | |
else | |
echo "check OS version is ubuntu or centos" | |
fi | |
fi | |
- name: Install Docker-compose | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.TEST_EC2_PUBLIC_IP }} | |
username: ubuntu | |
key: ${{ secrets.TEST_SSH_PRIVATE_KEY }} | |
envs: GITHUB_SHA | |
script: | | |
if ! command -v docker-compose &> /dev/null; then | |
sudo curl -L "https://github.com/docker/compose/releases/download/v2.15.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose | |
sudo chmod +x /usr/local/bin/docker-compose | |
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose | |
else | |
echo "Docker Compose already installed via curl." | |
fi | |
- name: Remove & Change docker-compose file | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.TEST_EC2_PUBLIC_IP }} | |
username: ubuntu | |
key: ${{ secrets.TEST_SSH_PRIVATE_KEY }} | |
envs: GITHUB_SHA | |
script: | | |
if [ -f docker-compose.yml ]; then | |
sudo rm docker-compose.yml | |
fi | |
curl -o docker-compose.yml -L https://raw.githubusercontent.com/kookmin-sw/capstone-2024-14/develop/server/docker-compose.yml | |
cat docker-compose.yml | |
- name: Remove & Change Server Docker image | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.TEST_EC2_PUBLIC_IP }} | |
username: ubuntu | |
key: ${{ secrets.TEST_SSH_PRIVATE_KEY }} | |
envs: GITHUB_SHA | |
script: | | |
if [ -n "$(docker ps -qa)" ]; then | |
sudo docker rm -f $(docker ps -qa) | |
fi | |
sudo docker pull ${{ secrets.DOCKER_REPO }}:latest | |
sudo docker image prune -f | |
- name: Docker-compose Execute | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.TEST_EC2_PUBLIC_IP }} | |
username: ubuntu | |
key: ${{ secrets.TEST_SSH_PRIVATE_KEY }} | |
envs: GITHUB_SHA | |
script: | | |
sudo docker-compose up | |