Skip to content

Commit

Permalink
Merge pull request #492 from andrechristikan/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
andrechristikan authored Nov 12, 2023
2 parents 153ab39 + 64deed6 commit d9e22d1
Show file tree
Hide file tree
Showing 8 changed files with 526 additions and 434 deletions.
File renamed without changes.
150 changes: 120 additions & 30 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
name: CD

on:
# workflow_run:
# workflows: ['CI']
# types:
# - completed
# push:
# branches:
# - main
# - staging
workflow_dispatch:

jobs:
deploy:
deploy_production:
runs-on: ubuntu-latest
environment: baibay
if: ${{ github.ref_name == 'main' }}
environment: production

env:
APP_NAME: ${{ github.event.repository.name }}
DOCKER_HUB: ${{ secrets.DOCKER_HUB }}
APP_NAME: ${{ github.repository }}
APP_PORT: 3000
APP_PORT_EXPOSE: 3000
APP_PORT_EXPOSE: ${{ secrets.APP_PORT }}
APP_NETWORK: app-network

steps:
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_PORT: ${{ secrets.SSH_PORT }}
SSH_USER: ${{ secrets.SSH_USER }}
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY}}

steps:
- name: Git checkout
uses: actions/checkout@v3

- name: Get commit
- name: Get short sha commit
id: git
run: |
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
echo "short_sha=$(git rev-parse --short $GITHUB_SHA)" >> "$GITHUB_OUTPUT"
- name: Get latest version
id: version
Expand All @@ -35,14 +41,15 @@ jobs:
- name: Git
run: |
echo Branch name is: ${{ github.ref_name }}
echo Short sha: ${{ steps.git.outputs.short_sha}}
echo Version is: ${{ steps.version.outputs.current-version}}
echo Short sha: ${{ steps.git.outputs.short_sha }}
echo Version is: ${{ steps.version.outputs.current-version }}
- name: Environment
run: |
echo APP_NAME is: ${{ env.APP_NAME}}
echo APP_PORT is: ${{ env.APP_PORT}}
echo APP_NETWORK is: ${{ env.APP_NETWORK}}
echo DOCKER_HUB is: ${{ env.DOCKER_HUB }}
echo APP_NAME is: ${{ env.APP_NAME }}
echo APP_PORT is: ${{ env.APP_PORT }}
echo APP_NETWORK is: ${{ env.APP_NETWORK }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand All @@ -54,21 +61,21 @@ jobs:
uses: fifsky/ssh-action@master
with:
command: |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.APP_NAME}}:${{ steps.git.outputs.short_sha }}
docker stop ${{ env.APP_NAME}} && docker rm ${{ env.APP_NAME}}
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKER_HUB }}:main_v${{ steps.version.outputs.current-version }}_sha-${{ steps.git.outputs.short_sha }}
docker stop ${{ env.APP_NAME }} && docker rm ${{ env.APP_NAME }}
docker network create ${{ env.APP_NETWORK }} --driver=bridge
docker run -itd \
--hostname ${{ env.APP_NAME}} \
--publish ${{ env.APP_PORT_EXPOSE }}:${{ env.APP_PORT }} \
--hostname ${{ env.APP_NAME }} \
--publish ${{ secrets.APP_PORT }}:${{ env.APP_PORT }} \
--network ${{ env.APP_NETWORK }} \
--volume /app/${{ env.APP_NAME}}/logs/:/app/logs/ \
--volume /app/${{ env.APP_NAME}}/.env:/app/.env \
--volume /app/${{ env.APP_NAME }}/logs/:/app/logs/ \
--volume /app/${{ env.APP_NAME }}/.env:/app/.env \
--restart unless-stopped \
--name ${{ env.APP_NAME}} ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.APP_NAME}}:v${{ steps.version.outputs.current-version}}
--name ${{ env.APP_NAME }} ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKER_HUB }}:main_v${{ steps.version.outputs.current-version }}_sha-${{ steps.git.outputs.short_sha }}
host: ${{ secrets.SSH_HOST }}
port: ${{ secrets.SSH_PORT }}
user: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY}}
key: ${{ secrets.SSH_PRIVATE_KEY }}

- name: Clean
uses: fifsky/ssh-action@master
Expand All @@ -77,8 +84,91 @@ jobs:
command: |
docker container prune --force
docker image prune --force
docker rmi $(docker images **/${{ secrets.APP_NAME }} -q) --force
host: ${{ secrets.SSH_HOST }}
port: ${{ secrets.SSH_PORT }}
user: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY}}
docker rmi $(docker images **/${{ env.DOCKER_HUB }} -q) --force
host: ${{ env.SSH_HOST }}
port: ${{ env.SSH_PORT }}
user: ${{ env.SSH_USER }}
key: ${{ env.SSH_PRIVATE_KEY }}


deploy_staging:
runs-on: ubuntu-latest
if: ${{ github.ref_name == 'staging' }}
environment: staging

env:
DOCKER_HUB: ${{ secrets.DOCKER_HUB }}
APP_NAME: ${{ github.repository }}
APP_PORT: 3000
APP_PORT_EXPOSE: ${{ secrets.APP_PORT }}
APP_NETWORK: app-network

SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_PORT: ${{ secrets.SSH_PORT }}
SSH_USER: ${{ secrets.SSH_USER }}
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}

steps:
- name: Git checkout
uses: actions/checkout@v3

- name: Get short sha commit
id: git
run: |
echo "short_sha=$(git rev-parse --short $GITHUB_SHA)" >> "$GITHUB_OUTPUT"
- name: Get latest version
id: version
uses: martinbeentjes/npm-get-version-action@main

- name: Git
run: |
echo Branch name is: ${{ github.ref_name }}
echo Short sha: ${{ steps.git.outputs.short_sha }}
echo Version is: ${{ steps.version.outputs.current-version }}
- name: Environment
run: |
echo DOCKER_HUB is: ${{ env.DOCKER_HUB }}
echo APP_NAME is: ${{ env.APP_NAME }}
echo APP_PORT is: ${{ env.APP_PORT }}
echo APP_NETWORK is: ${{ env.APP_NETWORK }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Deploy
uses: fifsky/ssh-action@master
with:
command: |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKER_HUB }}:staging_v${{ steps.version.outputs.current-version }}_sha-${{ steps.git.outputs.short_sha }}
docker stop ${{ env.APP_NAME }} && docker rm ${{ env.APP_NAME }}
docker network create ${{ env.APP_NETWORK }} --driver=bridge
docker run -itd \
--hostname ${{ env.APP_NAME }} \
--publish ${{ env.APP_PORT_EXPOSE }}:${{ env.APP_PORT }} \
--network ${{ env.APP_NETWORK }} \
--volume /app/${{ env.APP_NAME }}/logs/:/app/logs/ \
--volume /app/${{ env.APP_NAME }}/.env:/app/.env \
--restart unless-stopped \
--name ${{ env.APP_NAME }} ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKER_HUB }}:staging_v${{ steps.version.outputs.current-version }}_sha-${{ steps.git.outputs.short_sha }}
host: ${{ env.SSH_HOST }}
port: ${{ env.SSH_PORT }}
user: ${{ env.SSH_USER }}
key: ${{ env.SSH_PRIVATE_KEY }}

- name: Clean
uses: fifsky/ssh-action@master
continue-on-error: true
with:
command: |
docker container prune --force
docker image prune --force
docker rmi $(docker images **/${{ env.DOCKER_HUB }} -q) --force
host: ${{ env.SSH_HOST }}
port: ${{ env.SSH_PORT }}
user: ${{ env.SSH_USER }}
key: ${{ env.SSH_PRIVATE_KEY }}
50 changes: 31 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
name: CI
on:
# push:
# pull_request:
# branches:
# - main
# - staging
workflow_dispatch:

jobs:
build:
build_image:
runs-on: ubuntu-latest
environment: baibay

env:
APP_NAME: ${{ github.event.repository.name }}
DOCKERFILE: dockerfile.prod
DOCKER_HUB: ${{ secrets.DOCKER_HUB }}
DOCKERFILE: .github/dockerfile

steps:
- name: Git checkout
uses: actions/checkout@v3

- name: Get commit
- name: Get git short sha
id: git
run: |
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
- name: Get latest version
echo "short_sha=$(git rev-parse --short $GITHUB_SHA)" >> "$GITHUB_OUTPUT"
- name: Get latest version of package json
id: version
uses: martinbeentjes/npm-get-version-action@main

- name: Git
run: |
echo Branch name is: ${{ github.ref_name }}
echo Short sha: ${{ steps.git.outputs.short_sha}}
echo Version is: ${{ steps.version.outputs.current-version}}
echo Short sha: ${{ steps.git.outputs.short_sha }}
echo Version is: ${{ steps.version.outputs.current-version }}
- name: Environment
run: |
echo APP_NAME is: ${{ env.APP_NAME}}
echo DOCKERFILE is: ${{ env.DOCKERFILE}}
echo DOCKER_HUB is: ${{ env.DOCKER_HUB }}
echo DOCKERFILE is: ${{ env.DOCKERFILE }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand Down Expand Up @@ -68,16 +67,29 @@ jobs:
file: ${{ env.DOCKERFILE }}
target: builder

- name: Build against main and push
- name: Build main and push
uses: docker/build-push-action@v4
if: ${{ github.ref_name == 'main' }}
with:
builder: ${{ steps.main.outputs.name }}
file: ${{ env.DOCKERFILE }}
target: main
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKER_HUB }}:latest
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKER_HUB }}:main_v${{ steps.version.outputs.current-version }}
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKER_HUB }}:main_v${{ steps.version.outputs.current-version }}_sha-${{ steps.git.outputs.short_sha }}
push: true

- name: Build main and push
uses: docker/build-push-action@v4
if: ${{ github.ref_name == 'staging' }}
with:
builder: ${{ steps.main.outputs.name }}
file: ${{ env.DOCKERFILE }}
target: main
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.APP_NAME }}:latest
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.APP_NAME }}:v${{ steps.version.outputs.current-version}}
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.APP_NAME }}:v${{ steps.version.outputs.current-version}}_sha-${{ steps.git.outputs.short_sha }}
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKER_HUB }}:staging_v${{ steps.version.outputs.current-version }}
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKER_HUB }}:staging_v${{ steps.version.outputs.current-version }}_sha-${{ steps.git.outputs.short_sha }}
push: true


Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
pull_request:
branches:
- main
# - staging

jobs:
linter:
Expand All @@ -18,9 +19,6 @@ jobs:
- name: Git checkout
uses: actions/checkout@v3

- name: Git sort sha
run: echo ${{ steps.vars.outputs.sha_short }}

- name: Setup node version ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
steps:
- name: Git checkout
uses: actions/checkout@v3

- name: Get commit
- name: Get short sha commit
id: git
run: |
echo "::set-output name=short_sha::$(git rev-parse --short HEAD)"
echo "short_sha=$(git rev-parse --short $GITHUB_SHA)" >> "$GITHUB_OUTPUT"
- name: Get latest version
id: version
Expand All @@ -30,16 +30,16 @@ jobs:
- name: Git
run: |
echo Branch name is: ${{ github.ref_name }}
echo Short sha: ${{ steps.git.outputs.short_sha}}
echo Version is: ${{ steps.version.outputs.current-version}}
echo Short sha: ${{ steps.git.outputs.short_sha }}
echo Version is: ${{ steps.version.outputs.current-version }}
- name: Release
uses: softprops/action-gh-release@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ steps.version.outputs.current-version}}
name: v${{ steps.version.outputs.current-version}}
tag_name: v${{ steps.version.outputs.current-version }}
name: v${{ steps.version.outputs.current-version }}
generate_release_notes: true
draft: false
prerelease: false
Loading

0 comments on commit d9e22d1

Please sign in to comment.