feat: cd #9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Rust | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
env: | |
CARGO_TERM_COLOR: always | |
jobs: | |
build: | |
runs-on: ubuntu-20.04 # 勿使用ubuntu-latest, 打包后找不到lib依赖 | |
steps: | |
- uses: actions/checkout@v4 # pull代码 | |
- name: Cache | |
# 缓存大小10GB, 7天不用清空 | |
uses: actions/cache@v3 | |
with: | |
# 缓存或还原的路径 | |
path: | | |
./target | |
~/.cargo | |
# 唯一key | |
key: prd-${{ runner.os }}-${{ hashFiles('rust-toolchain.toml') }}-${{ hashFiles('Cargo.lock') }} | |
# key没有命中则按照前缀顺序依次来还原缓存 | |
restore-keys: | | |
prd-${{ runner.os }}-${{ hashFiles('rust-toolchain.toml') }}- | |
prd-${{ runner.os }}- | |
- name: Build src | |
run: | | |
cargo build --release --verbose | |
- name: Run tests | |
run: | | |
cargo test --verbose | |
- name: Docker login | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ secrets.REGISTRY_DOMAIN }} | |
username: ${{ secrets.REGISTRY_USERNAME }} | |
password: ${{ secrets.REGISTRY_PASSWORD }} | |
- name: Build and push image | |
env: | |
REGISTRY: ${{ secrets.REGISTRY_DOMAIN }} | |
NAMESPACE: a2cd | |
IMAGE_NAME: rs-hello-world | |
IMAGE_TAG: latest | |
run: | | |
docker build -t $REGISTRY/$NAMESPACE/$IMAGE_NAME:$IMAGE_TAG . | |
docker push $REGISTRY/$NAMESPACE/$IMAGE_NAME:$IMAGE_TAG | |
- name: Setup SSH | |
env: | |
SSH_HOST: ${{ secrets.SSH_HOST }} | |
SSH_USER: ${{ secrets.SSH_USER }} | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
KEY_FILE: a2cd.key # 不能用~/.ssh/a2cd.key作为value | |
run: | | |
mkdir -p ~/.ssh/ && echo "$SSH_PRIVATE_KEY" > ~/.ssh/$KEY_FILE && chmod 600 ~/.ssh/$KEY_FILE | |
cat >> ~/.ssh/config <<END | |
Host a2cd | |
HostName $SSH_HOST | |
User $SSH_USER | |
IdentityFile ~/.ssh/$KEY_FILE | |
StrictHostKeyChecking no | |
END | |
- name: Deploy | |
run: ssh a2cd 'cd /usr/local/repo/rs-hello-world/ && docker-compose pull && docker-compose up -d' |