Created
May 8, 2020 07:07
-
-
Save lavish10/5f870660fb6b4c4ba01bac1ee4a48003 to your computer and use it in GitHub Desktop.
Github Actions workflow for Gradle build and deploy
This file contains hidden or 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
# This workflow will build a Java project with Gradle and deploy it on server | |
# deploy.sh will take the given parameters and perform the necessary operations, deploy.sh runs `docker-compose pull` and `docker-compose up -d` | |
name: Java CI with Gradle | |
on: | |
push: | |
branches: [ master ] | |
tags: [ deployment ] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Set up JDK 1.8 | |
uses: actions/setup-java@v1 | |
with: | |
java-version: 1.8 | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
- name: Build with Gradle | |
run: ./gradlew clean build | |
- name: Get the version | |
id: vars | |
run: echo ::set-output name=tag::$(echo ${GITHUB_SHA:10}) | |
- name: Docker login | |
env: | |
DOCKER_USERNAME: ${{ secrets.dockerhub_username }} | |
DOCKER_PASSWORD: ${{ secrets.dockerhub_password }} | |
run : docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" docker.io | |
- name: Build the Docker image | |
run: docker build . --file Dockerfile --tag ${{secrets.docker_username}}/${{secrets.repo_name}}:${{steps.vars.outputs.tag}} | |
- name: Publish dockerimage to docker hub | |
run: docker push ${{secrets.docker_username}}/${{secrets.repo_name}}:${{steps.vars.outputs.tag}} | |
- name: Build Docker image with latest tag | |
run: docker build . --file Dockerfile --tag ${{secrets.docker_username}}/${{secrets.repo_name}}:latest | |
- name: Publish dockerimage to docker hub | |
run: docker push ${{secrets.docker_username}}/${{secrets.repo_name}}:latest | |
deploy: | |
needs: build | |
runs-on: ubuntu-latest | |
if: contains(github.ref, 'deployment') | |
steps: | |
- uses: actions/checkout@master | |
- name: copy file via ssh password | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ secrets.server_hostname }} | |
username: ${{ secrets.server_user }} | |
key: ${{ secrets.openssh_private_key }} | |
source: "docker-compose.yml" | |
target: "/home/dockerfiles" | |
- name: Get the version | |
id: vars | |
run: echo ::set-output name=tag::$(echo ${GITHUB_SHA:10}) | |
- name: Execute SSH commmands on remote server | |
uses: JimCronqvist/action-ssh@master | |
env: | |
APPLICATION_IMAGE_TAG: ${{steps.vars.outputs.tag}} | |
MYSQL_VERSION: ${{ secrets.MYSQL_VERSION }} | |
MYSQL_ROOT_PASSWORD: ${{ secrets.MYSQL_ROOT_PASSWORD }} | |
MYSQL_USER: ${{ secrets.MYSQL_USERNAME }} | |
MYSQL_PASS: ${{ secrets.MYSQL_PASSWORD }} | |
with: | |
hosts: '${{ secrets.server_user }}@${{ secrets.server_hostname }}' | |
privateKey: ${{ secrets.openssh_private_key }} | |
debug: false | |
command: | | |
ls -lah | |
echo "ssh successful" | |
cd /home/dockerfiles/ && ./deploy.sh $APPLICATION_IMAGE_TAG $MYSQL_VERSION $MYSQL_ROOT_PASSWORD $MYSQL_USER $MYSQL_PASS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice actions yaml 👍