Created
February 12, 2022 01:30
-
-
Save comaniac/3857b1965efae780eda0aef35016a163 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# The entry point of AWS Batch job. This script is in charge of configuring | |
# the repo, executing the given command, and uploading the results. | |
set -e | |
date | |
# Parse arguments | |
SOURCE_REF=$1 | |
REPO=$2 | |
COMMAND=$3 | |
SAVE_OUTPUT=$4 | |
REMOTE_FOLDER=$5 # e.g., s3://... | |
echo "Job Info" | |
echo "-------------------------------------" | |
echo "jobId: $AWS_BATCH_JOB_ID" | |
echo "jobQueue: $AWS_BATCH_JQ_NAME" | |
echo "computeEnvironment: $AWS_BATCH_CE_NAME" | |
echo "SOURCE_REF: $SOURCE_REF" | |
echo "REPO: $REPO" | |
echo "S3_PATH: $S3_PATH" | |
echo "CCACHE_DIR: $CCACHE_DIR" | |
echo "SAVE_OUTPUT: $SAVE_OUTPUT" | |
echo "REMOTE_FOLDER: $REMOTE_FOLDER" | |
echo "COMMAND: $COMMAND" | |
echo "-------------------------------------" | |
if [ -z $GITHUB_TOKEN ]; then | |
echo "GITHUB_TOKEN is not set" | |
exit 1 | |
fi; | |
# Checkout the repo. | |
git clone https://$GITHUB_TOKEN:[email protected]/$REPO repo --recursive | |
cd repo | |
# Config the repo | |
git fetch origin $SOURCE_REF:working | |
git checkout working | |
git submodule update --init --recursive --force | |
# Execute the command | |
/bin/bash -o pipefail -c "$COMMAND" | |
COMMAND_EXIT_CODE=$? | |
# Upload results | |
if [ ! -z $SAVE_OUTPUT ]; then | |
FULL_S3_PATH=$REMOTE_FOLDER/$AWS_BATCH_JOB_ID | |
echo "Uploading results to $FULL_S3_PATH" | |
if [[ -f $SAVE_OUTPUT ]]; then | |
aws s3 cp $SAVE_OUTPUT $FULL_S3_PATH/ --quiet; | |
elif [[ -d $SAVED_OUTPUT ]]; then | |
aws s3 cp --recursive $SAVE_OUTPUT $FULL_S3_PATH/ --quiet; | |
fi | |
fi | |
exit $COMMAND_EXIT_CODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment