diff --git a/.github/workflows/hello.yml b/.github/workflows/hello.yml index 34ed691..71eda3a 100644 --- a/.github/workflows/hello.yml +++ b/.github/workflows/hello.yml @@ -11,6 +11,9 @@ on: jobs: build: runs-on: ubuntu-latest + env: + IMAGE_NAME: hello + permissions: contents: read packages: write @@ -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 }} \ No newline at end of file diff --git a/Makefile b/Makefile index 48d7e3d..eb8a336 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # 定义变量 -IMAGE_NAME := hello -IMAGE_TAG := latest +IMAGE_NAME ?= hello +IMAGE_TAG ?= latest # 构建 Docker 镜像 build: