Skip to content

Chore: add sanitize env variables block #2

Chore: add sanitize env variables block

Chore: add sanitize env variables block #2

Workflow file for this run

name: cicd for discovery-service
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
env:
AWS_REGION: ap-northeast-2
IMAGE_NAME: esthete-discovery-service
jobs:
build:
runs-on: ubuntu-22.04
steps:
- name: Checkout the repository
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
- name: Setup JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Build with Gradle
run: ./gradlew build -x test
- name: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
- name: Determine and Push Next Version Tag
run: |
if ! git tag --sort version:refname | grep -q .; then
git tag 1.0.0
else
NEXT_DISCOVERY_VER=$(git tag --sort version:refname | tail -n 1 | awk -F. -v OFS=. '{$NF++; print}')
git tag $NEXT_DISCOVERY_VER
fi
git push --tags
echo "NEXT_DISCOVERY_VER=$NEXT_DISCOVERY_VER" >> $GITHUB_ENV
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Sanitize environment variables
run: |
export IMAGE_NAME=$(echo ${{ env.IMAGE_NAME }} | tr -cd '[:alnum:]_.-')
export NEXT_DISCOVERY_VER=$(echo ${{ env.NEXT_DISCOVERY_VER }} | tr -cd '[:alnum:]_.-')
echo "IMAGE_NAME=${IMAGE_NAME}"
echo "NEXT_DISCOVERY_VER=${NEXT_DISCOVERY_VER}"
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ env.NEXT_DISCOVERY_VER }}
platforms: linux/arm64
- name: Deploy to test server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST_TEST_DNS }}
username: ubuntu
key: ${{ secrets.PRIVATE_KEY }}
envs: GITHUB_SHA,NEXT_DISCOVERY_VER
script: |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ env.NEXT_DISCOVERY_VER }}
sudo docker stop ${{ env.IMAGE_NAME }} || true
sudo docker rm ${{ env.IMAGE_NAME }} || true
sudo docker run -d --name ${{ env.IMAGE_NAME }} --network esthete-network -p 8761:8761 \
${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:${{ env.NEXT_DISCOVERY_VER }}
sleep 15
sudo docker image prune -af || true
sudo docker images ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }} | grep -v ${{ env.NEXT_DISCOVERY_VER }} | awk '{print $3}' | xargs -r sudo docker rmi || true