|
| 1 | +name: auto-update |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: "0 0 * * *" |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - feat/github-actions |
| 9 | + |
| 10 | +jobs: |
| 11 | + auto-update: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout |
| 16 | + uses: actions/checkout@v3 |
| 17 | + - name: Install serverless |
| 18 | + run: npm install -g serverless |
| 19 | + - name: Note docker image digest |
| 20 | + id: docker-image-digest |
| 21 | + run: | |
| 22 | + docker pull public.ecr.aws/lambda/python:latest |
| 23 | + SHA256_DIGEST=$(docker inspect public.ecr.aws/lambda/python:latest | jq -r '.[0].RepoDigests[0] | split(":") | .[1]' ) |
| 24 | + echo "SHA256_DIGEST=${SHA256_DIGEST}" >> $GITHUB_OUTPUT |
| 25 | + - name: Note Chromium versions |
| 26 | + id: chromium-versions |
| 27 | + run: | |
| 28 | + WHOLE_JSON=$(curl https://omahaproxy.appspot.com/all.json) |
| 29 | + JSON=$(echo $WHOLE_JSON | jq -r '.[] | select(.os=="linux") | .versions[] | select(.channel=="stable")') |
| 30 | + CHANNEL="stable" |
| 31 | + POSITION=$(echo $JSON | jq -r '.branch_base_position') |
| 32 | + if (( POSITION < 900000 )); then |
| 33 | + JSON=$(echo $WHOLE_JSON | jq -r '.[] | select(.os=="linux") | .versions[] | select(.channel=="beta")') |
| 34 | + CHANNEL="beta" |
| 35 | + POSITION=$(echo $JSON | jq -r '.branch_base_position') |
| 36 | + echo "CHANNEL=${CHANNEL}" >> $GITHUB_OUTPUT |
| 37 | + fi |
| 38 | +
|
| 39 | + MAJOR_VERSION=$(echo $JSON | jq -r '.version | split(".") | .[0]') |
| 40 | + echo "MAJOR_VERSION=${MAJOR_VERSION}" >> $GITHUB_OUTPUT |
| 41 | +
|
| 42 | + for _ in {1..100}; do |
| 43 | + DOWNLOAD_URL="https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/Linux_x64%2F${POSITION}%2Fchrome-linux.zip?alt=media" |
| 44 | + curl -I $DOWNLOAD_URL | head -1 | grep -q 404 || break |
| 45 | + POSITION=$(($POSITION-1)) |
| 46 | + sleep 1 |
| 47 | + done |
| 48 | +
|
| 49 | + echo "POSITION=${POSITION}" >> $GITHUB_OUTPUT |
| 50 | + - name: Note chromedriver version |
| 51 | + id: chromedriver-version |
| 52 | + run: | |
| 53 | + MAJOR_VERSION=${{ steps.chromium-versions.outputs.MAJOR_VERSION }} |
| 54 | + DRIVER_VERSION=$(curl "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${MAJOR_VERSION}") |
| 55 | + echo "DRIVER_VERSION=${DRIVER_VERSION}" >> $GITHUB_OUTPUT |
| 56 | + - name: Update Dockerfile |
| 57 | + run: | |
| 58 | + SHA256_DIGEST=${{ steps.docker-image-digest.outputs.SHA256_DIGEST }} |
| 59 | + POSITION=${{ steps.chromium-versions.outputs.POSITION }} |
| 60 | + DRIVER_VERSION=${{ steps.chromedriver-version.outputs.DRIVER_VERSION }} |
| 61 | + sed -r "s/public.ecr.aws\/lambda\/python[:@a-z0-9]+/public.ecr.aws\/lambda\/python\@sha256\:${SHA256_DIGEST}/g; s/chromedriver.storage.googleapis.com\/[0-9.]+/chromedriver.storage.googleapis.com\/${DRIVER_VERSION}/g; s/Linux_x64%2F[0-9]+%2Fchrome-linux.zip/Linux_x64%2F${POSITION}%2Fchrome-linux.zip/g" -i Dockerfile |
| 62 | + - name: Deploy |
| 63 | + run: sls deploy |
| 64 | + env: |
| 65 | + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 66 | + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 67 | + AWS_REGION: ${{ secrets.AWS_REGION }} |
| 68 | + - name: Note chrome version |
| 69 | + id: chrome-version |
| 70 | + run: | |
| 71 | + CHROME_VERSION=$(docker run --rm --entrypoint '' serverless-docker-selenium-lambda-prod:img /opt/chrome/chrome --version | awk '{print $2}' | sed -e 's/^[[:space:]]*//') |
| 72 | + echo "CHROME_VERSION=${CHROME_VERSION}" >> $GITHUB_OUTPUT |
| 73 | + - name: Note selenium version |
| 74 | + id: selenium-version |
| 75 | + run: | |
| 76 | + SELENIUM_VERSION=$(docker run --rm --entrypoint '' serverless-docker-selenium-lambda-prod:img pip freeze | grep selenium | awk -F "==" '{print $2}') |
| 77 | + echo "SELENIUM_VERSION=${SELENIUM_VERSION}" >> $GITHUB_OUTPUT |
| 78 | + - name: Note python version |
| 79 | + id: python-version |
| 80 | + run: | |
| 81 | + PYTHON_VERSION=$(docker run --rm --entrypoint '' serverless-docker-selenium-lambda-prod:img python -V | awk '{print $2}') |
| 82 | + echo "PYTHON_VERSION=${PYTHON_VERSION}" >> $GITHUB_OUTPUT |
| 83 | + - name: Invoke |
| 84 | + id: invoke |
| 85 | + run: sls invoke -f demo > /tmp/scraping-result.txt |
| 86 | + env: |
| 87 | + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 88 | + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 89 | + AWS_REGION: ${{ secrets.AWS_REGION }} |
| 90 | + - name: Archive result |
| 91 | + uses: actions/upload-artifact@v3 |
| 92 | + if: ${{ !env.ACT }} |
| 93 | + with: |
| 94 | + name: scraping-result |
| 95 | + path: /tmp/scraping-result.txt |
| 96 | + - name: Test |
| 97 | + run: cat /tmp/scraping-result.txt | grep -q "This domain is for use in illustrative examples in documents" |
| 98 | + - name: Update README |
| 99 | + run: | |
| 100 | + CHROME_VERSION=${{ steps.chrome-version.outputs.CHROME_VERSION }} |
| 101 | + DRIVER_VERSION=${{ steps.chromedriver-version.outputs.DRIVER_VERSION }} |
| 102 | + SELENIUM_VERSION=${{ steps.selenium-version.outputs.SELENIUM_VERSION }} |
| 103 | + PYTHON_VERSION=${{ steps.python-version.outputs.PYTHON_VERSION }} |
| 104 | + sed -r "s/- chromium [0-9.]+/- chromium ${CHROME_VERSION}/g; s/- chromedriver [0-9.]+/- chromedriver ${DRIVER_VERSION}/g; s/- selenium [0-9.]+/- selenium ${SELENIUM_VERSION}/g; s/- Python [0-9.]+/- Python ${PYTHON_VERSION}/g" -i README.md |
| 105 | + - name: Detect changes |
| 106 | + id: detect-changes |
| 107 | + run: | |
| 108 | + DO_RELEASE="yes" |
| 109 | + git --no-pager diff --name-only | grep -q "README.md" || DO_RELEASE="no" |
| 110 | + git --no-pager diff --name-only | grep -q "Dockerfile" || DO_RELEASE="no" |
| 111 | + echo "DO_RELEASE=${DO_RELEASE}" >> $GITHUB_OUTPUT |
| 112 | + - name: Setup git config |
| 113 | + run: | |
| 114 | + # https://qiita.com/thaim/items/3d1a4d09ec4a7d8844ce |
| 115 | + git config user.name "github-actions[bot]" |
| 116 | + git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 117 | + - name: Release |
| 118 | + if: ${{ steps.detect-changes.outputs.DO_RELEASE == 'yes' && !env.ACT }} |
| 119 | + run: | |
| 120 | + BRANCH=$(date +%Y-%m-%d-%H%M%S) |
| 121 | + TITLE="Version Updates $(date)" |
| 122 | + git checkout -b $BRANCH |
| 123 | + git add Dockerfile README.md |
| 124 | + echo "SHA256_DIGEST=${{ steps.docker-image-digest.outputs.SHA256_DIGEST }}" > /tmp/body-text.txt |
| 125 | + echo "POSITION=${{ steps.chromium-versions.outputs.POSITION }}" >> /tmp/body-text.txt |
| 126 | + echo "DRIVER_VERSION=${{ steps.chromedriver-version.outputs.DRIVER_VERSION }}" >> /tmp/body-text.txt |
| 127 | + echo "PYTHON_VERSION=${{ steps.python-version.outputs.PYTHON_VERSION }}" >> /tmp/body-text.txt |
| 128 | + echo "SELENIUM_VERSION=${{ steps.selenium-version.outputs.SELENIUM_VERSION }}" >> /tmp/body-text.txt |
| 129 | + echo "CHROME_VERSION=${{ steps.chrome-version.outputs.CHROME_VERSION }}" >> /tmp/body-text.txt |
| 130 | + echo "\n```diff\n$(git diff --staged)\n```" >> /tmp/body-text.txt |
| 131 | + git commit -m "${TITLE}" |
| 132 | + git push --set-upstream origin $BRANCH |
| 133 | + gh pr create --body-file /tmp/body-text.txt --title "PR:${TITLE}" |
| 134 | + gh pr merge --delete-branch --merge $BRANCH |
| 135 | + gh release create $BRANCH --notes-file /tmp/body-text.txt --title "${TITLE}" |
| 136 | + env: |
| 137 | + GH_TOKEN: ${{ github.token }} |
| 138 | + - name: Publish image |
| 139 | + if: ${{ steps.detect-changes.outputs.DO_RELEASE == 'yes' && !env.ACT }} |
| 140 | + run: | |
| 141 | + echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin |
| 142 | + PYTHON_VERSION=${{ steps.python-version.outputs.PYTHON_VERSION }} |
| 143 | + SELENIUM_VERSION=${{ steps.selenium-version.outputs.SELENIUM_VERSION }} |
| 144 | + CHROME_VERSION=${{ steps.chrome-version.outputs.CHROME_VERSION }} |
| 145 | + MAJOR_PYTHON_VERSION=$(echo $PYTHON_VERSION | cut -d "." -f 1) |
| 146 | + MINOR_PYTHON_VERSION=$(echo $PYTHON_VERSION | cut -d "." -f 2) |
| 147 | + docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:latest |
| 148 | + docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${MAJOR_PYTHON_VERSION} |
| 149 | + docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${MAJOR_PYTHON_VERSION}.${MINOR_PYTHON_VERSION} |
| 150 | + docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${PYTHON_VERSION} |
| 151 | + docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${PYTHON_VERSION}-selenium${SELENIUM_VERSION} |
| 152 | + docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${PYTHON_VERSION}-chrome${CHROME_VERSION} |
| 153 | + docker image tag serverless-docker-selenium-lambda-prod:img umihico/aws-lambda-selenium-python:${PYTHON_VERSION}-selenium${SELENIUM_VERSION}-chrome${CHROME_VERSION} |
| 154 | + docker image push --all-tags umihico/aws-lambda-selenium-python |
0 commit comments