Woodpecker CI的使用
# 什么是woodpecker-ci
官网: woodpecker-ci
啄木鸟ci是docker的ci工具与Jenkins、gitlab ci、github actions、drone等ci工具类似,轻量免费开源,是基于drone ci@0.8的分叉,相当于drone的开源版本,drone@0.8以后就闭源免费发布了
# 应用场景
个人ci或小团队的私有化部署ci工具
# 搭建
我使用的是gitea对接的woodpecker CI,如需其他对接方式查看官网文档
docker-compose.yml
services:
woodpecker-gitea:
restart: always
container_name: woodpecker-gitea
image: woodpeckerci/woodpecker-server:latest
# ports:
# - 8000:8000
volumes:
- ./woodpecker-server-data:/var/lib/woodpecker/
environment:
- WOODPECKER_OPEN=true
- WOODPECKER_ADMIN=awei
- WOODPECKER_HOST=https://woodpecker-gitea.xxx.cn
- WOODPECKER_GITEA=true
- WOODPECKER_GITEA_URL=https://gitea.xxx.cn
- WOODPECKER_GITEA_CLIENT=xxx
- WOODPECKER_GITEA_SECRET=xxx
- WOODPECKER_AGENT_SECRET=xxx
networks:
- proxy
woodpecker-gitea-agent:
restart: always
container_name: woodpecker-gitea-agent
image: woodpeckerci/woodpecker-agent:latest
command: agent
depends_on:
- woodpecker-gitea
volumes:
- ./woodpecker-agent-config:/etc/woodpecker
- /var/run/docker.sock:/var/run/docker.sock
environment:
- WOODPECKER_SERVER=woodpecker-gitea:9000
- WOODPECKER_AGENT_SECRET=xxx
networks:
- proxy
networks:
proxy:
external: true
# 使用
测试一个vue项目的打包、上传、部署的例子
push test分支自动部署测试环境
创建tag自动部署线上环境
# 创建test-ci项目
├── .deploy
│ ├── .woodpecker.yaml
│ ├── Dockerfile
│ ├── default.conf
│ └── docker-compose.yml
├── .gitignore
├── .vscode
│ └── extensions.json
├── README.md
├── index.html
├── package.json
├── public
│ └── vite.svg
├── src
│ ├── App.vue
│ ├── assets
│ ├── components
│ ├── main.ts
│ ├── style.css
│ └── vite-env.d.ts
├── tsconfig.json
├── tsconfig.node.json
├── vite.config.ts
└── yarn.lock
# .woodpecker.yaml
variables:
- &when_test
- event: push
branch: test
- &when_live
- event: tag
ref: refs/tags/v*
- &build
- yarn
- yarn build:$${BUILD_ENV}
- mkdir -p $${APP_PATH}/source
- rm -rf $${APP_PATH}/source/*
- mkdir -p $${APP_PATH}/source/conf.d
- rm -rf $${APP_PATH}/source/conf.d/*
- cp -rf dist $${APP_PATH}/source
- cp -rf .deploy/default.conf $${APP_PATH}/source/conf.d/default.conf
- cp -rf .deploy/docker-compose.yml $${APP_PATH}/docker-compose.yml
- &publish
platforms: linux/amd64
repo: registry.cn-shenzhen.aliyuncs.com/sliwei/ci-test
registry: registry.cn-shenzhen.aliyuncs.com
dockerfile: .deploy/Dockerfile
username: wangxiaoshou666
password:
from_secret: docker_password
- &upload
image: chuangbo/ossutil
secrets: [ oss_access_key_id, oss_access_key_secret, oss_endpoint ]
commands:
- ls -la
- ossutil config -e $${OSS_ENDPOINT}.aliyuncs.com -i $${OSS_ACCESS_KEY_ID} -k $${OSS_ACCESS_KEY_SECRET}
- ossutil cp -rf dist/ oss://bstu/$${CI_REPO_NAME}/$${BUILD_ENV}/ -u
- &deploy
command_timeout: 10m
host: bstu.cn
username: root
key:
from_secret: id_rsa
port: 22
script_stop: false
when:
- <<: *when_test
- <<: *when_live
steps:
restore-cache:
image: meltwater/drone-cache
volumes:
- ci-test-cache:/tmp/cache
settings:
backend: "filesystem"
restore: true
mount:
- "node_modules"
build-test:
image: docker.io/node:18-alpine
environment:
- APP_PATH=/data/docker/awei/ci-test/test
- BUILD_ENV=test
volumes:
- /data/docker:/data/docker
commands:
- <<: *build
when: *when_test
build-live:
image: docker.io/node:18-alpine
environment:
- APP_PATH=/data/docker/awei/ci-test/live
- BUILD_ENV=live
volumes:
- /data/docker:/data/docker
commands:
- <<: *build
when: *when_live
rebuild-cache:
image: meltwater/drone-cache
volumes:
- ci-test-cache:/tmp/cache
settings:
backend: "filesystem"
rebuild: true
mount:
- "node_modules"
upload-test:
environment:
- BUILD_ENV=test
<<: *upload
when: *when_test
upload-live:
environment:
- BUILD_ENV=live
<<: *upload
when: *when_live
publish-test:
image: woodpeckerci/plugin-docker-buildx
settings:
<<: *publish
tags: test-${CI_COMMIT_SHA:0:8}
when: *when_test
publish-live:
image: woodpeckerci/plugin-docker-buildx
settings:
<<: *publish
tags: ${CI_COMMIT_TAG}
when: *when_live
deploy-test:
image: appleboy/drone-ssh
environment:
- WORK_PATH=/data/docker/awei/ci-test/test
settings:
<<: *deploy
envs: [CI_COMMIT_SHA, WORK_PATH, CI_REPO_NAME]
script:
- cd $${WORK_PATH}
- export DOCKER_CONTAINER_NAME=$${CI_REPO_NAME}-test
- export DOCKER_IMAGE=test-$${CI_COMMIT_SHA:0:8}
- export ENV1=ENV1_test
- docker compose up -d
- docker compose restart
when: *when_test
deploy-live:
image: appleboy/drone-ssh
environment:
- WORK_PATH=/data/docker/awei/ci-test/live
settings:
<<: *deploy
envs: [CI_COMMIT_TAG, WORK_PATH, CI_REPO_NAME]
script:
- cd $${WORK_PATH}
- export DOCKER_CONTAINER_NAME=$${CI_REPO_NAME}
- export DOCKER_IMAGE=$${CI_COMMIT_TAG}
- export ENV1=ENV1_live
- docker compose up -d
- docker compose restart
when: *when_live
notify:
image: guoxudongdocker/drone-dingtalk:latest
settings:
message_color: true
message_pic: true
sha_link: true
token:
from_secret: dingtalk_token
type: markdown
when:
- status: [ success, failure ]
# 添加密钥
密钥分项目密钥、个人密钥、全局密钥
-
项目密钥只能在项目本身使用,其他项目无法使用
-
个人密钥可以在个人的所有项目下使用
-
全局密钥所有项目都可以使用
这里我们添加全局密钥
# 项目设置
# 测试
提交代码到test分支或新建tag,自动触发部署
^_^ Be the first to comment.
Comment
Nickname
Email (Reply notice)
Website
Content (can use Markdown syntax)