-
Notifications
You must be signed in to change notification settings - Fork 0
76 lines (63 loc) · 2.57 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
name: Build And Deploy for Api Server
on:
push:
branches: ['dev']
env:
ACTIVE_PROFILE: "dev"
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
DOCKERHUB_API_IMAGE_NAME: ${{ secrets.DOCKERHUB_API_IMAGE_NAME }}
jobs:
build-and-push:
runs-on: ubuntu-latest
strategy:
matrix:
java-version: [ 17 ]
outputs:
image_tag: ${{ steps.image_tag.outputs.image_tag }}
steps:
- name: Check Out The Repository
uses: actions/checkout@v3
- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.java-version }}
distribution: 'corretto'
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Execute Gradle build
run: |
./gradlew build --no-daemon
- name: Make image tag
run: echo "IMAGE_TAG=$ACTIVE_PROFILE-${GITHUB_SHA::7}" >> $GITHUB_ENV
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build docker image and push image
run: |
docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_TOKEN
docker build --build-arg PROFILE=$ACTIVE_PROFILE -t $DOCKERHUB_USERNAME/$DOCKERHUB_API_IMAGE_NAME:${{env.IMAGE_TAG}} .
docker push $DOCKERHUB_USERNAME/$DOCKERHUB_API_IMAGE_NAME:${{env.IMAGE_TAG}}
- name: Pass Image tag value
id: image_tag
run: echo "image_tag=${{env.IMAGE_TAG}}" >> $GITHUB_OUTPUT
deploy:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Set image tag from Output
run: echo "IMAGE_TAG=${{ needs.build-and-push.outputs.image_tag }}" >> $GITHUB_ENV
- name: SSH remote and Run Docker container
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_IP }}
username: ${{ secrets.REMOTE_USERNAME }}
key: ${{ secrets.REMOTE_SSH_KEY }}
port: ${{ secrets.REMOTE_SSH_PORT }}
script: |
sudo touch .env
echo "${{ secrets.ENV_VARS }}" | sudo tee .env > /dev/null
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_API_IMAGE_NAME }}:${{env.IMAGE_TAG}}
docker stop $(docker ps -a -q)
docker run --restart=unless-stopped -d -p 8080:8080 --env-file .env --add-host=host.docker.internal:host-gateway ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_API_IMAGE_NAME }}:${{env.IMAGE_TAG}}
docker rm $(docker ps --filter 'status=exited' -a -q)
docker image prune -a -f