diff --git a/.circleci/.gitignore b/.circleci/.gitignore new file mode 100644 index 00000000..bab8481a --- /dev/null +++ b/.circleci/.gitignore @@ -0,0 +1,2 @@ +cookiejar.txt +test.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 5c61e0f4..0ad9461e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -26,43 +26,50 @@ jobs: steps: - checkout - # - run: git checkout gh-pages + - run: git checkout gh-pages - # - run: | - # git clone https://github.com/Afforess/Factorio-Stdlib.git /tmp/stdlib - # cd /tmp/stdlib - # git checkout release - # make quick - # cp -R .build/doc/* ~/stdlib/ + - run: + name: Build Docs for gh-pages + command: | + git clone https://github.com/Afforess/Factorio-Stdlib.git /tmp/stdlib + cd /tmp/stdlib + git checkout release + make doc + cp -R .build/doc/* ~/stdlib/ - # - store_artifacts: - # path: /tmp/stdlib/.build/artifacts - # destination: files + - run: + name: Commit gh-pages + command: | + git config user.email "bot@circleci.com" + git config user.name "circleci" + git add . + git commit -am "Automatic Github Page generation" + git push - # - run: git config user.email "bot@circleci.com" - # - run: git config user.name "circleci" - # - run: git add . - # - run: git commit -am "Automatic Github Page generation" - # - run: git push - run: git checkout release - - run: | - NAME=$(jq -r '.name' info.json) - VERSION=$(jq -r '.version' info.json) - mkdir /tmp/upload - cd /tmp/upload - # Get a CSRF token by loading the login form - CSRF=$(curl -b cookiejar.txt -c cookiejar.txt -s https://mods.factorio.com/login | grep csrf_token | sed -r -e 's/.*value="(.*)".*/\1/') - # Authenticate with the credential secrets and the CSRF token, getting a session cookie for the authorized user - curl -b cookiejar.txt -c cookiejar.txt -s -F "csrf_token=${CSRF}" -F "username=${FACTORIO_USER}" -F "password=${FACTORIO_PASSWORD}" -o /dev/null https://mods.factorio.com/login - # Query the mod info, verify the version number we're trying to push doesn't already exist - curl -b cookiejar.txt -c cookiejar.txt -s "https://mods.factorio.com/api/mods/${NAME}/full" | jq -e ".releases[] | select(.version == \"${VERSION}\")" - # store the return code before running anything else - STATUS_CODE=$? - if [[ $STATUS_CODE -ne 4 ]]; then - echo "Release already exists, skipping" - exit 78 - fi - echo "Release doesn't exist for ${TAG}, uploading" + + - run: make quick + + - store_artifacts: + path: ~/stdlib/.build/artifacts + destination: files + + - run: + name: Export Vars + command: | + echo 'export NAME=$(jq -r '.name' info.json)' >> $BASH_ENV + echo 'export VERSION=$(jq -r '.version' info.json)' >> $BASH_ENV + echo 'FILEPATH="/root/stdlib/.build/artifacts/${NAME}_${VERSION}.zip"' >> $BASH_ENV + echo 'FILESIZE=$(stat --printf="%s" "${FILEPATH}")' >> $BASH_ENV + + - run: echo ${FILEPATH} ${FILESIZE} + + - run: + name: Upload Release + command: /root/stdlib/.circleci/release.sh + + - run: git tag -v${VERSION} + - run: git push --tags workflows: version: 2 @@ -75,8 +82,8 @@ workflows: - gh-pages - /feature-.*/ - deploy-release: - # requires: - # - build + requires: + - build filters: branches: only: release diff --git a/.circleci/release.sh b/.circleci/release.sh new file mode 100755 index 00000000..de7b46fa --- /dev/null +++ b/.circleci/release.sh @@ -0,0 +1,37 @@ +mkdir /tmp/upload +cd /tmp/upload + +echo ${FILEPATH} ${FILESIZE} +CSRF=$(curl -b cookiejar.txt -c cookiejar.txt -s https://mods.factorio.com/login | grep csrf_token | sed -r -e 's/.*value="(.*)".*/\1/') +# Authenticate with the credential secrets and the CSRF token, getting a session cookie for the authorized user +curl -b cookiejar.txt -c cookiejar.txt -s -F "csrf_token=${CSRF}" -F "username=${FACTORIO_USER}" -F "password=${FACTORIO_PASSWORD}" -o /dev/null https://mods.factorio.com/login +# Query the mod info, verify the version number we're trying to push doesn't already exist +curl -b cookiejar.txt -c cookiejar.txt -s "https://mods.factorio.com/api/mods/${NAME}/full" | jq -e ".releases[] | select(.version == \"${VERSION}\")" +# store the return code before running anything else +STATUS_CODE=$? +if [[ $STATUS_CODE -ne 4 ]]; then + echo "Release already exists, skipping" + exit 78 +fi +echo "Release doesn't exist for ${VERSION}, uploading" +# Load the upload form, getting an upload token +UPLOAD_TOKEN=$(curl -b cookiejar.txt -c cookiejar.txt -s "https://mods.factorio.com/mod/${NAME}/downloads/edit" | grep token | sed -r -e "s/.*token: '(.*)'.*/\1/") +if [[ -z "${UPLOAD_TOKEN}" ]]; then + echo "Couldn't get an upload token, failed" + exit 1 +fi +# Upload the file, getting back a response with details to send in the final form submission to complete the upload +UPLOAD_RESULT=$(curl -b cookiejar.txt -c cookiejar.txt -s -F "file=@${FILEPATH};type=application/x-zip-compressed" "https://direct.mods-data.factorio.com/upload/mod/${UPLOAD_TOKEN}") +# Parse 'em and stat the file for the form fields +CHANGELOG=$(echo "${UPLOAD_RESULT}" | jq -r '@uri "\(.changelog)"') +INFO=$(echo "${UPLOAD_RESULT}" | jq -r '@uri "\(.info)"') +FILENAME=$(echo "${UPLOAD_RESULT}" | jq -r '.filename') +THUMBNAIL=$(echo "${UPLOAD_RESULT}" | jq -r '.thumbnail // empty') +if [[ "${FILENAME}" == "null" ]] || [[ -z "${FILENAME}" ]]; then + echo "Upload failed" + exit 1 +fi +echo "Uploaded ${NAME}_${VERSION}.zip to ${FILENAME}, submitting as new version" +# Post the form, completing the release +curl -b cookiejar.txt -c cookiejar.txt -s -X POST -d "file=&info_json=${INFO}&changelog=${CHANGELOG}&filename=${FILENAME}&file_size=${FILESIZE}&thumbnail=${THUMBNAIL}" -H "Content-Type: application/x-www-form-urlencoded" -o /dev/null "https://mods.factorio.com/mod/${NAME}/downloads/edit" +echo "Completed" diff --git a/info.json b/info.json index 60c14511..8b73ae6b 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "stdlib", - "version": "1.0.3", + "version": "1.0.4", "factorio_version": "0.17", "title": "Factorio Standard Library", "author": "Afforess",