Skip to content

update

update #63

Workflow file for this run

name: Build Image for test
env:
# IMAGE_NAME 用于 程序名
REGISTRY: ghcr.io/${{ github.repository }}
IMAGE_NAME: hello
SECRETS_GITHUB_TOKEN: ${{ secrets.SECRETS_GITHUB_TOKEN }}
on:
push:
branches: [ master ]
release:
types: [ published ]
jobs:
GetSHA:
runs-on: ubuntu-latest
outputs:
IMAGE_SHA: ${{ steps.get_short_sha.outputs.SHORT_SHA}}
steps:
- name: Get Short SHA
id: get_short_sha
run: |
SHORT_SHA=$(echo ${GITHUB_SHA} | cut -c1-7)
echo "SHORT_SHA=$SHORT_SHA" >> $GITHUB_OUTPUT
echo "Short SHA: $SHORT_SHA"
Build:
runs-on: ubuntu-latest
needs: GetSHA
if: ${{ github.ref_type == 'branch' }}
env:
IMAGE_SHA: ${{ needs.GetSHA.outputs.IMAGE_SHA}}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Login to GitHub Packages
run: echo "${SECRETS_GITHUB_TOKEN}" | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin
- name: Build images
run: make build ARGS="${REGISTRY}/" IMAGE_TAG="${IMAGE_SHA}" IMAGE_NAME=${IMAGE_NAME}
- name: Push images to repository
run: make push ARGS="${REGISTRY}/" IMAGE_TAG="${IMAGE_SHA}" IMAGE_NAME=${IMAGE_NAME}
Deploy_test:
runs-on: ubuntu-latest
needs: [ GetSHA, Build ]
if: ${{ github.ref_type == 'branch' }}
env:
IMAGE_SHA: ${{ needs.GetSHA.outputs.IMAGE_SHA}}
steps:
- name: Deploy Test
uses: appleboy/[email protected]
with:
host: ${{ secrets.REMOTE_HOST_TEST }}
username: ubuntu
key: ${{ secrets.SSH_PRIVATE_KEY_TEST }}
port: 22
envs: GITHUB_ACTOR,SECRETS_GITHUB_TOKEN,IMAGE_NAME,IMAGE_SHA,REGISTRY
script: |
echo "$GITHUB_ACTOR $REGISTRY $IMAGE_SHA $IMAGE_NAME $SECRETS_GITHUB_TOKEN" > test.txt
echo "$SECRETS_GITHUB_TOKEN" | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin
docker stop $IMAGE_NAME
docker rm $IMAGE_NAME
docker run -d --name $IMAGE_NAME ${REGISTRY}/${IMAGE_NAME}:${IMAGE_SHA}
Deploy_Prod:
runs-on: ubuntu-latest
if: ${{ github.ref_type == 'tag' }}
steps:
- name: Get Tag Name
id: get_tag_name
run: |
TAG_NAME=$(echo $GITHUB_REF | awk -F '/' '{print $NF}')
echo $TAG_NAME
echo "TAG_NAME=$TAG_NAME" >> $GITHUB_ENV
- name: get short sha
run: |
full_sha=${{ github.event.before }}
IMAGE_SHA=$(echo $full_sha | cut -c1-7)
echo "IMAGE_SHA=$IMAGE_SHA" >> $GITHUB_ENV
- name: docker login github repo
run: echo "${SECRETS_GITHUB_TOKEN}" | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin
- name: build image to prod
env:
IMAGE_TAG: ${{ env.IMAGE_TAG }}
IMAGE_SHA: ${{ env.IMAGE_SHA }}
run: |
docker pull ${REGISTRY}/${IMAGE_NAME}:${IMAGE_SHA}
docker tag ${REGISTRY}/${IMAGE_NAME}:${IMAGE_SHA} ${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}
docker push ${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}
- name: Deploy prod
uses: appleboy/[email protected]
env:
IMAGE_TAG: ${{ env.IMAGE_TAG }}
with:
host: ${{ secrets.REMOTE_HOST_TEST }}
username: ubuntu
key: ${{ secrets.SSH_PRIVATE_KEY_TEST }}
port: 22
envs: GITHUB_ACTOR,SECRETS_GITHUB_TOKEN,IMAGE_NAME,IMAGE_TAG,REGISTRY
script: |
echo "$GITHUB_ACTOR $REGISTRY $IMAGE_SHA $IMAGE_NAME $SECRETS_GITHUB_TOKEN" > test.txt
echo "$SECRETS_GITHUB_TOKEN" | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin
docker stop $IMAGE_NAME
docker rm $IMAGE_NAME
docker run -d --name $IMAGE_NAME ghcr.io/${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}