在 CI/CD 最後的 Deploy 中,最希望的是 Build 到 Deploy 的環境,盡量不需要手動設定就不要手動設定,比方說,在 Bitbucket Pipeline 執行的時候,自動建好 AWS Configure,然後上傳檔案到 S3 上面。
有部分功能可能只在 Bitbucket Pipeline 有用:
- git lfs 功能 (見程式碼)
Pipelines.yaml:
# This is a sample build configuration for JavaScript.
# Check our guides at https://confluence.atlassian.com/x/14UWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
clone:
lfs: true # 支援大檔案模式
pipelines:
tags: # add the 'tags' section
release-*: # specify the tag
- step: # define the build pipeline for the tag
name: Build and release
script:
- mkdir -p ~/.ssh
- (umask 077 ; echo $MY_SSH_KEY | base64 --decode > ~/.ssh/id_rsa) #事先在 Variables 新增 MY_SSH_KEY 變數,並把 privatekey.pem 用 base64 encode 放到環境變數中
- ssh -o "StrictHostKeyChecking no" [email protected] "touch /tmp/testfile" #用 id_rsa key 登入 ssh,並且直接執行指令
deploy-*: # 自動佈署到 s3
- step:
name: upload to s3
script:
- apt-get update && apt-get install -y awscli
- aws --version
- mkdir ~/.aws
- echo -e $AWS_ACCESS_KEY"\n"$AWS_SECRET_ACCESS_KEY"\nap-northeast-1\n\n" | aws configure 2>/dev/null
- aws configure list
- aws s3 cp ./XXX/views/ s3://BUCKETNAME/ --recursive --region ap-northeast-1
- aws s3 cp ./XXX/public/ s3://BUCKETNAME/ --recursive --region ap-northeast-1
- echo "GOOD!"
在上面的 config 檔案中,共使用了三個變數,在 Pipelines 旁邊的 Variables 可以設定:
- MY_SSH_KEY: (把 -----BEGIN PRIVATE KEY-----..... 轉成成 base64 字串,貼上去)
- AWS_ACCESS_KEY: (access_key)
- AWS_SECRET_ACCESS_KEY: (secret_key)
沒有留言:
張貼留言