Skip to content

Instantly share code, notes, and snippets.

@romain-trotard
Created June 22, 2023 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romain-trotard/8de7a37ffe07e2cddcf9e6b7ebd70a04 to your computer and use it in GitHub Desktop.
Save romain-trotard/8de7a37ffe07e2cddcf9e6b7ebd70a04 to your computer and use it in GitHub Desktop.
Github action workflow to dockerize an application, publish it on registry and deploy it on a server
name: Deploy to Server
on:
push:
branches:
- master
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get new version
id: newVersion
run: |
PREVIOUS_VERSION=$(git describe --abbrev=0 --tags)
echo "Previous Version: $PREVIOUS_VERSION"
LATEST_VERSION=${LATEST_TAG#v}
NEW_VERSION=$(($LATEST_VERSION+1))
echo "New Version: $NEW_VERSION"
echo "NEW_VERSION=$NEW_VERSION" >> "$GITHUB_OUTPUT"
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker
uses: docker/setup-buildx-action@v2
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
build-args: |
"DB_URL=${{ secrets.DB_URL }}"
push: true
tags: astroApp:${{ steps.newVersion.outputs.NEW_VERSION }}
- name: Setup git config
run: |
git config --local user.name "GitHub Action"
git config --local user.email "githubAction@users.noreply.github.com"
- name: Create and push git tag
run: |
git tag v${{ steps.newVersion.outputs.NEW_VERSION }}
git push origin v${{ steps.newVersion.outputs.NEW_VERSION }}
- name: Deploy docker image to server
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
docker pull astroApp:${{ steps.newVersion.outputs.NEW_VERSION }}
docker stop web || true
docker rm web || true
docker run -d -p 8888:8888 --restart unless-stopped --name web astroApp:${{ steps.newVersion.outputs.NEW_VERSION }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment