-
Notifications
You must be signed in to change notification settings - Fork 132
144 lines (140 loc) · 7.49 KB
/
auto-update.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
name: auto-update
on:
schedule:
- cron: "0 0 * * *"
push:
branches:
- feat/github-actions
- main
permissions:
id-token: write
contents: write
pull-requests: write
concurrency:
group: deploy-prod-stack
jobs:
auto-update:
runs-on: ubuntu-latest
env:
AWS_REGION: ap-northeast-1
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
- name: Install serverless
run: npm install -g serverless@^3
- name: Note docker image digest
id: docker-image-digest
run: |
docker pull public.ecr.aws/lambda/python:latest
SHA256_DIGEST=$(docker inspect public.ecr.aws/lambda/python:latest | jq -r '.[0].RepoDigests[0] | split(":") | .[1]' )
echo "SHA256_DIGEST=${SHA256_DIGEST}" >> $GITHUB_OUTPUT
- name: Note Chromium versions
id: chrome-versions
run: |
WHOLE_JSON=$(curl https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json)
CHANNEL="Stable"
REVISION=$(echo $WHOLE_JSON | jq -r '.channels.Stable.revision')
echo "REVISION=${REVISION}" >> $GITHUB_OUTPUT
CHROME_VERSION=$(echo $WHOLE_JSON | jq -r '.channels.Stable.version')
echo "CHROME_VERSION=${CHROME_VERSION}" >> $GITHUB_OUTPUT
MAJOR_VERSION=$(echo $WHOLE_JSON | jq -r '.channels.Stable.version | split(".") | .[0]')
echo "MAJOR_VERSION=${MAJOR_VERSION}" >> $GITHUB_OUTPUT
- name: Update Selenium
id: selenium-version
run: |
echo "SELENIUM_VERSION=$(curl -s https://pypi.org/pypi/selenium/json | jq -r .info.version)" >> $GITHUB_OUTPUT
- name: Update Dockerfile
run: |
SHA256_DIGEST=${{ steps.docker-image-digest.outputs.SHA256_DIGEST }}
CHROME_VERSION=${{ steps.chrome-versions.outputs.CHROME_VERSION }}
SELENIUM_VERSION=${{ steps.selenium-version.outputs.SELENIUM_VERSION }}
sed -r "s/public.ecr.aws\/lambda\/python[:@a-z0-9]+/public.ecr.aws\/lambda\/python\@sha256\:${SHA256_DIGEST}/g; s/chrome-for-testing-public\/[0-9.]+/chrome-for-testing-public\/${CHROME_VERSION}/g; s/selenium==[0-9\.]*/selenium==${SELENIUM_VERSION}/g" -i Dockerfile
- name: Deploy
run: sls deploy
- name: Note chrome version
id: chrome-version
run: |
CHROME_VERSION=$(docker run --rm --entrypoint '' serverless-docker-selenium-lambda-prod:img /opt/chrome/chrome --version | awk '{print $2}' | sed -e 's/^[[:space:]]*//')
echo "CHROME_VERSION=${CHROME_VERSION}" >> $GITHUB_OUTPUT
- name: Note python version
id: python-version
run: |
PYTHON_VERSION=$(docker run --rm --entrypoint '' serverless-docker-selenium-lambda-prod:img python -V | awk '{print $2}')
echo "PYTHON_VERSION=${PYTHON_VERSION}" >> $GITHUB_OUTPUT
- name: Invoke
id: invoke
run: sls invoke -f demo > /tmp/scraping-result.txt
- name: Clean up (ECR costs)
run: sls remove
- name: Archive result
uses: actions/upload-artifact@v3
if: ${{ !env.ACT }}
with:
name: scraping-result
path: /tmp/scraping-result.txt
- name: Test
run: cat /tmp/scraping-result.txt | grep -q "This domain is for use in illustrative examples in documents"
- name: Update README
run: |
CHROME_VERSION=${{ steps.chrome-versions.outputs.CHROME_VERSION }}
SELENIUM_VERSION=${{ steps.selenium-version.outputs.SELENIUM_VERSION }}
PYTHON_VERSION=${{ steps.python-version.outputs.PYTHON_VERSION }}
sed -r "s/- chromium [0-9.]+/- chromium ${CHROME_VERSION}/g; s/- chromedriver [0-9.]+/- chromedriver ${CHROME_VERSION}/g; s/- selenium [0-9.]+/- selenium ${SELENIUM_VERSION}/g; s/- Python [0-9.]+/- Python ${PYTHON_VERSION}/g" -i README.md
- name: Detect changes
id: detect-changes
run: |
DO_RELEASE="yes"
git --no-pager diff --name-only | grep -q "README.md" || DO_RELEASE="no"
git --no-pager diff --name-only | grep -q "Dockerfile" || DO_RELEASE="no"
echo "DO_RELEASE=${DO_RELEASE}" >> $GITHUB_OUTPUT
- name: Setup git config
run: |
# https://qiita.com/thaim/items/3d1a4d09ec4a7d8844ce
git config user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Release
if: ${{ steps.detect-changes.outputs.DO_RELEASE == 'yes' && !env.ACT }}
run: |
BRANCH=$(date +%Y-%m-%d-%H%M%S)
TITLE="Version Updates $(date)"
git checkout -b $BRANCH
git add Dockerfile README.md
echo "SHA256_DIGEST=${{ steps.docker-image-digest.outputs.SHA256_DIGEST }}" > /tmp/body-text.txt
echo "REVISION=${{ steps.chrome-versions.outputs.REVISION }}" >> /tmp/body-text.txt
echo "PYTHON_VERSION=${{ steps.python-version.outputs.PYTHON_VERSION }}" >> /tmp/body-text.txt
echo "SELENIUM_VERSION=${{ steps.selenium-version.outputs.SELENIUM_VERSION }}" >> /tmp/body-text.txt
echo -e "CHROME_VERSION=${{ steps.chrome-versions.outputs.CHROME_VERSION }}\n\n" >> /tmp/body-text.txt
git diff --staged >> /tmp/body-text.txt
git commit -m "${TITLE}"
git push --set-upstream origin $BRANCH
gh pr create --body-file /tmp/body-text.txt --title "PR:${TITLE}"
gh pr merge --delete-branch --merge
gh release create $BRANCH --notes-file /tmp/body-text.txt --title "${TITLE}"
env:
GH_TOKEN: ${{ github.token }}
- name: Publish image
if: ${{ steps.detect-changes.outputs.DO_RELEASE == 'yes' && !env.ACT }}
env:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
run: |
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
PYTHON_VERSION=${{ steps.python-version.outputs.PYTHON_VERSION }}
SELENIUM_VERSION=${{ steps.selenium-version.outputs.SELENIUM_VERSION }}
CHROME_VERSION=${{ steps.chrome-versions.outputs.CHROME_VERSION }}
MAJOR_PYTHON_VERSION=$(echo $PYTHON_VERSION | cut -d "." -f 1)
MINOR_PYTHON_VERSION=$(echo $PYTHON_VERSION | cut -d "." -f 2)
docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:latest
docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${MAJOR_PYTHON_VERSION}
docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${MAJOR_PYTHON_VERSION}.${MINOR_PYTHON_VERSION}
docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${PYTHON_VERSION}
docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${PYTHON_VERSION}-selenium${SELENIUM_VERSION}
docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${PYTHON_VERSION}-chrome${CHROME_VERSION}
docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${PYTHON_VERSION}-selenium${SELENIUM_VERSION}-chrome${CHROME_VERSION}
docker image push --all-tags umihico/aws-lambda-selenium-python