Switch deploy preview
to Gitea Actions
#119
90
.drone.yml
90
.drone.yml
@ -1,90 +0,0 @@
|
||||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: Build and deploy website PR preview
|
||||
|
||||
platform:
|
||||
os: linux
|
||||
arch: arm64
|
||||
|
||||
trigger:
|
||||
event:
|
||||
- pull_request
|
||||
|
||||
steps:
|
||||
- name: build website
|
||||
pull: always
|
||||
image: golang:1.19
|
||||
commands:
|
||||
- make clean
|
||||
- make build
|
||||
|
||||
- name: "[PR] create s3 bucket"
|
||||
when:
|
||||
event:
|
||||
- pull_request
|
||||
pull: always
|
||||
image: alpine:latest
|
||||
environment:
|
||||
AWS_ACCESS_KEY_ID:
|
||||
from_secret: preview_aws_access_token
|
||||
AWS_SECRET_ACCESS_KEY:
|
||||
from_secret: preview_aws_secret_access_key
|
||||
AWS_REGION: eu-central-1
|
||||
commands:
|
||||
- apk add --no-cache aws-cli
|
||||
- aws s3api create-bucket --acl public-read --bucket preview-gitea-org-website-${DRONE_REPO_NAME}-${DRONE_PULL_REQUEST} --region $${AWS_REGION} --create-bucket-configuration LocationConstraint=$${AWS_REGION} || /bin/true
|
||||
- aws s3 website s3://preview-gitea-org-website-${DRONE_REPO_NAME}-${DRONE_PULL_REQUEST}/ --index-document index.html --error-document error.html || /bin/true
|
||||
|
||||
- name: "[PR] deploy website to S3 bucket"
|
||||
when:
|
||||
event:
|
||||
- pull_request
|
||||
pull: always
|
||||
image: plugins/s3-sync
|
||||
environment:
|
||||
AWS_ACCESS_KEY_ID:
|
||||
from_secret: preview_aws_access_token
|
||||
AWS_SECRET_ACCESS_KEY:
|
||||
from_secret: preview_aws_secret_access_key
|
||||
AWS_REGION: eu-central-1
|
||||
settings:
|
||||
source: public/
|
||||
target: /
|
||||
region: eu-central-1
|
||||
bucket: preview-gitea-org-website-${DRONE_REPO_NAME}-${DRONE_PULL_REQUEST}
|
||||
acl: public-read
|
||||
|
||||
- name: "[PR] Post comment to PR"
|
||||
when:
|
||||
event:
|
||||
- pull_request
|
||||
image: byrnedo/alpine-curl
|
||||
environment:
|
||||
GITEA_TOKEN:
|
||||
from_secret: access_token
|
||||
AWS_REGION: eu-central-1
|
||||
commands:
|
||||
# approach: check if comment already exists to prevent spamming in future runs
|
||||
- 'COMMENTS=$(curl -sL -X GET -H "Authorization: token $GITEA_TOKEN" https://gitea.com/api/v1/repos/${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}/issues/${DRONE_PULL_REQUEST}/comments)'
|
||||
- 'if [[ $COMMENTS == "[]" ]]; then curl -sL -X POST -H "Authorization: token $GITEA_TOKEN" -H "Content-type: application/json" https://gitea.com/api/v1/repos/${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}/issues/${DRONE_PULL_REQUEST}/comments -d "{\"body\": \"Website preview: http://preview-gitea-org-website-${DRONE_REPO_NAME}-${DRONE_PULL_REQUEST}.s3-website.$${AWS_REGION}.amazonaws.com/\"}"; else echo -e "\n INFO: Comment already exist, doing nothing"; fi'
|
||||
|
||||
### NB: not working as of 2023-02-06 due to Drone ignoring the Gitea webhook for PR close events: https://community.harness.io/t/closing-pull-request/13205
|
||||
# - name: "[PR] Delete S3 bucket after closing PR"
|
||||
# image: byrnedo/alpine-curl
|
||||
# environment:
|
||||
# preview_aws_access_token:
|
||||
# from_secret: preview_aws_access_token
|
||||
# preview_aws_secret_access_key:
|
||||
# from_secret: preview_aws_secret_access_key
|
||||
# AWS_REGION: eu-central-1
|
||||
# commands:
|
||||
# - apk add --no-cache jq
|
||||
# # check if PR got closed
|
||||
# - "PR_STATE=$(curl https://gitea.com/api/v1/repos/${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}/pulls/${DRONE_PULL_REQUEST} | jq -r .state)"
|
||||
# # delete S3 if PR is closed
|
||||
# - "if [[ $PR_STATE == 'closed' ]]; then aws s3 rb s3://preview-gitea-org-website-${DRONE_REPO_NAME}-${DRONE_PULL_REQUEST} --force; else echo -e '\n INFO: PR not in state closed, doing nothing'; fi"
|
||||
|
||||
volumes:
|
||||
- name: cache
|
||||
temp: {}
|
19
.gitea/workflows/delete-preview.yml
Normal file
19
.gitea/workflows/delete-preview.yml
Normal file
@ -0,0 +1,19 @@
|
||||
name: delete closed PR preview s3
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [closed]
|
||||
|
||||
env:
|
||||
AWS_REGION: eu-central-1
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
||||
|
||||
jobs:
|
||||
delete_preview:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: install aws cli
|
||||
run: apt update -y && apt install -y awscli
|
||||
- name: delete bucket
|
||||
run: aws s3 rb s3://preview-gitea-org-website-${{ github.event.repository.name }}-${{ github.event.number }} --force || /bin/true
|
@ -1,24 +1,52 @@
|
||||
name: check-and-test
|
||||
|
||||
on:
|
||||
- push
|
||||
- pull_request
|
||||
|
||||
name: build and deploy website PR preview
|
||||
env:
|
||||
AWS_REGION: eu-central-1
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
jobs:
|
||||
check-and-test:
|
||||
build-and-deploy:
|
||||
name: build and deploy
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: Setup Hugo
|
||||
uses: https://github.com/peaceiris/actions-hugo@v2
|
||||
with:
|
||||
hugo-version: '0.111.3'
|
||||
hugo-version: "0.111.3"
|
||||
extended: true
|
||||
- name: setup go
|
||||
- name: Setup Go
|
||||
uses: https://github.com/actions/setup-go@v4
|
||||
with:
|
||||
go-version: '>=1.20.1'
|
||||
go-version: ">=1.20.1"
|
||||
- name: Build
|
||||
run: |
|
||||
make clean
|
||||
make build
|
||||
make build
|
||||
- name: Install AWS Client Tool
|
||||
run: apt update -y && apt install -y awscli
|
||||
- name: "[PR] create s3 bucket"
|
||||
env:
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
||||
run: |
|
||||
aws s3api create-bucket --acl public-read --bucket preview-gitea-org-website-${{ github.event.repository.name }}-${{ github.event.number }} --region ${{ env.AWS_REGION }} --create-bucket-configuration LocationConstraint=${{ env.AWS_REGION }} || /bin/true
|
||||
aws s3 website s3://preview-gitea-org-website-${{ github.event.repository.name }}-${{ github.event.number }}/ --index-document index.html --error-document error.html || /bin/true
|
||||
- name: "[PR] deploy website to S3 bucket"
|
||||
uses: https://github.com/jakejarvis/s3-sync-action@master
|
||||
with:
|
||||
args: --acl public-read --follow-symlinks --delete
|
||||
env:
|
||||
AWS_S3_BUCKET: preview-gitea-org-website-${{ github.event.repository.name }}-${{ github.event.number }}
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
||||
AWS_REGION: ${{ env.AWS_REGION }}}
|
||||
SOURCE_DIR: public/
|
||||
DEST_DIR: /
|
||||
- name: "[PR] Post comment to PR"
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.COMMENT_ACCESS_TOKEN }}
|
||||
run: |
|
||||
'COMMENTS=$(curl -sL -X GET -H "Authorization: token $GITEA_TOKEN" https://gitea.com/api/v1/repos/${{ github.event.repository.owner }}/${{ github.event.repository.name }}/issues/${{ github.event.number }}/comments)'
|
||||
'if [[ $COMMENTS == "[]" ]]; then curl -sL -X POST -H "Authorization: token $GITEA_TOKEN" -H "Content-type: application/json" https://gitea.com/api/v1/repos/${{ github.event.repository.owner }}/${{ github.event.repository.name }}/issues/${{ github.event.number }}/comments -d "{\"body\": \"Website preview: http://preview-gitea-org-website-${{ github.event.repository.name }}-${{ github.event.number }}.s3-website.${{ env.AWS_REGION }}}.amazonaws.com/\"}"; else echo -e "\n INFO: Comment already exist, doing nothing"; fi'
|
||||
|
Reference in New Issue
Block a user