Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PM2 Official Docker Repository #52

Open
simonepri opened this issue Jan 3, 2018 · 14 comments
Open

PM2 Official Docker Repository #52

simonepri opened this issue Jan 3, 2018 · 14 comments

Comments

@simonepri
Copy link

simonepri commented Jan 3, 2018

As discussed in #25 it would be interesting to see if we can publish this image as "Official" on the Docker Hub.

The process seems quite simple. We have to simply open a PR here adding a file called pm2 inside the library folder of the repo.

The pm2 file can be generated automagically using this script i wrote.

So why we don't do that?
It seems that the images are rebuilt manually by the docker team. This means that we are not able to setup any sort of automated build like the one we have now.

@simonepri
Copy link
Author

Script usage example

bash docker-tags.sh > pm2
image

@vmarchaud
Copy link

As discussed before with @Unitech, it would be helpful to have it the official pm2 name for newcomers. Do you want to make the PR @simonepri ?

@simonepri
Copy link
Author

simonepri commented Jan 8, 2018

@vmarchaud I'm sorry but I haven't understood what you have wrote.
Can you re-articulate? Thanks.

@simonepri
Copy link
Author

I post here some relevant massages while talking with @Unitech

simonepri
Anyway about the official image i've opened this issue #52

unitech
About the official pm2 image, Someone working at docker told me that we need to make a PR each time we do a new release

simonepri
The problem is that we don't do new releases (in general). The images are just rebuilt automatically when the projects on which we depend on gets updated.
The only way I see is to setup a webhook here:
https://hub.docker.com/r/keymetrics/pm2/~/settings/webhooks/
And somehow ping a script that runs the script I wrote and creates the PR automatically.

@vmarchaud
Copy link

@simonepri Simply put : lets do it
For the hook, we are currently building release pipeline for deb, rpm and alpine distributions, see https://github.com/Unitech/pm2/blob/master/.drone.yml and https://github.com/Unitech/pm2/tree/master/packager
We use https://drone.io CI internally at @keymetrics, it's docker based so every step is a docker image.

What can be done is a docker image that use a bot account to make a PR automatically then we will add it to our CI, what do you think ?

@simonepri
Copy link
Author

simonepri commented Jan 8, 2018

Yes looks good.
But how to create this bot? We have to create a new fake GitHub user?
Sorry but it's the first time I do something like this.

This is a draft of the publishing script that works in case we create a fake GitHub account called keymetrics-bot (We can also use the @Unitech account 😄)
At the moment it seems to work. The only problem is the hub login, but I think that if the $GITHUB_TOKEN env is set then hub does not ask for the interactive login.

pr.sh:

#!/bin/bash

hash git 2>/dev/null || { echo >&2 "git not found, exiting."; }
hash hub 2>/dev/null || { echo >&2 "hub not found, exiting."; }

username='keymetrics-bot'

# Generate pm2 file
rm -rf ./docker-pm2/
git clone https://github.com/keymetrics/docker-pm2.git
cd ./docker-pm2/
bash docker-tags.sh > ../pm2
cd ..
rm -rf ./docker-pm2/

echo ""

# Fork their repo
rm -rf ./official-images/
git clone https://github.com/docker-library/official-images.git
cd ./official-images/
hub fork
cd ..
rm -rf ./official-images/

echo ""

# Clone our fork
git clone https://github.com/$username/official-images.git
cd official-images
git remote add upstream https://github.com/docker-library/official-images.git
git fetch upstream

echo ""

# Reset our master in case the fork already exists
git reset --hard upstream/master
git clean -f -d
git push -f

echo ""

# Move the pm2 file
mkdir -p $(dirname ./library/pm2)
mv ../pm2 ./library/pm2

echo ""

# Make the PR
git add .
git commit -m "Update pm2 images"
git push
hub pull-request -m "Update pm2 images"

@simonepri
Copy link
Author

simonepri commented Jan 9, 2018

This morning I had an idea on how we can actually setup the entire workflow without hosting nothing on our end.

Steps:

  • Create a repo on the Docker Hub under keymetrics/pm2-trigger-official
  • Link pm2-trigger-official Docker Hub repo with docker-pm2 GitHub repo (Triggers build each time a push is performed on docker-pm2 GitHub repo)
  • Link pm2-trigger-official Docker Hub repo to node Docker Hub repo (Triggers build each time a new release is built in node Docker Hub repo)
  • Add the trigger URL of the repo inside the WebHook section of the GitHub pm2 repo (Triggers build each time a push is performed on pm2 GitHub repo)
  • Create the script that generates the pm2 file for the docker-library GitHub repo (script)
  • Create the script that performs the Pull Request on the docker-library GitHub repo (draft)
  • Create a Dockerfile inside docker-pm2 GitHub repo that simply runs the script that performs the PR
  • Setup the keymetrics/pm2-trigger-official Docker Hub repo to perform the build process of the previously created Dockerfile
  • Wait the Docker's Team to publish for the first time the images under their official channel
  • Deprecate all the other images we have published 😭
  • Enjoy

Everything should work, but I think is bit tricky as system.
Maybe it's better to just stick with the actual setup and don't care about the official image.
What's the real advantage of the official image for the users?

Also I believe that keeping keymetrics before the pm2 name improve the visibility of the company 👍.

@vmarchaud
Copy link

I think the following order will work and seems shorter :

  • Add a webhook to trigger the build of the image on keymetrics/pm2-docker from the Unitech/pm2 repo
  • Add a webhook to our CI when keymetrics/pm2-docker is built from the dockre hub
  • Create the image with your script inside
  • Create an account on Github (that has access to the fork) and give it as a secret to our CI
  • Enjoy

I'm not sure if we need to have the first three step, what do you think ?

@simonepri
Copy link
Author

simonepri commented Jan 9, 2018

Thekeymetrics/pm2-docker will not exist anymore after we move it to the official channel.
This means that we cannot use the WebHook of keymetrics/pm2-docker, that's the reason why I was saying to create a new empty Docker Hub repo (can be also a private repo) to be able to create this WebHook.

@vmarchaud
Copy link

Oh i see, so yeah indeed, your setup is fine. I will create this and give you access. Can you come on http://slack.pm2.io so i can give you the secret of the account i will create ?

@simonepri
Copy link
Author

Yes of course, I'm a bit busy now. I'll reach you in the afternoon.

Anyway I miss a little, important, detail. The "empty" Dockerfile that actually must be private.
By the way with the my setup we don't need your CI at all:

I've done this to recap the idea:
image
(sorry the bad calligraphy)

We need to keep both private because we need to hardcode the $GITHUB_TOKEN inside the Dockerfile and we can't make it public.

There's anything I'm missing?

@vmarchaud
Copy link

No problem, i will be away the next hour also. Lets discuss on slack when you are available :)

@simonepri
Copy link
Author

simonepri commented Jan 9, 2018

The Dockerfile inside the pm2-trig-official private repo should look like this:

FROM alpine:latest

# Install git and hub
RUN apk update && apk upgrade && apk add --no-cache git hub

# Copy the script
COPY pr.sh .

# Run the script
RUN bash pr.sh

The repo folder structure should be:

|-- Dockerfile
`-- pr.sh (or whatever name we want to use)

Inside the script we have to hardcode the token for GitHub.

@simonepri
Copy link
Author

simonepri commented Jan 10, 2018

Dockerfile

FROM alpine:latest

# Updating apk
RUN echo http://dl-cdn.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories
RUN apk update

# Install needed tools
RUN apk add git hub bash

# Copy the script
COPY pr.sh .

# Run the script
RUN bash pr.sh

pr.sh

#!/bin/bash

function ee {
  echo -e "\033[31m  ✘ \033[0m$1"
  exit 1
}

# GitHub auth info
token='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
username='username'
email='[email protected]'

# Repo on which to submit the PR
owner='owner'
repo='repo'

# Checks if the neded tools are installed
hash git 2>/dev/null || { ee "git not found, exiting."; }
hash hub 2>/dev/null || { ee "hub not found, exiting."; }

# Prints git and hub versions
hub version

# Configure hub
hub config --global hub.protocol https
hub config --global user.email $email
hub config --global user.name $username
export GITHUB_TOKEN=$token

echo ""
echo "Logged as $(hub config --get user.name) <$(hub config --get user.email)>"

echo ""
echo "Cloning https://github.com/keymetrics/docker-pm2.git"

# Clones docker-pm2 repo
rm -rf ./docker-pm2/
hub clone keymetrics/docker-pm2

echo ""
echo "Executing docker-tags.sh"

# Generates pm2 file
cd ./docker-pm2/
bash docker-tags.sh > ../pm2 || { ee "unable to execute, exiting."; }
cd ..

# Removes the folder created
rm -rf ./docker-pm2/

echo ""
echo "Cloning https://github.com/$owner/$repo.git"

# Clones their repo
rm -rf ./$repo/
hub clone $owner/$repo ||  { ee "unable to clone, exiting."; }

cd ./$repo/

echo ""
echo "Forking https://github.com/$owner/$repo.git"

# Makes the fork
hub fork ||  { ee "unable to fork, exiting."; }

echo ""
echo "Updating remotes"

# Update remotes
hub remote rename origin upstream
hub remote rename $username origin
hub remote set-url upstream https://$token:[email protected]/$owner/$repo.git
hub remote set-url origin https://$token:[email protected]/$username/$repo.git
hub remote -v
hub pull --all

echo ""
echo "Syncing master with $(hub remote get-url upstream)"

# Resets our master to be event with the upstream
hub reset --hard upstream/master
hub clean -f -d
hub push origin -f || { ee "unable to push, exiting."; }

echo ""
echo "Applying changes"

# Moves the pm2 file
mkdir -p $(dirname ./library/pm2)
mv ../pm2 ./library/pm2

echo ""
echo "Pushing changes to $(hub remote get-url origin)"

# Pushes changes to our fork
hub add library/pm2
hub commit -a -m "Update pm2 images"
hub push origin -f || { ee "unable to push, exiting."; }

echo ""
echo "Creating the Pull Request"

# Makes the PR
hub pull-request -m "Update pm2 images"  || { ee "unable to create the pr, exiting."; }

cd ..

# Removes the folder created
rm -rf ./$repo/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants