Skip to content

Commit

Permalink
Add deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
davema-clan committed Apr 24, 2024
1 parent 97191d2 commit f1e414e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
34 changes: 20 additions & 14 deletions .github/workflows/hello.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ on:
jobs:
build:
runs-on: ubuntu-latest
env:
IMAGE_NAME: hello

permissions:
contents: read
packages: write
Expand All @@ -20,34 +23,37 @@ jobs:
uses: actions/checkout@v4

- name: Login to GitHub Packages
if: ${{ github.ref_type == 'branch' }}
run: echo "${{ secrets.SECRETS_GITHUB_TOKEN }}" | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin

- name: Get Commit Ref Slug
- name: Get Short SHA
if: ${{ github.ref_type == 'branch' }}
run: |
echo ${GITHUB_REF}
COMMIT_REF_SLUG=$(echo "${GITHUB_REF}" | cut -d'/' -f 3 | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9]/-/g')
echo "Commit Ref Slug: $COMMIT_REF_SLUG"
SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-7)
echo "Short SHA: $SHORT_SHA"
- name: Build images
run: make build ARGS="ghcr.io/${{ github.repository }}/"
if: ${{ github.ref_type == 'branch' }}
run: make build ARGS="ghcr.io/${{ github.repository }}/" IMAGE_TAG="${{ SHORT_SHA }}" IMAGE_NAME=${{ IMAGE_NAME }}

- name: Push images to repository
if: ${{ github.ref_type == 'branch' }}
run: make push ARGS="ghcr.io/${{ github.repository }}/"

- name: Connect twingate
uses: twingate/github-action@v1
with:
service-key: ${{ secrets.TWG_SERVICE_KEY }}
deploy_test:
runs-on: ubuntu-latest
needs: build

steps:

- name: Deploy Test
uses: easingthemes/ssh-deploy@main
if: ${{ github.ref_type == 'branch' }}
env:
REMOTE_HOST: ${{ vars.REMOTE_HOST_TEST }}
REMOTE_HOST: ${{ secrets.REMOTE_HOST_TEST }}
REMOTE_USER: ubuntu
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY_TEST }}
SCRIPT_BEFORE: |
docker stop hello
docker rm hello
SCRIPT_RUN: |
docker run -d --name hello ghcr.io/${{ github.repository }}/hello:latest
docker ps |grep ${{ IMAGE_NAME }} && docker stop ${{ IMAGE_NAME }} && docker rm ${{ IMAGE_NAME }}
docker login ghcr.io -u $GITHUB_ACTOR -p "${{ secrets.SECRETS_GITHUB_TOKEN }}"
docker run -d --name ${{ env.IMAGE_NAME }} ghcr.io/${{ github.repository }}/${{ env.IMAGE_NAME }}:${{ SHORT_SHA }}
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 定义变量
IMAGE_NAME := hello
IMAGE_TAG := latest
IMAGE_NAME ?= hello
IMAGE_TAG ?= latest

# 构建 Docker 镜像
build:
Expand Down

0 comments on commit f1e414e

Please sign in to comment.