Genellikle npm paketi yazarken atladığımız bizi gereksiz yere uğraştıran can sıkıcı bir işlemi otomatik olarak nasıl çözebiliriz bunun üzerine kafa yoralım istiyorum.
Küçük bir örnek proje üzerinden bunu ele alabiliriz.
Projelerimizde ortak olarak kullandığımız bazı fonksiyonlar var diyelim.
Örneğin timeparser, bussiness logicten birazcık karışım mesela proje içinde sıkça kullandığımız bir kategori parsing işlemi olsun.
Bunların hepsi ortak ama bir projede yeniledik diğerlerinde de tek tek gidip ilgili satırı bul değiştir uğraşmaktan bıktık.
Bu fonksiyonların hepsini utils diye bir npm paketinde tutalım diye karar verdik diyelim.
Npm paketini oluşturduk, github' a yükledik. Diğer projeler ile bağlantılarını yaptık. Projelerde her zaman son versiyon üzerinden işlem yapması için package.json klasörünü ayarladık.
Yeni bir talep üzerine ilerde diğer projelere de eklenebilecek bir utils fonksiyonu geldi diyelim.
Yeni bir branch açtık. Geliştirmeyi tamamladık, unit testlerini yazdık. package.json klasörünü güncelledik. Review'dan geçti. Deploy ettik.
Sonrasında bump diye bir yeni commit girdik. package.json klasöründe npm versiyonunu güncelledik. Gönderdik. Sonra bilgisayarımıza döndük ve npm publish diyerek ilgili utils' i npm e gönderdik.
Ben yazarken yoruldum..
You will preffer to use taging deployment for your npm package. And you will need to change for npm package.json automatically. You can handle it full-auto and safely with this workflow.
- You need to add publish.yml your workflow directory .github/workflows/
- You must have a npm token that have publish permission from npm panel.
- You must add token as project or organization secrets. Name -> NPM_PUBLISH_TOKEN
- You can change bumper user, email and commit name, between 79-82 lines.
Get github tag from last tagging. format number.number.number exp: 4.2.1
Get last npm version from npm and check with your github tag. Your tagging must be grather than last npm version.
Change local package.json, push main/master branch and push to npm.
name: Version Increment | |
on: | |
push: | |
tags: | |
- "*" | |
jobs: | |
job_tag_control: | |
runs-on: ubuntu-latest | |
outputs: | |
CHECK_TAG: ${{ steps.check_tag.outputs.CHECK_TAG }} | |
VERSION: ${{ steps.get_version.outputs.VERSION }} | |
steps: | |
- name: Get the version | |
id: get_version | |
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} | |
- name: Check Tag | |
id: check_tag | |
run: | | |
if [[ ${{ steps.get_version.outputs.VERSION }} =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
echo ::set-output name=CHECK_TAG::true | |
fi | |
job_npm_tag_control: | |
runs-on: ubuntu-latest | |
needs: | |
- job_tag_control | |
if: needs.job_tag_control.outputs.CHECK_TAG == 'true' | |
outputs: | |
NPM_VERSION: ${{ steps.get_external_npm_latest_version.outputs.NPM_VERSION }} | |
CHECK_TAG_WITH_EXTARNAL: ${{ steps.check_version_greater_than_npm_version.outputs.CHECK_TAG_WITH_EXTARNAL }} | |
steps: | |
- name: Checkout Source Code | |
uses: actions/checkout@v2 | |
- name: Get package.json name | |
id: get_package_json_name | |
run: echo ::set-output name=PACKAGE_JSON_NAME::$(cat package.json | jq -r .name) | |
- name: Setup Node | |
uses: actions/setup-node@v2 | |
with: | |
node-version: 14 | |
registry-url: //registry.npmjs.org/:_authToken=${{ secrets.NPM_PUBLISH_TOKEN }} | |
- run: npm set //registry.npmjs.org/:_authToken=${{ secrets.NPM_PUBLISH_TOKEN }} | |
- name: External npm latest version set echo | |
id: get_external_npm_latest_version | |
run: echo ::set-output name=NPM_VERSION::$(npm view ${{ steps.get_package_json_name.outputs.PACKAGE_JSON_NAME }} version) | |
- name: VERSION must be greater than NPM_VERSION | |
id: check_version_greater_than_npm_version | |
run: | | |
if [[ ${{ steps.get_external_npm_latest_version.outputs.NPM_VERSION }} < ${{ needs.job_tag_control.outputs.VERSION }} ]]; then | |
echo ::set-output name=CHECK_TAG_WITH_EXTARNAL::true | |
fi | |
job_publish: | |
runs-on: ubuntu-latest | |
needs: | |
- job_tag_control | |
- job_npm_tag_control | |
if: needs.job_npm_tag_control.outputs.CHECK_TAG_WITH_EXTARNAL == 'true' | |
steps: | |
- name: Checkout Source Code | |
uses: actions/checkout@v3 | |
with: | |
ref: master | |
- name: Setup Node | |
uses: actions/setup-node@v2 | |
with: | |
node-version: 14 | |
registry-url: //registry.npmjs.org/:_authToken=${{ secrets.NPM_PUBLISH_TOKEN }} | |
- run: npm set //registry.npmjs.org/:_authToken=${{ secrets.NPM_PUBLISH_TOKEN }} | |
- run: git config --global user.name 'testuser-bumper' | |
- run: git config --global user.email 'test@test.com' | |
- run: npm version ${{ needs.job_tag_control.outputs.VERSION }} -m "[BUMPER] %s" | |
- run: git push | |
- run: npm install --only=prod | |
- run: npm publish | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} | |
Yukarıda README.md klasöründe biraz yazdım ama aşama aşama burada ne yaptığımızı inceleyelim isterim.
Öncelikle daha önce workflow ile uğraşanlar için en üstteki event listen dikkatinizi çekmiştir. genellikle orada branch naming üzerinden işlem yapılır. Bizim ihtiyacımız tagging üzerinden olacağından bu şekilde ilerliyoruz.
Üç adet jobtan oluşan bir workflow umuz var ve aşama aşama;
- job_tag_control
- job_npm_tag_control
- job_publish
Her şeyden önce sağlıklı bir yaşam dileğiyle..
Yorumlar
Yorum Gönder